Concept Flow - Why conditional statements are needed
Start
Check Condition?
No→Skip Action
Yes
Perform Action
End
The program checks a condition and decides to do something only if the condition is true, otherwise it skips that action.
Jump into concepts and practice - no test required
age = 18 if age >= 18: print("You can vote") else: print("You cannot vote")
| Step | Condition | Result | Branch Taken | Output |
|---|---|---|---|---|
| 1 | age >= 18 | True | If branch | You can vote |
| 2 | End of program | - | - | - |
| Variable | Start | After Step 1 | Final |
|---|---|---|---|
| age | 18 | 18 | 18 |
Conditional statements let programs choose actions based on conditions. Syntax: if condition: do something else: do something else They help programs make decisions like real-life choices.
if in Python?def.if statement in Python?if syntaxif syntax [OK]age = 18
if age >= 18:
print('Adult')
else:
print('Child')age >= 18if block runs and prints 'Adult'. The else block is skipped.number = 10
if number > 5
print('Big number')if statement syntaxif condition line.if number > 5, causing a syntax error.if condition [OK]if condition [OK]ifif, elif, and elseelif correctly and covers all hour ranges without overlap.if statements causing multiple prints; if hour < 12:
print('Good morning')
else if 12 <= hour <= 18:
print('Good afternoon')
else:
print('Good evening') uses invalid 'else if' syntax; if hour < 12:
print('Good morning')
elif hour > 12 and hour < 18:
print('Good afternoon')
else:
print('Good evening') misses equality for 12 and 18 in afternoon range.if, elif, and else and correct ranges -> Option Delif for multiple conditions [OK]elif for multiple exclusive conditions [OK]if causing multiple outputs