2. 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
3. 3
Sequence Control Structure
Fig. 3.1 Sequence structure flowchart with pseudo code.
add grade to
total
add 1 to counter
total = total + grade;
counter = counter + 1;
6. 6
if/elif/else Selection Structure
condition a
true
false
.
.
.
false
false
condition z
default action(s)
true
true
condition b
case a action(s)
case b action(s)
case z action(s)
if statement
first elif
statement
last
elif
statemen
t else
stateme
nt
Fig. 3.5 if/elif/else multiple-selection structure.
7. 7
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