Bird
Raised Fist0
Pythonprogramming~10 mins

While loop 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 - While loop execution flow
Initialize variable
Check condition
Execute loop body
Update variable
Back to Check condition
The while loop starts by initializing a variable, then checks a condition. If true, it runs the loop body, updates the variable, and repeats. If false, it exits.
Execution Sample
Python
i = 0
while i < 3:
    print(i)
    i += 1
This code prints numbers 0, 1, and 2 by looping while i is less than 3.
Execution Table
Stepi valueCondition (i < 3)ActionOutput
10TruePrint 0, i = i + 10
21TruePrint 1, i = i + 11
32TruePrint 2, i = i + 12
43FalseExit loop
💡 i reaches 3, condition 3 < 3 is False, loop stops
Variable Tracker
VariableStartAfter 1After 2After 3Final
i01233
Key Moments - 2 Insights
Why does the loop stop when i is 3 even though the code inside the loop updates i?
The condition i < 3 is checked before each loop iteration. When i becomes 3, the condition is False, so the loop exits before running the body again (see execution_table step 4).
What happens if we forget to update i inside the loop?
If i is not updated, the condition i < 3 stays True forever, causing an infinite loop. The execution_table shows i changing each step to avoid this.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i at step 3?
A2
B3
C1
D0
💡 Hint
Check the 'i value' column in execution_table row 3.
At which step does the condition become false and the loop stops?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at the 'Condition' column in execution_table where it shows False.
If we change the condition to i < 5, how would the variable_tracker change?
Ai would stop at 3
Bi would go up to 5
Ci would stay at 0
Di would decrease
💡 Hint
Variable i increases each loop until condition is False; changing condition to i < 5 means i goes up to 5.
Concept Snapshot
while condition:
    # loop body

- Checks condition before each loop
- Runs body only if condition True
- Updates variables inside loop to avoid infinite loops
- Stops when condition becomes False
Full Transcript
This visual shows how a while loop works in Python. We start with a variable i set to 0. The loop checks if i is less than 3. If yes, it prints i and adds 1 to i. This repeats until i reaches 3. Then the condition is false and the loop stops. The execution table tracks each step, showing i's value, the condition check, the action taken, and output printed. The variable tracker shows how i changes after each loop. Key moments explain why the loop stops and what happens if we forget to update i. The quiz tests understanding of i's value at steps, when the loop ends, and effects of changing the condition.

Practice

(1/5)
1.

What does a while loop do in Python?

easy
A. Stops the program immediately
B. Runs code only once
C. Repeats code as long as a condition is true
D. Runs code a fixed number of times

Solution

  1. Step 1: Understand the purpose of a while loop

    A while loop runs repeatedly while its condition is true.
  2. Step 2: Compare options with this behavior

    Only Repeats code as long as a condition is true describes repeating code while a condition is true.
  3. Final Answer:

    Repeats code as long as a condition is true -> Option C
  4. Quick Check:

    While loop = repeat while true [OK]
Hint: While loops run repeatedly while condition is true [OK]
Common Mistakes:
  • Thinking while loops run a fixed number of times
  • Confusing while with if statement
  • Believing while loops run only once
2.

Which of the following is the correct syntax to start a while loop in Python?

?
easy
A. while x > 0:
B. while (x > 0)
C. while x > 0 {}
D. while x > 0 then

Solution

  1. Step 1: Recall Python while loop syntax

    Python uses a colon after the condition and no parentheses or braces.
  2. Step 2: Match options with correct syntax

    while x > 0: uses 'while x > 0:' which is correct syntax.
  3. Final Answer:

    while x > 0: -> Option A
  4. Quick Check:

    Colon ends while condition line [OK]
Hint: Use colon after condition, no braces or 'then' [OK]
Common Mistakes:
  • Adding braces {} like other languages
  • Using 'then' keyword
  • Putting condition in parentheses unnecessarily
3.

What will be the output of this code?

count = 3
while count > 0:
    print(count)
    count -= 1
print("Done")
medium
A. Done
B. 3 2 1 Done
C. 3 2 1 0 Done
D. 3 2 1 2 Done

Solution

  1. Step 1: Trace the while loop iterations

    count starts at 3, prints 3, then decreases to 2, prints 2, then 1, then stops when count is 0.
  2. Step 2: Note the final print after loop

    After loop ends, "Done" is printed.
  3. Final Answer:

    3 2 1 Done -> Option B
  4. Quick Check:

    Prints 3,2,1 then Done [OK]
Hint: Count decreases before loop ends, stops at zero [OK]
Common Mistakes:
  • Expecting 0 to print inside loop
  • Forgetting to decrease count
  • Thinking loop runs forever
4.

Find the error in this code snippet:

i = 1
while i < 5
    print(i)
    i += 1
medium
A. Missing colon after while condition
B. Variable i is not initialized
C. Indentation error in print statement
D. Infinite loop because i never changes

Solution

  1. Step 1: Check while loop syntax

    The while line must end with a colon (:).
  2. Step 2: Identify the missing colon

    The code misses ':' after 'while i < 5', causing syntax error.
  3. Final Answer:

    Missing colon after while condition -> Option A
  4. Quick Check:

    While line needs colon [OK]
Hint: Always put colon after while condition [OK]
Common Mistakes:
  • Forgetting colon after while
  • Misindenting inside loop
  • Not updating loop variable
5.

Consider this code:

n = 5
result = 1
while n > 1:
    result *= n
    n -= 1
print(result)

What does this code calculate?

hard
A. Sum of numbers from 1 to 5
B. Infinite loop with no output
C. Product of numbers from 1 to 4
D. Factorial of 5 (5!)

Solution

  1. Step 1: Understand the loop's multiplication

    result multiplies by n each time, starting at 5 down to 2.
  2. Step 2: Recognize factorial pattern

    Multiplying 5*4*3*2*1 equals 5 factorial (5!).
  3. Final Answer:

    Factorial of 5 (5!) -> Option D
  4. Quick Check:

    Multiplying down to 1 = factorial [OK]
Hint: Multiplying down from n to 1 calculates factorial [OK]
Common Mistakes:
  • Thinking it sums numbers instead of multiplies
  • Stopping multiplication at 2 instead of 1
  • Assuming infinite loop