0
0
Pythonprogramming~10 mins

Nested while loops in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested while loops
Initialize outer variable i=0
Check outer condition i < 2?
NoEXIT
Yes
Initialize inner variable j=0
Check inner condition j < 3?
NoIncrement i
Yes
Execute inner loop body
Increment j
Back to inner condition
Increment i
Back to inner condition
The outer loop runs while i < 2. For each i, the inner loop runs while j < 3. Inner loop resets j each time.
Execution Sample
Python
i = 0
while i < 2:
    j = 0
    while j < 3:
        print(i, j)
        j += 1
    i += 1
Print pairs of i and j where i goes 0 to 1 and j goes 0 to 2 for each i.
Execution Table
StepijOuter condition (i<2)Inner condition (j<3)ActionOutput
10-True-Initialize j=0
200TrueTruePrint (0,0), j+=1(0, 0)
301TrueTruePrint (0,1), j+=1(0, 1)
402TrueTruePrint (0,2), j+=1(0, 2)
503TrueFalseInner loop ends, i+=1
61-True-Initialize j=0
710TrueTruePrint (1,0), j+=1(1, 0)
811TrueTruePrint (1,1), j+=1(1, 1)
912TrueTruePrint (1,2), j+=1(1, 2)
1013TrueFalseInner loop ends, i+=1
112-False-Outer loop ends
💡 At step 11, i=2, outer condition i<2 is False, so loop ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10Final
i000001111122
j-0123-0123--
Key Moments - 3 Insights
Why does j reset to 0 each time the outer loop runs?
Because at each outer loop iteration (see steps 1 and 6), j is set to 0 before the inner loop starts. This is shown in the execution_table where j is '-' before initialization and then 0 after.
When does the inner loop stop running?
The inner loop stops when j reaches 3, making the condition j < 3 false (see steps 5 and 10). Then control goes back to the outer loop.
Why does the outer loop run only twice?
Because the outer loop condition is i < 2. When i becomes 2 at step 11, the condition is false and the loop ends.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What are the values of i and j?
Ai=0, j=2
Bi=1, j=2
Ci=0, j=3
Di=1, j=3
💡 Hint
Check the 'i' and 'j' columns in execution_table row with Step 4.
At which step does the outer loop condition become false?
AStep 10
BStep 11
CStep 5
DStep 6
💡 Hint
Look at the 'Outer condition' column in execution_table and find where it is False.
If we change the outer loop condition to i < 3, how many times will the inner loop run in total?
A6 times
B3 times
C9 times
D2 times
💡 Hint
Each outer loop runs inner loop 3 times; with i < 3, outer runs 3 times, so 3*3=9.
Concept Snapshot
Nested while loops:
- Outer loop runs while condition true
- Inner loop runs fully for each outer iteration
- Inner loop variable resets each outer iteration
- Use indentation to show nesting
- Loop ends when outer condition false
Full Transcript
This example shows nested while loops in Python. The outer loop variable i starts at 0 and runs while i < 2. For each i, the inner loop variable j starts at 0 and runs while j < 3. Each inner loop prints the pair (i, j). After inner loop ends, i increments. The loops stop when i reaches 2. Variables i and j change as shown in the variable tracker. Key points: j resets each outer loop, inner loop runs fully each time, outer loop runs twice total.