Programming Companion
Java

Programming technique

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, with Boolean or relational conditions controlling execution.

Pre-condition loop: while

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

Invalid input returns to the start of the loop
flowchart TD A[Display the menu] --> B[Read the user's choice] B --> C{Is the choice from 1 to 3?} C -- No --> D[Explain the valid range] D --> A C -- Yes --> E[Process the selected option] E --> F[Continue after the loop]

The loop exits only after the user enters a value that satisfies the condition.

Post-condition loop: do while

A do while loop executes the body once before testing the condition, so it always runs at least once.

Post-condition behaviour with while True

Python has no dedicated do while statement. Use while True when the body must run before the exit test, then use break when the accepted condition is reached.

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.

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.

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.

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.

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.

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