Unit 2 Basic Python : Control Statements
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
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;
4
if Selection Structure
Fig. 3.3 if single-selection structure
flowchart.
print “Passed”Grade >= 60
true
false
5
if/else Structure
Fig. 3.4 if/else double-selection structure flowchart.
Grade >= 60
print “Passed”print “Failed”
false true
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
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
8
while Repetition Structure
true
false
Product = 2 *
product
Product <= 1000
Fig. 3.8 while repetition structure flowchart.

Unit2 control statements

  • 1.
    Unit 2 BasicPython : Control Statements
  • 2.
    control structures • 3control 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;
  • 4.
    4 if Selection Structure Fig.3.3 if single-selection structure flowchart. print “Passed”Grade >= 60 true false
  • 5.
    5 if/else Structure Fig. 3.4if/else double-selection structure flowchart. Grade >= 60 print “Passed”print “Failed” false true
  • 6.
    6 if/elif/else Selection Structure conditiona 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
  • 8.
    8 while Repetition Structure true false Product= 2 * product Product <= 1000 Fig. 3.8 while repetition structure flowchart.