Bird
Raised Fist0
Pythonprogramming~10 mins

Counter-based while loop 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 - Counter-based while loop
Initialize counter = 0
Check: counter < limit?
NoEXIT
Yes
Execute loop body
Update counter (e.g., counter += 1)
Back to Check
Start with a counter, check a condition before each loop, run the loop body if true, then update the counter and repeat until the condition is false.
Execution Sample
Python
counter = 0
while counter < 3:
    print(counter)
    counter += 1
Prints numbers 0, 1, 2 by increasing counter until it reaches 3.
Execution Table
StepcounterCondition (counter < 3)ActionOutput
10TruePrint 0, counter = 10
21TruePrint 1, counter = 21
32TruePrint 2, counter = 32
43FalseExit loop
💡 counter reaches 3, condition 3 < 3 is False, loop stops
Variable Tracker
VariableStartAfter 1After 2After 3Final
counter01233
Key Moments - 3 Insights
Why does the loop stop when counter equals 3?
Because the condition counter < 3 becomes False at step 4 (see execution_table row 4), so the loop exits.
Why do we update the counter inside the loop?
Updating counter (counter += 1) inside the loop changes its value each time, eventually making the condition False to stop the loop (see execution_table actions).
What would happen if we forget to update the counter?
The condition would always be True, causing an infinite loop because counter never changes (no progress in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of counter at Step 3?
A1
B3
C2
D0
💡 Hint
Check the 'counter' column in execution_table row 3.
At which step does the loop condition become False?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the 'Condition' column in execution_table to find when it is False.
If we change the condition to counter < 5, how many times will the loop run?
A5 times
B3 times
C4 times
D6 times
💡 Hint
The loop runs while counter is less than the limit; see variable_tracker for how counter changes.
Concept Snapshot
Counter-based while loop syntax:
counter = start_value
while counter < limit:
    # loop body
    counter += 1

Loop runs while condition is True.
Update counter inside loop to avoid infinite loop.
Full Transcript
This visual trace shows how a counter-based while loop works in Python. We start by setting counter to zero. Before each loop run, we check if counter is less than 3. If yes, we print the counter and increase it by one. This repeats until counter reaches 3, when the condition becomes False and the loop stops. The execution table tracks each step, showing counter values, condition checks, actions, and outputs. The variable tracker shows how counter changes from 0 to 3. Key moments explain why the loop stops, why updating counter is important, and what happens if we forget to update it. The quiz questions help check understanding by asking about counter values at specific steps, when the loop ends, and how changing the condition affects loop runs.

Practice

(1/5)
1.

What is the main purpose of a counter in a while loop?

easy
A. To keep track of how many times the loop has run
B. To store the final result of the loop
C. To stop the program immediately
D. To print messages inside the loop

Solution

  1. Step 1: Understand the role of a counter

    A counter is a variable that counts how many times the loop runs.
  2. Step 2: Identify the purpose in a while loop

    The counter helps the loop know when to stop by checking its value in the condition.
  3. Final Answer:

    To keep track of how many times the loop has run -> Option A
  4. Quick Check:

    Counter tracks loop runs = D [OK]
Hint: Counter counts loop repeats to control stopping [OK]
Common Mistakes:
  • Thinking counter stores final result
  • Confusing counter with printing inside loop
  • Believing counter stops program immediately
2.

Which of the following is the correct syntax to start a counter at 0 before a while loop?

?
easy
A. counter := 0
B. while counter = 0:
C. counter == 0
D. counter = 0

Solution

  1. Step 1: Identify correct variable assignment

    To start a counter, assign 0 using a single equal sign: counter = 0.
  2. Step 2: Check other options for syntax errors

    while counter = 0: uses assignment in while condition (invalid), C uses comparison, D uses walrus operator incorrectly here.
  3. Final Answer:

    counter = 0 -> Option D
  4. Quick Check:

    Assign counter with = 0 before loop = A [OK]
Hint: Use single = to assign counter before loop [OK]
Common Mistakes:
  • Using == instead of = for assignment
  • Trying to assign inside while condition
  • Confusing walrus operator usage
3.

What will be the output of this code?

counter = 1
while counter <= 3:
    print(counter)
    counter += 1
medium
A. 1 2 3
B. 1 2 3 4
C. 0 1 2 3
D. 3 2 1

Solution

  1. Step 1: Trace the loop iterations

    Counter starts at 1, prints 1, then increments to 2, prints 2, increments to 3, prints 3, then increments to 4.
  2. Step 2: Check loop condition

    Loop stops when counter is 4 because 4 <= 3 is false. So printed numbers are 1, 2, 3.
  3. Final Answer:

    1 2 3 -> Option A
  4. Quick Check:

    Prints 1 to 3 with counter increment = C [OK]
Hint: Loop runs while counter ≤ 3, prints 1,2,3 [OK]
Common Mistakes:
  • Including 4 in output
  • Starting count from 0 instead of 1
  • Printing numbers in reverse
4.

Find the error in this code and choose the fix:

counter = 0
while counter < 5
    print(counter)
    counter += 1
medium
A. Change < to <= in the while condition
B. Initialize counter to 1 instead of 0
C. Add a colon ':' after the while condition
D. Remove the increment line inside the loop

Solution

  1. Step 1: Identify syntax error in while loop

    The while statement is missing a colon ':' at the end of the condition line.
  2. Step 2: Check other options

    Changing < to <= is not an error fix, initializing counter differently or removing increment causes logic errors.
  3. Final Answer:

    Add a colon ':' after the while condition -> Option C
  4. Quick Check:

    Missing colon causes syntax error = B [OK]
Hint: Always put ':' after while condition [OK]
Common Mistakes:
  • Forgetting colon after while condition
  • Changing loop logic instead of fixing syntax
  • Removing counter increment causing infinite loop
5.

Write a counter-based while loop that prints only even numbers from 2 to 10 inclusive.

counter = 2
while counter <= 10:
    print(counter)
    ?
What should replace ? to correctly increment the counter?

hard
A. counter += 1
B. counter += 2
C. counter = counter * 2
D. counter -= 2

Solution

  1. Step 1: Understand the goal

    We want to print even numbers from 2 to 10, so counter should increase by 2 each time.
  2. Step 2: Choose correct increment

    Using counter += 2 moves counter from 2 to 4, 6, 8, 10 correctly.
  3. Final Answer:

    counter += 2 -> Option B
  4. Quick Check:

    Increment by 2 to print evens = A [OK]
Hint: Increase counter by 2 to get even numbers [OK]
Common Mistakes:
  • Incrementing by 1 prints odd numbers too
  • Multiplying counter causes wrong jumps
  • Decrementing counter causes infinite loop