Professional Documents
Culture Documents
Output:
false
Score true
< 40
start
get score
false true
score >= 40
Display Display
‘fail’ ‘pass’
stop
if..else
statement -Two alternatives (cont.)
SYNTAX: if condition:
statementT
else:
statementF
statementT if condition else statementF
EX: if score >= 40:
print(‘pass’)
else:
print(‘fail’)
print(‘pass’) if score>=40 else print(‘fail’)
if..elif..else
Multiple-Alternative Decisions
start
get score
stop
if..elif..else
Multiple-Alternative Decisions (cont.)
SYNTAX: if condition:
statementT
elif condition:
statementT
else:
statementF
get x
true Display
x > 10
‘Above ten’
false true
x > 20 Display ‘and
also above 20!’
false
Display ‘but
not above 20’
stop
Nested if..
Multiple-Alternative Decisions
• You can have if statements inside if statements, this is
called nested if statements.
SYNTAX: if condition:
statementT
if condition:
statementT
else:
statementF
EX: if x >= 10:
print(‘Above ten,’)
if x >= 40:
print(‘and also above 20!’)
else:
print(‘but not above 20.’)
Exercise
• Create an analysis, pseudocode, flowchart and
python program for checking whether a
number is divisible by 2 and 3, by 2 or 3, and
by 2 or 3 but not both. Display appropriate
message for each checking.
Solution
• Analysis
Input Process Output
1) Start
2) Prompt and get number
3) If (number % 2 ==0) and (number%3==0)
display (number, “is divisible by 2 and 3”)
4) If (number %2==0)or(number %3==0)
display (number, “is divisible by 2 or 3”)
5) If((number%2==0 )or (number%3==0)) and not((number%2==0)and(number%3==0))
display (number, “is divisible by 2 or 3,but not both”)
6) End
Solution(cont.)
• Flowchart
start
If ((number%2==0)
If
or(number%3==0)) f
(number%2==0) f and not
((number%2==0)
and
and(number%3==0))
(number%3==0)
If f
(number%2==0)
t or t
display (number, (number%3==0)
display (number,
“is divisible by 2 “is divisible by 2
and 3”) or 3,but not
t
both”)
display (number,
“is divisible by 2
or 3”)
end
Solution(cont.)