Concept Flow - If statement execution flow
Start
Evaluate condition
Execute if-block
End if-block
No
Skip if-block
End
The program checks the condition. If true, it runs the if-block. If false, it skips it and continues.
Jump into concepts and practice - no test required
x = 10 if x > 5: print("x is greater than 5") print("Done")
| Step | Action | Condition (x > 5) | Branch Taken | Output |
|---|---|---|---|---|
| 1 | Evaluate condition | 10 > 5 | Yes | |
| 2 | Execute if-block | x is greater than 5 | ||
| 3 | Execute next statement | Done | ||
| 4 | End | Program ends |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| x | 10 | 10 | 10 | 10 |
If statement syntax:
if condition:
# code runs if condition is true
Behavior:
- Condition checked first
- If true, run code inside if-block
- If false, skip if-block
Key rule: Only runs if condition is true.if statement do in Python?ifif statement checks a condition and runs code only if that condition is true.def.if runs code if condition true [OK]if statement in Python?if syntax: after the condition and no parentheses or 'then'.if x > 5: which is correct. Others use 'then' or braces which are not Python syntax.if ends with colon [OK]if ends with colon, no 'then' or braces [OK]age = 20
if age < 18:
print("Child")
elif age < 65:
print("Adult")
else:
print("Senior")ageage is 20.age < 18 is false (20 is not less than 18). Second condition age < 65 is true (20 is less than 65), so it prints "Adult" and skips the else.elif and jumping to elsescore = 75
if score >= 90
print("Excellent")
elif score >= 60:
print("Pass")
else:
print("Fail")if statementif line is missing a colon : at the end, which is required in Python.elif usage are correct. The variable score is defined.if needs a colon [OK]if, elif, and else to do this?if-elif-else structureif for first condition, elif for the second, and else for all other cases.elif and else. if num > 0:
print("Positive")
if num == 0:
print("Zero")
else:
print("Negative") uses two separate if statements which can cause multiple prints. if num > 0:
print("Positive")
else if num == 0:
print("Zero")
else:
print("Negative") uses invalid syntax else if. if num > 0:
print("Positive")
elif num < 0:
print("Zero")
else:
print("Negative")'s elif num < 0 will print "Zero" for negative numbers and "Negative" for zero incorrectly.