Bird
Raised Fist0
Pythonprogramming~10 mins

If statement execution flow in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.
Execution Sample
Python
x = 10
if x > 5:
    print("x is greater than 5")
print("Done")
Checks if x is greater than 5, prints a message if true, then prints 'Done'.
Execution Table
StepActionCondition (x > 5)Branch TakenOutput
1Evaluate condition10 > 5Yes
2Execute if-blockx is greater than 5
3Execute next statementDone
4EndProgram ends
💡 Condition true at step 1, if-block executed; program ends after printing all outputs.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
x10101010
Key Moments - 2 Insights
Why does the program print 'x is greater than 5'?
Because at step 1 in the execution table, the condition 'x > 5' is true, so the if-block runs and prints the message.
What happens if the condition is false?
The program skips the if-block (see branch 'No' in concept flow) and directly executes the next statement after the if-block.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 2?
A"x is greater than 5"
B"Done"
CNo output
DError
💡 Hint
Check the 'Output' column at step 2 in the execution_table.
At which step does the program decide to skip the if-block if the condition was false?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Condition' and 'Branch Taken' columns at step 1 in the execution_table.
If x was 3 instead of 10, what would be the output at step 3?
A"x is greater than 5"
BNo output
C"Done"
DError
💡 Hint
Refer to variable_tracker and execution_table to see how condition affects output.
Concept Snapshot
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.
Full Transcript
This visual execution shows how an if statement works in Python. The program starts by evaluating the condition. If the condition is true, it runs the code inside the if-block. If false, it skips that code and continues. In the example, x is 10, so the condition 'x > 5' is true. The program prints 'x is greater than 5' and then prints 'Done'. Variables like x keep their values throughout. If x was less or equal to 5, the if-block would be skipped and only 'Done' would print. This helps beginners see exactly how the if statement controls the flow of the program step-by-step.

Practice

(1/5)
1. What does an if statement do in Python?
easy
A. It stores data in a list.
B. It repeats code multiple times.
C. It runs code only if a condition is true.
D. It defines a new function.

Solution

  1. Step 1: Understand the purpose of if

    An if statement checks a condition and runs code only if that condition is true.
  2. Step 2: Compare with other options

    Repeating code is done by loops, storing data is done by lists, and defining functions uses def.
  3. Final Answer:

    It runs code only if a condition is true. -> Option C
  4. Quick Check:

    if runs code if condition true [OK]
Hint: If checks condition; runs code only when true [OK]
Common Mistakes:
  • Confusing if with loops
  • Thinking if stores data
  • Mixing if with function definition
2. Which of the following is the correct syntax for an if statement in Python?
easy
A. if x > 5 then:
B. if x > 5:
C. if (x > 5) {}
D. if x > 5 then

Solution

  1. Step 1: Recall Python if syntax

    Python uses a colon : after the condition and no parentheses or 'then'.
  2. Step 2: Check each option

    if x > 5: uses if x > 5: which is correct. Others use 'then' or braces which are not Python syntax.
  3. Final Answer:

    if x > 5: -> Option B
  4. Quick Check:

    Python if ends with colon [OK]
Hint: Python if ends with colon, no 'then' or braces [OK]
Common Mistakes:
  • Adding 'then' after condition
  • Using braces {} like other languages
  • Forgetting the colon at the end
3. What will be the output of this code?
age = 20
if age < 18:
    print("Child")
elif age < 65:
    print("Adult")
else:
    print("Senior")
medium
A. Adult
B. Senior
C. Child
D. No output

Solution

  1. Step 1: Check the value of age

    The variable age is 20.
  2. Step 2: Evaluate conditions in order

    First condition 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.
  3. Final Answer:

    Adult -> Option A
  4. Quick Check:

    20 is less than 65, prints Adult [OK]
Hint: Check conditions top to bottom; first true runs [OK]
Common Mistakes:
  • Printing Child for age 20
  • Ignoring elif and jumping to else
  • Thinking no output if first condition false
4. Find the error in this code:
score = 75
if score >= 90
    print("Excellent")
elif score >= 60:
    print("Pass")
else:
    print("Fail")
medium
A. score variable not defined
B. Wrong indentation on print statements
C. Using elif instead of else if
D. Missing colon after first if condition

Solution

  1. Step 1: Check syntax of if statement

    The first if line is missing a colon : at the end, which is required in Python.
  2. Step 2: Verify other parts

    Indentation and elif usage are correct. The variable score is defined.
  3. Final Answer:

    Missing colon after first if condition -> Option D
  4. Quick Check:

    Every if needs a colon [OK]
Hint: Check colons after all if/elif/else lines [OK]
Common Mistakes:
  • Forgetting colon after if condition
  • Confusing elif with else if syntax
  • Incorrect indentation of print lines
5. You want to print "Positive", "Zero", or "Negative" based on a number's value. Which code correctly uses if, elif, and else to do this?
hard
A. if num > 0: print("Positive") elif num == 0: print("Zero") else: print("Negative")
B. if num > 0: print("Positive") if num == 0: print("Zero") else: print("Negative")
C. if num > 0: print("Positive") else if num == 0: print("Zero") else: print("Negative")
D. if num > 0: print("Positive") elif num < 0: print("Zero") else: print("Negative")

Solution

  1. Step 1: Understand correct if-elif-else structure

    Use if for first condition, elif for the second, and else for all other cases.
  2. Step 2: Check each option

    if num > 0: print("Positive") elif num == 0: print("Zero") else: print("Negative") correctly uses 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.
  3. Final Answer:

    if num > 0: print("Positive") elif num == 0: print("Zero") else: print("Negative") -> Option A
  4. Quick Check:

    Use if, elif, else for exclusive conditions [OK]
Hint: Use if, elif, else for clear exclusive choices [OK]
Common Mistakes:
  • Using multiple separate ifs causing multiple outputs
  • Writing else if instead of elif
  • Overlapping conditions causing wrong output