Concept Flow - while True pattern
Start
while True
Execute body
Check for break?
Yes→Exit loop
No
while True
The loop runs forever until a break condition inside the loop stops it.
Jump into concepts and practice - no test required
while True: user_input = input('Enter q to quit: ') if user_input == 'q': break print('You typed:', user_input)
| Step | user_input | Condition (user_input == 'q') | Action | Output |
|---|---|---|---|---|
| 1 | 'hello' | False | print('You typed: hello') | You typed: hello |
| 2 | 'world' | False | print('You typed: world') | You typed: world |
| 3 | 'q' | True | break loop | |
| Exit | Loop ends because user_input == 'q' |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| user_input | None | 'hello' | 'world' | 'q' | 'q' |
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.
What does the while True loop do in Python?
while TrueTrue is always true, so the loop will keep running forever.break statement is used inside the loop when a condition is met.break. -> Option Awhile True = infinite loop until break [OK]while True loops forever until break stops it [OK]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
?== to compare values, so if count == 5 is correct.break to exit the loopbreak statement stops the loop immediately when the condition is true.== and break to stop loop [OK]What will be the output of the following code?
i = 0
while True:
i += 2
if i > 6:
break
print(i)Find the error in this code snippet:
count = 0
while True
count += 1
if count == 3:
break
print(count)while statementwhile True to start the loop block.break is present.while True -> Option DYou 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?
while True and asks for input inside the loop, converting it to int.