Code Companion
Java

Programming technique · B2.3.3

Conditional loops

A conditional loop repeats while a Boolean condition remains true. It is useful when the number of repetitions is not known in advance.

IB DP CS standard B2.3.3: Construct programs using counted and conditional loops appropriately, with Boolean or relational conditions controlling repetition and decisions inside loops where needed.

Choose the loop from what you know before it starts

QuestionCounted loopConditional loop
Do you know the number of repetitions in advance?Usually yesUsually no
Typical controlCounter or generated rangeBoolean/relational condition that changes during execution
ExampleDisplay 12 times-table rowsKeep checking until a future value satisfies the exit condition

Pre-condition loop: while

A while loop checks its condition before each repetition. It may execute zero times.

A future input decides when the loop ends
flowchart TD A[Ask whether checks are complete] --> B[Read the status text] B --> C{Is the status ready?} C -- No --> D[Ask again] D --> B C -- Yes --> E[Continue after the loop]

The status is read again during repetition. The loop exits only when the new value satisfies the exit condition.

Selection can happen inside repetition

The loop condition decides whether another repetition occurs. A separate if or branch inside the loop can decide what happens during that repetition.

Design the exit condition first

Before writing the loop, state exactly what must become true or false for repetition to stop. Then ensure something inside the loop can change that condition.

Check your understanding

Answer each question before opening the model answer.

  1. You must print exactly 20 square numbers. Which loop type is the natural choice?

    Reveal model answer

    A counted loop, because the number of repetitions is known before the loop begins.

  2. You must process readings until a sentinel value arrives. Which loop type is the natural choice?

    Reveal model answer

    A conditional loop, because the number of readings is not known before the loop begins.

  3. What creates an infinite-loop risk in the ready-status example?

    Reveal model answer

    If status is never read or changed inside the loop, the condition can remain true forever.

  4. In the temperature example, what does the if/elif or if/else-if structure do?

    Reveal model answer

    It classifies each reading during a repetition; the while condition separately decides whether another reading should be processed.

Optional Java pattern: do while

Java also has do while, which executes the body before testing the condition, so it always runs at least once. It is useful when the first attempt must happen before the exit test.

Equivalent Python behaviour

Python has no dedicated do while statement. When the body must run before an exit test, a common pattern is while True followed by a deliberate break after the accepted condition is reached.

Syllabus focus: the core requirement is choosing and constructing counted versus conditional loops. The post-condition pattern above is a language-specific way to express a useful loop shape, not a separate additional loop standard.

Unknown repetitions

Use a conditional loop when input, a score or another changing condition decides when to stop.

Update the condition

If nothing can change the loop condition, the program may repeat forever.

Use AND and OR carefully

Write test values that make each part of && and || conditions true and false.

Use and / or carefully

Write test values that make each part of combined and and or conditions true and false.

Challenges Choose one

Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.

Menu Selection

Challenge ID: PC-T08-C01 · Standards: B2.3.3

Display a menu with 1. Play Game, 2. Instructions and 3. Quit. Keep asking until the user enters a valid option from 1 to 3, then output a suitable response.

Scaffold available

Guess the Number

Challenge ID: PC-T08-C02 · Standards: B2.3.2, B2.3.3

Generate a random number from 1 to 100. Keep asking the player to guess until the number is correct. After each incorrect guess, say whether it was too high or too low. Report the number of guesses at the end.

Scaffold available
A hidden mystery number with guesses marked too high and too low.

Gamertag Validator

Challenge ID: PC-T08-C03 · Standards: B2.1.2, B2.3.2, B2.3.3

Keep asking for a gamertag until it is valid. It must contain between 4 and 15 characters, contain no spaces and begin with a letter. Explain every failed rule before asking again.

Scaffold available

Rock Paper Scissors

Challenge ID: PC-T08-C04 · Standards: B2.3.2, B2.3.3

Create a game of rock, paper, scissors against the computer. Validate the player's choice, keep a score for both sides and repeat rounds until one player reaches 10 points. Output the winner of each round and the final match winner.

Scaffold available
Rock, paper and scissors symbols beside a simple score display.