Programming Companion
Java

Programming technique

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, relational operators and Boolean operators.

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 user's age] --> B{Is age less than 18?} B -- Yes --> C[Display: You are under 18] B -- No --> D[Display: You are 18 or older] C --> E[Continue the program] D --> E

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

Combining conditions

OperatorMeaningExample
&&Both conditions must be trueage >= 12 && age <= 17
||At least one condition must be trueday == 6 || day == 7
!Reverses a Boolean value!isLoggedIn
OperatorMeaningExample
andBoth conditions must be trueage >= 12 and age <= 17
orAt least one condition must be trueday == 6 or day == 7
notReverses a Boolean valuenot is_logged_in

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.

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.

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