Bird
Raised Fist0
Pythonprogramming~10 mins

If–else 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–else execution flow
Start
Evaluate Condition
Execute If
End
The program checks a condition. If true, it runs the 'if' block; if false, it runs the 'else' block, then continues.
Execution Sample
Python
x = 10
if x > 5:
    print("x is greater than 5")
else:
    print("x is 5 or less")
This code checks if x is greater than 5 and prints a message based on the result.
Execution Table
StepCondition (x > 5)ResultBranch TakenOutput
110 > 5TrueIf block"x is greater than 5" printed
2--EndExecution stops after if block
💡 Condition is True, so else block is skipped and execution ends after if block.
Variable Tracker
VariableStartAfter Step 1Final
xundefined1010
Key Moments - 2 Insights
Why does the else block not run when the condition is true?
Because the condition evaluated to True at Step 1, the program runs only the if block and skips the else block, as shown in the execution_table.
What happens if the condition is false?
If the condition were false, the program would run the else block instead of the if block, as the flow diagram shows the path for False leading to else.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the condition result at Step 1?
AFalse
BTrue
CUndefined
DError
💡 Hint
Check the 'Condition (x > 5)' column in the first row of the execution_table.
At which step does the program decide which branch to take?
AStep 2
BBefore Step 1
CStep 1
DAfter Step 2
💡 Hint
Look at the 'Step' column and see where the condition is evaluated in the execution_table.
If x was 3 instead of 10, which branch would be taken according to the flow?
AElse block
BIf block
CBoth blocks
DNo block
💡 Hint
Refer to the concept_flow diagram and imagine the condition x > 5 is False.
Concept Snapshot
if condition:
    # run this if True
else:
    # run this if False

The program checks the condition once.
Runs only one block based on True/False.
Then continues after the if-else.
Full Transcript
This visual execution shows how an if-else statement works in Python. The program starts and evaluates the condition x > 5. Since x is 10, the condition is True. It runs the if block and prints 'x is greater than 5'. The else block is skipped. The variable x starts undefined and is set to 10 before the condition check. The flow diagram shows the decision path clearly: if True, run if block; if False, run else block. The execution table traces each step with condition, branch taken, and output. Key moments clarify why only one block runs and what happens if the condition changes. The quiz tests understanding of condition evaluation, branch decision, and alternative paths. This helps beginners see exactly how if-else controls program flow step-by-step.

Practice

(1/5)
1. What does an if-else statement do in Python?
easy
A. It chooses between two paths based on a condition.
B. It repeats code multiple times.
C. It defines a function.
D. It creates a list.

Solution

  1. Step 1: Understand the purpose of if-else

    An if-else statement lets the program decide which code to run based on a condition being true or false.
  2. Step 2: Compare with other options

    Repeating code is done by loops, functions define reusable code blocks, and lists store multiple items, so these are not correct.
  3. Final Answer:

    It chooses between two paths based on a condition. -> Option A
  4. Quick Check:

    If-else = choose path [OK]
Hint: If-else picks one of two paths based on condition [OK]
Common Mistakes:
  • Confusing if-else with loops
  • Thinking if-else creates data structures
  • Mixing if-else with function definitions
2. Which of the following is the correct syntax for an if-else statement in Python?
easy
A. if x > 0: print('Positive') else: print('Non-positive')
B. if x > 0 then print('Positive') else print('Non-positive')
C. if (x > 0) { print('Positive'); } else { print('Non-positive'); }
D. if x > 0 print('Positive') else print('Non-positive')

Solution

  1. Step 1: Recall Python if-else syntax

    Python uses a colon after the condition and indentation for the code blocks.
  2. Step 2: Check each option

    if x > 0: print('Positive') else: print('Non-positive') uses colons and indentation correctly. if x > 0 then print('Positive') else print('Non-positive') uses 'then' which is not Python syntax. if (x > 0) { print('Positive'); } else { print('Non-positive'); } uses braces and semicolons, which are for other languages. if x > 0 print('Positive') else print('Non-positive') misses colons and indentation.
  3. Final Answer:

    if x > 0: print('Positive') else: print('Non-positive') -> Option A
  4. Quick Check:

    Colon + indent = correct if-else [OK]
Hint: Remember colons and indentation for if-else in Python [OK]
Common Mistakes:
  • Using 'then' keyword
  • Forgetting colons after if and else
  • Not indenting code blocks
3. What will be the output of this code?
age = 18
if age >= 18:
    print('Adult')
else:
    print('Minor')
medium
A. Minor
B. Adult
C. 18
D. No output

Solution

  1. Step 1: Evaluate the condition age >= 18

    Since age is 18, the condition age >= 18 is true.
  2. Step 2: Determine which block runs

    Because the condition is true, the code inside the if block runs, printing 'Adult'.
  3. Final Answer:

    Adult -> Option B
  4. Quick Check:

    Condition true -> print 'Adult' [OK]
Hint: Check if condition is true or false to pick output [OK]
Common Mistakes:
  • Assuming >= means less than
  • Printing else block by mistake
  • Confusing output with variable value
4. Find the error in this code:
num = 5
if num > 0
    print('Positive')
else:
    print('Non-positive')
medium
A. Variable 'num' is not defined
B. Wrong indentation on print statements
C. Missing colon ':' after 'if num > 0' line
D. 'else' should be 'elif' here

Solution

  1. Step 1: Check syntax of if statement

    The if statement must end with a colon ':' to mark the start of the block.
  2. Step 2: Identify the missing colon

    The code misses the colon after 'if num > 0', causing a syntax error.
  3. Final Answer:

    Missing colon ':' after 'if num > 0' line -> Option C
  4. Quick Check:

    Colon needed after if condition [OK]
Hint: Always put colon after if condition [OK]
Common Mistakes:
  • Forgetting colon after if
  • Confusing else with elif
  • Incorrect indentation
5. You want to write a program that prints 'Even' if a number is even, and 'Odd' if it is odd. Which code correctly uses if-else to do this?
hard
A. num = 4 if num % 2: print('Even') else: print('Odd')
B. num = 4 if num / 2 == 0: print('Even') else: print('Odd')
C. num = 4 if num % 2 != 0: print('Even') else: print('Odd')
D. num = 4 if num % 2 == 0: print('Even') else: print('Odd')

Solution

  1. Step 1: Understand how to check even numbers

    A number is even if dividing by 2 leaves no remainder, so num % 2 == 0 is true for even numbers.
  2. Step 2: Check each option's condition

    num = 4 if num % 2 == 0: print('Even') else: print('Odd') correctly uses num % 2 == 0. num = 4 if num / 2 == 0: print('Even') else: print('Odd') uses division instead of modulo, which is wrong. num = 4 if num % 2: print('Even') else: print('Odd') treats nonzero remainder as even, which is incorrect. num = 4 if num % 2 != 0: print('Even') else: print('Odd') reverses the logic.
  3. Final Answer:

    num = 4 if num % 2 == 0: print('Even') else: print('Odd') -> Option D
  4. Quick Check:

    Modulo equals zero means even [OK]
Hint: Use 'num % 2 == 0' to check even numbers [OK]
Common Mistakes:
  • Using division instead of modulo
  • Reversing even and odd logic
  • Checking truthiness of modulo without comparison