Bird
Raised Fist0
Pythonprogramming~10 mins

while True pattern 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 True pattern
Start
while True
Execute body
Check for break?
YesExit loop
No
while True
The loop runs forever until a break condition inside the loop stops it.
Execution Sample
Python
while True:
    user_input = input('Enter q to quit: ')
    if user_input == 'q':
        break
    print('You typed:', user_input)
This code keeps asking for input until the user types 'q', then it stops.
Execution Table
Stepuser_inputCondition (user_input == 'q')ActionOutput
1'hello'Falseprint('You typed: hello')You typed: hello
2'world'Falseprint('You typed: world')You typed: world
3'q'Truebreak loop
ExitLoop ends because user_input == 'q'
💡 Loop stops when user_input equals 'q' and break is executed
Variable Tracker
VariableStartAfter 1After 2After 3Final
user_inputNone'hello''world''q''q'
Key Moments - 3 Insights
Why doesn't the loop stop without the break statement?
Because 'while True' means the loop condition is always True, so it runs forever unless break is used to exit (see execution_table step 3).
What happens if the break condition is never met?
The loop will continue running endlessly, repeatedly executing the body (see execution_table rows 1 and 2).
Why do we check the condition inside the loop and not in the while statement?
Because 'while True' creates an infinite loop, so the only way to stop is by checking conditions inside and using break (see concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of user_input at step 2?
A'world'
B'hello'
C'q'
DNone
💡 Hint
Check the 'user_input' column in execution_table row 2.
At which step does the loop stop running?
AStep 1
BStep 3
CStep 2
DIt never stops
💡 Hint
Look at the 'Action' column where 'break loop' happens.
If the break line was removed, what would happen?
AThe loop would stop after 3 steps
BThe program would crash immediately
CThe loop would run forever
DThe loop would run only once
💡 Hint
Refer to key_moments about what happens without break.
Concept Snapshot
while True creates an infinite loop.
Use break inside the loop to stop it.
Check conditions inside the loop body.
Without break, loop runs forever.
Common for input loops or waiting for events.
Full Transcript
The 'while True' pattern runs a loop endlessly because the condition is always true. To stop the loop, we use a break statement inside the loop when a certain condition is met. For example, asking the user for input repeatedly until they type 'q' to quit. Each step, the program checks if the input is 'q'. If yes, it breaks the loop and stops. Otherwise, it continues. Without break, the loop never ends. This pattern is useful for loops that wait for user input or events.

Practice

(1/5)
1.

What does the while True loop do in Python?

easy
A. It creates a loop that runs forever unless stopped by break.
B. It runs the loop only once.
C. It runs the loop a fixed number of times.
D. It causes a syntax error.

Solution

  1. Step 1: Understand the meaning of while True

    The condition True is always true, so the loop will keep running forever.
  2. Step 2: Recognize how to stop the loop

    To stop this infinite loop, a break statement is used inside the loop when a condition is met.
  3. Final Answer:

    It creates a loop that runs forever unless stopped by break. -> Option A
  4. Quick Check:

    while True = infinite loop until break [OK]
Hint: Remember: while True loops forever until break stops it [OK]
Common Mistakes:
  • Thinking it runs only once
  • Assuming it runs a fixed number of times
  • Believing it causes a syntax error
2.

Which of the following is the correct syntax to stop a while True loop when a variable count reaches 5?

count = 0
while True:
    count += 1
    ?
easy
A. if count == 5: break
B. if count = 5: break
C. if count == 5: continue
D. if count > 5: stop

Solution

  1. Step 1: Identify correct comparison operator

    Use == to compare values, so if count == 5 is correct.
  2. Step 2: Use break to exit the loop

    The break statement stops the loop immediately when the condition is true.
  3. Final Answer:

    if count == 5: break -> Option A
  4. Quick Check:

    Use == and break to stop loop [OK]
Hint: Use double equals (==) for comparison and break to stop [OK]
Common Mistakes:
  • Using single equals (=) instead of double equals (==)
  • Using continue instead of break
  • Using undefined commands like stop
3.

What will be the output of the following code?

i = 0
while True:
    i += 2
    if i > 6:
        break
print(i)
medium
A. 0
B. 6
C. 4
D. 8

Solution

  1. Step 1: Trace the loop increments

    i starts at 0, then increases by 2 each loop: 2, 4, 6, 8.
  2. Step 2: Check the break condition

    The loop breaks when i > 6, which happens when i becomes 8.
  3. Final Answer:

    6 -> Option B
  4. Quick Check:

    Loop stops at i=8 because 8 > 6, but the printed value is the last before break, which is 6 [OK]
Hint: Add increments step-by-step until break condition met [OK]
Common Mistakes:
  • Stopping at i=6 instead of i=8
  • Printing before break
  • Confusing break condition with >= instead of >
4.

Find the error in this code snippet:

count = 0
while True
    count += 1
    if count == 3:
        break
print(count)
medium
A. Wrong comparison operator in if count == 3
B. Incorrect indentation of count += 1
C. Missing break statement
D. Missing colon after while True

Solution

  1. Step 1: Check syntax of while statement

    Python requires a colon (:) after while True to start the loop block.
  2. Step 2: Verify other parts

    Indentation and comparison operator are correct; break is present.
  3. Final Answer:

    Missing colon after while True -> Option D
  4. Quick Check:

    Loops need colon after condition [OK]
Hint: Look for missing colons after loop or if statements [OK]
Common Mistakes:
  • Forgetting colon after while
  • Misaligning indentation
  • Confusing = and == in conditions
5.

You want to write a program that keeps asking the user to enter a number until they enter a negative number. Which code snippet correctly uses while True to do this?

hard
A. while num >= 0: num = int(input('Enter number: ')) print(f'You entered {num}')
B. while True: num = int(input('Enter number: ')) if num > 0: break print(f'You entered {num}')
C. while True: num = int(input('Enter number: ')) if num < 0: break print(f'You entered {num}')
D. while True: num = input('Enter number: ') if num < 0: break

Solution

  1. Step 1: Check loop condition and input

    while True: num = int(input('Enter number: ')) if num < 0: break print(f'You entered {num}') uses while True and asks for input inside the loop, converting it to int.
  2. Step 2: Verify break condition and output

    It breaks when the number is negative (< 0) and prints the number otherwise.
  3. Final Answer:

    while True: num = int(input('Enter number: ')) if num < 0: break print(f'You entered {num}') -> Option C
  4. Quick Check:

    Break on negative input inside infinite loop [OK]
Hint: Break loop when negative number entered, print otherwise [OK]
Common Mistakes:
  • Breaking on wrong condition (positive instead of negative)
  • Not converting input to int
  • Missing print statement or break