Conditional Logic with if Statements
An interactive input loop uses while True to run indefinitely and break to exit when the user provides a specific command.
The Stop Signal
Interactive programs often need to keep asking for input until the user gives a specific command. A useful Python pattern combines while True with an if statement and break. The loop keeps requesting input, the if statement checks whether the input is the exit command, and break ends the loop when that condition is true.
Following One Complete Cycle
The loop begins at while True. Python prompts the user and stores the typed text in line. The if statement then compares line with the exact command done. If the comparison is false, break is skipped, the entered text is printed, and control returns to the beginning of the loop. If the comparison is true, break immediately exits the loop, and execution continues with print("Done!").
Why while True Continues
while True creates the repetition part of the pattern. Because its condition is always true, reaching the bottom of the loop does not finish the loop. Instead, after a non-exit command is processed, execution returns to while True and the program asks for input again. Repetition stops only when execution reaches break.
while True does not decide when to stop. It only creates continued repetition. The if statement supplies the condition, and break supplies the exit action.
What break Does
break immediately exits the loop. Once Python encounters break, it does not execute the remaining code in that iteration. Execution continues with the code after the loop.
Tracing User Commands
Suppose the user enters apple, then banana, then done. The first two commands do not match the exit command, so each is printed and the loop starts another iteration. The third command matches exactly. Python executes break, skips the print(line) statement in that iteration, leaves the loop, and then prints Done!.
| Input | Condition result | Action | Next location |
|---|---|---|---|
| apple | false | Print apple | Beginning of loop |
| banana | false | Print banana | Beginning of loop |
| done | true | Execute break | Code after loop |
Execution trace for the input sequence apple, banana, done.
> apple
apple
> banana
banana
> done
Done!What do you think happens?
If the user enters done first, will the program print done before printing Done!?
Reveal answer
Answer: No, because break runs before print(line).
When line equals done, the if condition is true. break immediately exits the loop, so the print(line) statement is not executed for that iteration. The code after the loop then prints Done!.
Mistakes That Prevent Exit
Forgetting break
while True continues repeating because nothing tells it to leave.
Fix:
Place break in the branch that handles the intended exit condition.Using the wrong exit command
The condition is based on the command being checked. If the entered text does not match it, the loop treats the input as a non-exit command.
Fix:
Use the documented exit command exactly, or change the condition so it matches the command you intend to accept.Placing break outside the if statement
The loop exits without waiting for the specific command.
Fix:
Place break inside the conditional branch that represents the exit condition.
Tell the user how to stop the loop, test the exit command, and verify that ordinary input is processed before the next prompt appears.
Practice the Trace
Trace the input loop for the commands orange, done. For each command, decide whether line is printed, whether the loop repeats, and whether the code after the loop runs.
Hints
- Compare each command exactly with done.
- Remember that break skips the remaining code in the current iteration.
- The code after the loop runs only after break exits the loop.
Tracing orange, done
Determine the control flow for the input sequence orange, done.
Process orange: orange does not equal done, so break is skipped. The program prints orange and returns to the beginning of the loop.
Process done: done equals the exit command, so Python executes break. The current iteration ends immediately, and line is not printed.
Run code after the loop: After break exits the loop, the statement after the loop executes and prints Done!.
The program prints orange, then Done!, and does not print done.
Pattern in Practice
This input-loop pattern is useful whenever a program should respond repeatedly to user commands. The same structure can support interactive applications such as chat interfaces, calculators, and menu-driven programs: ask for input, check whether the user signaled to stop, process non-exit input, and repeat.
Key Takeaways
- while True creates a loop whose condition remains true, so repetition continues until an explicit exit occurs.
- An if statement checks whether the user's input matches the chosen exit command.
- break immediately leaves the loop and skips any remaining code in the current iteration.
- Non-exit input is processed, then control returns to the beginning of the loop.
- Reliable interactive loops include break, use the intended exit command, place break inside the correct conditional branch, and tell the user how to stop.