September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Conditional Statements Introduction to Computing Science and Programming I.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Structured programming
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
The University of Texas – Pan American
Fundamentals of Python: First Programs
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Flow Charting. Goals Create Algorithms using Flow Charting procedures. Distinguish between Flow Charting and Pseudocode. Top-Down Design Bottom-up Design.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Guide to Programming with Python Chapter Three Branching, while Loops, and Program Planning: The Guess My Number Game.
Decision Structures and Boolean Logic
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Mastery Objective: Students will understand how to use while loops in computer programming.
Flow of Control Part 1: Selection
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
© Jalal Kawash Programming Peeking into Computer Science 1.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Flow of Control Chapter 3 Flow of control Branching Loops
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Course A201: Introduction to Programming 09/16/2010.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
CPS120: Introduction to Computer Science Decision Making in Programs.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Controlling Program Flow with Decision Structures.
Decision Making and Branching
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Control Flow (Python) Dr. José M. Reyes Álamo.
REPETITION CONTROL STRUCTURE
Chapter 3: Decisions and Loops
Guide to Programming with Python
The switch Statement, and Introduction to Looping
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Chapter 2.1 Control Structures (Selection)
Topics The if Statement The if-else Statement Comparing Strings
If, else, elif.
Topics The if Statement The if-else Statement Comparing Strings
Structured Program
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
3 Control Statements:.
Chapter 3 – Control Structures
Introduction to Programming Using Python PART 2
Selection Statements.
Computer Science Core Concepts
Boolean Expressions to Make Comparisons
Structural Program Development: If, If-Else
Presentation transcript:

September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of Computer Science Kent State University

September 7, 2004ICP: Chapter 3: Control Structures2 Contents Generating Numbers with randrange() Using if, if-else, if-elif-else Structures Using while Loops Planning Programs with Pseudocode

September 7, 2004ICP: Chapter 3: Control Structures3 Guess My Number Example: Guess My Number

September 7, 2004ICP: Chapter 3: Control Structures4 Generating Random Numbers Programs often must generate random numbers to simulate events that are often based on probability You can import the random module into your Python program Use the randrange() function (method) to generate random numbers in a given range. –Not really a “true” random number generator…it is a pseudo-random number generator

September 7, 2004ICP: Chapter 3: Control Structures5 Generating Random Numbers Use the import statement to include a module –Files that contain functions that can be used in any program –Import statements are usually at the top of your Python program Example –import random

September 7, 2004ICP: Chapter 3: Control Structures6 Generating Random Numbers The randrange() function will return a random integer in the range [start..end) –From the value start up to but not including end. –Example anydigit = random.randrange(10) [0..10) -> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 die = random.randrange(6) + 1 [1..7) = [1..6] -> 1, 2, 3, 4, 5, 6 –Example: Craps RollerExample: Craps Roller

September 7, 2004ICP: Chapter 3: Control Structures7 Using the if Structure Branching is how computers and computer programs make decisions –Make a decision to take one path or another The if structure allows your program to branch to a section of code…or skip it Example: Password Program

September 7, 2004ICP: Chapter 3: Control Structures8 Using the if Structure All if structures (statements) have a “condition” that evaluates to either “True” or “False” if password == “secret”: print “You’re In!” Only prints if the condition is “True”

September 7, 2004ICP: Chapter 3: Control Structures9 Comparison Operators ==equal to !=not equal to >greater than <less than >=greater than or equal to <=less than or equal to

September 7, 2004ICP: Chapter 3: Control Structures10 Indentation Blocks Code statement blocks in if structures (any control structure) need to be indented –tabbed or spaced inside –improves readability –determines what is the “True” block from other code

September 7, 2004ICP: Chapter 3: Control Structures11 Using the if-else Structure Sometimes programs need to make a choice –if the condition is “True” execute something –if the condition is “False” execute something else if password == “secret”: do something else: do something else Example: Granted or Denied

September 7, 2004ICP: Chapter 3: Control Structures12 Using the if-elif-else Structure If the program needs to choose from more than two possibilities if x == 1: y = y+1 else: if x== 2: y = y+2 else: if x == 3: y = y+3 else: y = y-10

September 7, 2004ICP: Chapter 3: Control Structures13 Using the if-elif-else Structure If the program needs to choose from more than two possibilities…use the if-elif-else structure if x == 1: y = y+1 elif x == 2: y = y+2 elif x == 3: y = y+3 else: y = y-10

September 7, 2004ICP: Chapter 3: Control Structures14 Using the if-elif-else Structure Important! –Once a condition evaluates to true in an if-elif- else structure, the computer executes the corresponding block. –The other conditions are not evaluated

September 7, 2004ICP: Chapter 3: Control Structures15 Creating while Loops Repetition is another control structure –Also known as looping While hair is not clean: –Rinse, Lather, Repeat Need to make sure we can control and stop the looping structure

September 7, 2004ICP: Chapter 3: Control Structures16 Creating while Loops Python while loop is an example of a “sentry controlled loop” –Also known as a “pre-test” loop Example: Three Year Old Simulator

September 7, 2004ICP: Chapter 3: Control Structures17 Avoiding Infinite Loops An infinite loop is a loop whose exit condition remains true never terminates Example: counter = 0 while counter <= 10: print counter Often infinite loops are a form of logic error –but not always!

September 7, 2004ICP: Chapter 3: Control Structures18 Avoiding Infinite Loops Example: Losing Battle ProgramLosing Battle Program Program Trace –Table on right shows what happened –What is the logic error? –How do we to fix it? HealthTrollsDamageHealth!=0 1003True True -553True -863True

September 7, 2004ICP: Chapter 3: Control Structures19 Treating Values and Conditions All expressions have a conditional value –That is all expressions (numeric, string, boolean, etc) will evaluate to either True or False Basic rule is this… –False is anything “empty” or “zero” –True is anything else (“non-empty” or “not zero”)

September 7, 2004ICP: Chapter 3: Control Structures20 Treating Values as Conditions Example –0 is False –“” is False –2500 is True –“Hello Class” is True Example: Maitre D’

September 7, 2004ICP: Chapter 3: Control Structures21 Creating Intentional Infinite Loops Break – breaks out of a loop –Terminates the loop prior to the exit condition becoming false Continue – jump back to the top of the loop Example: Finicky Counter

September 7, 2004ICP: Chapter 3: Control Structures22 Using Compound Conditions So far, conditional expressions are considered to be simple –Only one conditional operator is used Often programs must check more than one conditional expression to make decisions or to control loops

September 7, 2004ICP: Chapter 3: Control Structures23 Using Compound Conditions Use and, or, and not to form compound conditions Example: Exclusive Network

September 7, 2004ICP: Chapter 3: Control Structures24 Using Compound Conditions NOT AND OR ANot A FalseTrue False ABA AND B False TrueFalse TrueFalse True ABA OR B False True FalseTrue

September 7, 2004ICP: Chapter 3: Control Structures25 Planning Your Programs It is very important to plan your programs before coding them –Decide on paper what your program should do –Outline the steps to achieve the purpose or algorithm of your program Pseudocode

September 7, 2004ICP: Chapter 3: Control Structures26 Planning Your Programs Flowchart symbols Process Start End Decision Input/ Output Connector Flow

September 7, 2004ICP: Chapter 3: Control Structures27 Planning Your Programs Start End Input guess guess == number Pick random number Congratulate Player Input guess

September 7, 2004ICP: Chapter 3: Control Structures28 Guess My Number - Again Flowchart for Guess My Number Example: Guess My Number