Code Companion
Java

Programming technique · B2.3.2

Selection

Selection allows a program to choose which instructions to execute. Each decision is controlled by a condition that evaluates to either true or false.

IB DP CS standard B2.3.2: Construct programs using appropriate selection structures with relational and Boolean operators to control which code blocks execute.

Using an if statement

Use if when an action should happen only when a condition is true.

Choosing between alternatives

Add else when exactly one of two paths should run. Use an ordered else if chain when several mutually exclusive ranges or cases are possible.

Add else when exactly one of two paths should run. Use an ordered elif chain when several mutually exclusive ranges or cases are possible.

One condition creates two possible paths
flowchart TD A[Read the battery percentage] --> B{Is battery below 20?} B -- Yes --> C[Display: Charge soon] B -- No --> D[Display: Battery level acceptable] C --> E[Continue the program] D --> E

Only one branch runs. After that branch finishes, both paths rejoin and the program continues.

Relational operators build conditions

OperatorMeaningExample
<less thantemperature < 0
<=less than or equal tobattery <= 100
>greater thanbalance > 0
>=greater than or equal tobattery >= 80
==equal tochoice == 3
!=not equal tochoice != 0

Combine conditions when one comparison is not enough

OperatorMeaningExample
&&Both conditions must be truetemperature >= 18 && temperature <= 24
||At least one condition must be trueday == 6 || day == 7
!Reverses a Boolean value!isLoggedIn
OperatorMeaningExample
andBoth conditions must be truetemperature >= 18 and temperature <= 24
orAt least one condition must be trueday == 6 or day == 7
notReverses a Boolean valuenot is_logged_in

Check your understanding

Answer each question before opening the model answer.

  1. For battery = 62, which branch of the worked example runs?

    Reveal model answer

    The battery is not at least 80, but it is at least 30, so the second branch outputs Medium.

  2. Why must battery >= 80 appear before battery >= 30?

    Reveal model answer

    A value such as 90 satisfies both tests. If the broader lower threshold came first, it would capture values that should reach the High category.

  3. Which condition accepts temperatures from 18 through 24 inclusive?

    Reveal model answer

    Java: temperature >= 18 && temperature <= 24. Python: temperature >= 18 and temperature <= 24.

  4. What is the difference between = and ==?

    Reveal model answer

    = assigns a value; == compares two values for equality.

Common mistakes

Assignment instead of comparison

Use == to compare values. A single = assigns a value.

String comparison

Use name.equals("Alex"), not name == "Alex".

String comparison

Use name == "Alex" to compare string values.

Wrong branch order

Test the most restrictive or highest range first when earlier branches could capture later cases.

Challenges Choose one

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

Under Age

Challenge ID: PC-T06-C01 · Standards: B2.3.2

Ask the user for their age. Display one message when the user is under 18 and a different message when they are 18 or older.

Vocational Grade

Challenge ID: PC-T06-C02 · Standards: B2.3.2

Ask the user for a mark from 0 to 100. Output an appropriate vocational grade using an ordered if / else-if chain. Reject marks outside the valid range and make each grade boundary clear in your code.

Train Ticket

Challenge ID: PC-T06-C03 · Standards: B2.3.2

A train fare costs £20 per station for each adult and half price for each child. Add £5 per station during the peak period from 06:00 up to 09:00. Ask for the number of stations, adults, children and hour of travel, then output the complete fare. Test peak and off-peak journeys.

Scaffold available
A train, ticket, route and fare calculation symbols.

Hours Worked

Challenge ID: PC-T06-C04 · Standards: B2.3.2

Ask for the number of hours worked this week and the hourly rate. Calculate gross pay, paying hours above 40 at 1.5 times the normal rate. Display an error when hours are outside the range 0 to 60. Test the lower boundary, exactly 40 hours, overtime and an invalid value.

Scaffold available
Working hours and an hourly rate leading to normal pay, overtime and a wage total.