Académique Documents
Professionnel Documents
Culture Documents
control structures
• 3 control structures
• Sequential structure
• Built into Python
• Selection structure
• The if statement
• The if/else statement
• The if/elif/else statement
• Repetition structure
• The while repetition structure
• The for repetition structure
Sequence Control Structure
3
if Selection Structure
true
Grade >= 60 print “Passed”
false
false true
Grade >= 60
5
if/elif/else Selection Structure
true
if statement condition a case a action(s)
false
true
first elif condition b case b action(s)
statement false
.
.
.
last true
condition z case z action(s)
elif
false
statemen
t else default action(s)
stateme
nt
Fig. 3.5 if/elif/else multiple-selection structure.
6
while Repetition Structure
• Repetition Structures
• Allow a program to repeat an action while a condition is true
• Using while Repetition
• Action(s) contained within the body of the loop
• Condition should evaluate to false at some point
• Otherwise infinite loop and program hangs
7
while Repetition Structure
true
Product <= 1000 Product = 2 *
product
false