Conditional Execution
Obaidullah Zaland
x=5
Conditional Steps
Ye Program:
X < 10 ? s
x=5
No print 'Smaller' if x < 10: Output:
print
Ye 'Smaller’ Smaller
X > 20 ? s Finis
if x > 20:
No print 'Bigger' print
'Bigger'
print 'Finis'
print 'Finis'
Comparison Operators
• Boolean expressions ask a
question and produce a Yes or
Python Meaning
No result which we use to
< Less than
control program flow
<= Less than or Equal
• Boolean expressions using == Equal to
comparison operators Greater than or
>=
evaluate to - True / False - Equal
Yes / No > Greater than
!= Not equal
• Comparison operators look at Remember: “=” is used for assignment.
variables but do not change
the variables
[Link]
Comparison Operators
x = 5
if x == 5 :
print 'Equals 5' Equals 5
if x > 4 :
print 'Greater than 4'
Greater than 4
if x >= 5 : Greater than or Equals
print 'Greater than or Equals 5' 5
if x < 6 : print 'Less than 6'
if x <= 5 : Less than 6
print 'Less than or Equals 5' Less than or Equals 5
if x != 6 :
Not equal 6
print 'Not equal 6'
One-Way Decisions Ye
x = 5 X == 5 s
print 'Before 5' Before 5 ?
if x == 5 : Is 5 No print 'Is 5'
print 'Is 5' Is Still 5
print 'Is Still 5' Third 5
print 'Third 5' print 'Still 5'
Afterwards
print 'Afterwards 5’
print 'Before 6’ 5 print 'Third 5'
if x == 6 : Before 6
print 'Is 6' Afterwards
print 'Is Still 6' 6
print 'Third 6'
print 'Afterwards 6'
Indentation
• Increase indent indent after an if statement or for statement (after : )
• Maintain indent to indicate the scope of the block (which lines are
affected by the if/for)
• Reduce indent back to the level of the if statement or for statement to
indicate the end of the block
• Blank lines are ignored - they do not affect indentation
• Comments on a line by themselves are ignored with regard to
indentation
increase / maintain after if or for
decrease to indicate end of block
x = 5
if x > 2 :
print 'Bigger than 2'
print 'Still bigger'
print 'Done with 2'
for i in range(5) :
print i
if i > 2 :
print 'Bigger than 2'
print 'Done with i', i
print 'All Done'
Think about begin/end blocks
x = 5
if x > 2 :
print 'Bigger than 2'
print 'Still bigger'
print 'Done with 2'
for i in range(5) :
print i
if i > 2 :
print 'Bigger than 2'
print 'Done with i', i
print 'All Done'
yes
Nested x>1
Decisions no print 'More than one'
x = 42
if x > 1 : yes
print 'More than one' x < 100
if x < 100 :
print 'Less than 100' no
print 'Less than 100'
print 'All done'
print 'All Done'
Two-way
Decisions X=4
• Sometimes we want to
do one thing if a no yes
x>2
logical expression is
true and something
else if the expression print 'Not bigger' print 'Bigger'
is false
• It is like a fork in the
road - we must choose
one or the other path print 'All Done'
but not both
Two-way
x=4
using else :
no yes
x>2
x = 4
if x > 2 : print 'Smaller' print 'Bigger'
print 'Bigger'
else :
print 'Smaller'
print 'All done'
print 'All Done'
Two-way
x=4
using else :
no yes
x>2
x = 4
if x > 2 : print 'Smaller' print 'Bigger'
print 'Bigger'
else :
print 'Smaller'
print 'All done'
print 'All Done'
Multi-way
yes
x<2 print 'small'
no
if x < 2 :
yes
print 'small'
elif x < 10 : x < 10 print 'Medium'
print 'Medium' no
else :
print 'LARGE' print 'LARGE'
print 'All done'
print 'All Done'
Multi-way x=0
yes
x<2 print 'small'
x = 0 no
if x < 2 :
yes
print 'small'
elif x < 10 : x < 10 print 'Medium'
print 'Medium' no
else :
print 'LARGE' print 'LARGE'
print 'All done'
print 'All Done'
Multi-way x=5
yes
x<2 print 'small'
x = 5 no
if x < 2 :
print 'small'
yes
elif x < 10 : x < 10 print 'Medium'
print 'Medium' no
else :
print 'LARGE' print 'LARGE'
print 'All done'
print 'All Done'
Multi-way x = 20
yes
x<2 print 'small'
x = 20 no
if x < 2 : yes
print 'small' x < 10 print 'Medium'
elif x < 10 :
print 'Medium' no
else :
print 'LARGE' print 'LARGE'
print 'All done'
print 'All Done'
Multi-way
if x < 2 :
print 'Small'
# No Else elif x < 10 :
x = 5 print 'Medium'
if x < 2 : elif x < 20 :
print 'Small' print 'Big'
elif x < 10 : elif x < 40 :
print 'Medium' print 'Large'
elif x < 100:
print 'All done' print 'Huge'
else :
print 'Ginormous'
Multi-way Puzzles
Which will never
print?
if x < 2 :
print 'Below 2'
if x < 2 : elif x < 20 :
print 'Below 2' print 'Below 20'
elif x >= 2 : elif x < 10 :
print 'Two or more' print 'Below 10'
else : else :
print 'Something else' print 'Something else'
Summary
• Comparison operators • Nested Decisions
== <= >= > < ! =
• Multi-way decisions using
• Logical operators: and or elif
not
• Indentation
• One-way Decisions
• Two-way decisions:
if: and else: