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.
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.
Only one branch runs. After that branch finishes, both paths rejoin and the program continues.
Combining conditions
| Operator | Meaning | Example |
|---|---|---|
&& | Both conditions must be true | age >= 12 && age <= 17 |
|| | At least one condition must be true | day == 6 || day == 7 |
! | Reverses a Boolean value | !isLoggedIn |
| Operator | Meaning | Example |
|---|---|---|
and | Both conditions must be true | age >= 12 and age <= 17 |
or | At least one condition must be true | day == 6 or day == 7 |
not | Reverses a Boolean value | not 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
SelectedAsk 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
SelectedAsk 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
SelectedA 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.
Hours Worked
SelectedAsk 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.
Selected challenge
This choice is shared with the portfolio setup page.
Create your challenge folder
Run this command before starting. It creates the correct empty folder inside your portfolio.
Complete the challenge
Write and test your own solution in the folder created above.
Optional scaffolded support
Use only as much support as you need. The templates organise the program but leave the important algorithm unfinished.
Submit for review
Run this when your program is complete. It creates the README, commits the folder and pushes it.