if construct

icon picker
qualifier

a triathlon competitor may qualify for the next competition if they have enough points

requirement

The program should allow the user to enter points for swim, cycle and run events.
The three events has a maximum of 100 points each.
The program should calculate the total points and then decide if the competitor has enough total points to qualify for the next competition.
The program should display a message stating either qualified or not qualified.

design

In this task there are two options. One way to design a solution is to use 2 if-conditional constructs. The first one to check for a successful qualifying score and the second to check for an unsuccessful score. Although this solution would work it can be simplified and improved because one and only one option is possible. It is not possible to choose both, nor is it possible to choose neither.
design 1 (2 if constructs)
enter the points

calculate total points

if total points is 200 or above then
display qualify
end if

if total points is less than 200 then
display not qualify
end if
When a competitor gets 200 or above there is no need to check if they got less than 200 since that is impossible!
(totalPoints >= 200) is true so (totalPoints < 200) must be false
Also when a competitor fails to get 200 or above then again there is no need to actually check they got less than 200 since that must be true.
(totalPoints >= 200) is false so (totalPoints < 200) must be true

design 2 (2 if constructs)
enter the points

calculate total points

if total points is 200 or above then
display qualify
else
display not qualify
end if
this version has only one condition instead of two (is total points 200 or above)
when the condition becomes true, the first option is executed and the second is skipped
when the condition becomes false, the first option is skipped and the second option is executed
the key word else marks the end of the first option and the start of the second option
the code has fewer lines and avoids unnecessary checking of a second condition

learning-point

2-way option using if-else construct
understand that the condition evaluates to either true or false and this boolean result determines which one choice of two alternative instructions to execute
use if (condition) {nested instruction} else {alternative instruction} to implement conditional execution of one or other of two alternative instructions
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 (totalPoints >= 200) {
2 System.out.println("You have qualified: ");
3 } else {
4 System.out.println("You have not qualified: ");
5 }
line 1 begins the conditional construct with the if keyword, the condition (in parentheses) and open brace { for the nested code.
line 2 is the first nested code which will be conditionally executed when the condition is true
line 3 is the close brace } for the first nested code an else and then open brace for the second nested code
line 4 is the second nested code which will be conditionally executed when the condition is false

expected output

example output 1
Enter the points for swimming: 71
Enter the points for cycling: 79
Enter the points for running: 68
Your points are 218
You have qualified:
example output 2
Enter the points for swimming: 65
Enter the points for cycling: 72
Enter the points for running: 61
Your points are 198
You have not qualified:
in the first example the test values produced a total of 218,
the comparison (totalPoints >= 200) becomes (218 >= 200) which is true
so the first nested instruction is executed
in the second example the test values produced a total of 198,
the comparison (totalPoints >= 200) becomes (198 >= 200) which is false
so the second nested instruction is executed

PRIMM: developer task

test values
test value 1: swim: 68 cycle: 67 run: 71
test value 2: swim: 64 cycle: 67 run: 69
test value 3: swim: 65 cycle: 61 run: 70
Run the example code in the program, enter test values for the swim, cycle and run events shown above) 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 else and the two alternative nested instructions within the control structure in braces { }.
Compare the code with the output. Check out how the variables are used to display the qualification message.

PRIMM investigate
Find code that contains the condition to be evaluated for the control structure
if (totalPoints >= 200) {
Try changing the inequality operator from >= to simply >
what happens when you enter the test data, the second set of number (which total to 200)
is the result correct, can you explain why you get this result

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

the program code


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