Concepts / Understanding Boolean Values and Conditions

Understanding Boolean Values and Conditions

The central cycle is condition, decision, loop body, and condition again. The cycle stops when the condition becomes false.

  • Programming

Repetition Controlled by a Condition

A while loop repeats a block of code as long as its condition is true. Instead of writing the same instructions several times, you write the instructions once inside the loop body. Python evaluates the condition before each iteration and uses that Boolean result to decide whether the body should run.

Key point

The central cycle is condition, decision, loop body, and condition again. The cycle stops when the condition becomes false.

Tracing Each Loop Cycle

ConditiontrueLoop bodyruns onceConditioncheck againLoop bodyruns again if trueConditioneventual checktruereturntruereturn
What happens next each time the condition is checked?

Every iteration follows the same order. Python evaluates the condition first. If the result is true, Python enters the loop body. After the body finishes, execution returns to the condition. The body does not run before the first condition check, and it does not run after a condition check produces false.

Worked example

Tracing a Guessing Game

A guessing game keeps running until the user guesses the number 23. The guesses are 20, then 25, then 23.

  1. 1
    First check

    The running condition starts as true, so the loop body executes and the user makes the first guess, 20.

  2. 2
    First result

    The guess is not 23, so the game remains active. Execution returns to check the condition again.

  3. 3
    Second check

    The condition is still true, so the body executes again and the user guesses 25.

  4. 4
    Second result

    The guess is still not 23. The loop returns to its condition check.

  5. 5
    Third check

    The condition is true, so the body executes and the user guesses 23.

  6. 6
    Termination change

    The correct guess causes running to become false. After the body finishes, the next condition check is false, so the loop exits.

Result

The loop executes once for each guess and stops after the correct guess changes the condition to false.

Boolean Decisions in Practice

A Boolean condition has two possible outcomes for the loop's decision: true or false. A true condition allows the loop body to execute. A false condition prevents another iteration and makes the loop exit immediately. This is why changing the condition variable inside the loop body is essential when the loop is meant to finish.

Truecondition resultLoop bodyexecutesFalsecondition resultLoop exitbody is skippedallowscheck againstops
How does a true or false Boolean value determine whether the loop body executes or is skipped?
python

In this example, running controls repetition. While running is true, the body obtains and checks a guess. When the guess is 23, running changes to false. The next condition check therefore stops the loop. The optional else clause runs because the loop exits normally when the condition becomes false.

Knowing When Repetition Stops

Check conditionRun loop bodyChange conditionvariableLoop exittruebody completescheck againfalse
How does the loop move from repeatedly executing its body to stopping when the condition becomes false?

A loop terminates normally when a condition check produces false. Usually, the loop body must change the variable or state used by the condition so that this can eventually happen. If nothing changes the condition, the condition can remain true and the loop can run forever. This is called an infinite loop.

Before
running = True
while running:
    print("Still running")

The condition variable is never changed, so running remains true.

After
running = True
while running:
    print("Still running")
    running = False

The loop body changes running to false, allowing the next condition check to terminate the loop.

Best practice

Before writing a while loop, identify the condition variable and the statement that can change it. Then trace at least one complete cycle: condition check, body execution, condition-variable change, and next check.

Choosing While for Practical Problems

A while loop is useful when the number of repetitions is not known in advance. In the guessing game, the program cannot know how many guesses the user will make before reaching 23. The changing condition determines when the repetition ends.

20incorrect guess25incorrect guess23correct guessFalserunning conditiontry againtry againset running false
How does the guess change during each iteration until the practical problem is solved?

The guessing game demonstrates a practical pattern: continue while the task is unfinished, perform one attempt in the body, update the condition when the task is complete, and let the next condition check end the loop.

Loop choiceWhen it fitsReason
while loopThe number of repetitions is not known in advanceA changing condition determines when execution stops
for loopThe number of repetitions or items is knownIteration is handled automatically and is usually clearer and safer

Choose a while loop when repetition depends on a condition that changes during execution.

Mistakes Beginners Make

  • Common mistake

    Forgetting to change the condition variable

    What it looks like

    The loop checks running, but the body never changes running.

    Why it fails

    The condition can remain true forever, producing an infinite loop.

    Do this instead

    Make sure the loop body contains an action that can eventually make the condition false.

  • Common mistake

    Assuming the body runs before the first condition check

    What it looks like

    Treating the first loop-body action as automatic.

    Why it fails

    A while loop checks its condition before every iteration, including the first one.

    Do this instead

    Trace the initial condition first. If it is false, the body is skipped.

  • Common mistake

    Confusing normal termination with break

    What it looks like

    Expecting the while else clause to run after a break statement.

    Why it fails

    The optional else clause runs only when the condition becomes false and the loop exits normally.

    Do this instead

    Distinguish between a false condition and a break-based exit.

Practice the Execution Trace

MEDIUM

Trace a while loop for a guessing game in which running starts as true, the target is 23, and the guesses are 20, 25, and 23. For each iteration, record whether the body runs, what the guess is, and when running becomes false.

Hints
  • Begin with the condition check before the first guess.
  • An incorrect guess leaves the game running.
  • The correct guess changes running to false, but the loop exits at the following condition check.
MEDIUM

Design a while-loop trace for a practical task where you do not know in advance how many attempts will be needed. Identify the Boolean condition, the action performed in the body, and the change that eventually makes the condition false.

Hints
  • Use a condition that represents whether the task is still unfinished.
  • Show the condition check before each attempt.
  • Include the state change that allows normal termination.

Key Takeaways

  • A while loop repeatedly executes its body while its condition is true.
  • Python checks the condition before every iteration, so a false initial condition skips the body.
  • The loop body should change the condition variable so the loop can eventually terminate.
  • A while loop's optional else clause runs after normal termination, but not after break.
  • Use a while loop when the required number of repetitions is unknown and depends on a changing condition.

Related concepts

More concepts starting with U