Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of."— Presentation transcript:

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

2 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

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

4 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

5 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

6 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

7 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

8 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”

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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!

18 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 713 423 133 -243True -553True -863True

19 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”)

20 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’

21 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

22 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

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

24 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

25 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

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

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

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


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

Similar presentations


Ads by Google