0
0
Pythonprogramming~10 mins

If–else execution flow in Python - Step-by-Step Execution

Choose your learning style9 modes available
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.