if construct

icon picker

a car system decides what messages to display for the driver


The program should allow the user to enter the tyre pressure.
In a real car the pressure would be read by a sensor rather than a human driver.
The program should decide if a message can be displayed to confirm the correct pressure.
The program should also check if a warning message is to be displayed for low fuel.


conditional code
understand that the nested code will only be executed when the condition evaluates to true
use if (condition) {nested instruction} to implement conditional execution of instructions
use an equality comparator (==) in a condition
use an inequality comparator (<) in a condition
use several test values to evaluate if the code meets the functional requirements

introducing the code

1 if (gameLives == 0) {
2 System.out.println("Sorry, this game is finished!");
3 } // end if
line 1 begins the conditional construct with the if keyword, the condition (in parentheses) and open brace { for the nested code.
line2 is the nested code which will be conditionally executed
line 3 is the close brace } for the nested code
on line 1 the condition uses the equality operator, and will evaluate to true when the value on the left is the same as the value on the right
the nested code will be executed if and only if the condition evaluates to true

expected output

example output
Please enter the tyre pressure: 7
OK, the tyre pressure is correct
task output
Please enter the fuel level: 5
Warning, the fuel level is low

PRIMM developer task

Run the example code in the program, enter test values for the tyre pressure (shown below) and look at the output.
Read the code. Try to recognise the names, types and values for each variable.
Identify the if control structure.
Notice the condition in parenthesis ( ).
Notice the nested instruction within the control structure in braces { }.
Compare the code with the output. Check out how the variables are used to display the driver’s message.

Find the start of the IF control structure
if (tyrePressure == 7) {
Try changing the double == to a single = and run the program
what happens?
return the double ==
Now try changing to != and also change the display message to “Warning pressure error”
run the program with the test values and notice the output
Which version of the conditional code do you think would be most appropriate for a real car system
Try to give a reason for your answer
This example will help you in the next exercise, so study it carefully!
read the comment lines in the program to develop similar code for the additional requirement described above
choose suitable test values to ensure that the new code works correctly

test values

test value 1: 7
expected result: pressure is OK
test value 2: 5
expected result: (no output as the pressure is not OK)
test value 2: 8
expected result: (no output as the pressure is not OK)

the program code


developer task (PRIMM modify)

Add another input asking the user to enter the number of litres of fuel in the tank. The program should display a warning then the level is below 10.
Please enter the fuel level: 7
Waring the fuel level is low!

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.