if construct

icon picker
temperature

3-way options using nested if-else construct

requirement

display a message about the temperature
when the temperature is above 5 it is normal, below -5 it is very cold and in between these values it is freezing
only one of the three messages will be displayed

design

In this task there are three options. One way to design a solution is to use 3 if-conditional constructs. The first one to check for a normal temperature, the second to check for a very cold temperature and the third for in-between temperatures that would be around freezing. 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 3 or 2, nor is it possible to choose none.
design 1 (3 if constructs)
prompt for the temperature
enter the temperature

if temparature is above 5 then
display normal message
end if

if temparature is below -5 then
display very cold message
end if

if temperature is between -5 and 5 then
display freezing point message
end if
When the temperature is 5 or above there is no need to check if it is less than -5 or in between since that is impossible!
(temperature >= 5) is true, so (temperature < -5) must be false, as well as (temperature between -5 and 5)
When the temperature is not 5 or above then it is necessary to to check if it is below -5
if (temperature < -5) is true so (temperature between -5 and 5) must be false
if (temperature < -5) is false so (temperature between -5 and 5) must be true
it can’t be 5 or above since that has already been checked and is false

design 2 (nested if-else construct)
prompt for the temperature
enter the temperature

if temparature is above 5 then
display normal message

else
if temparature is below -5 then
display very cold message

else
display freezing point message

end if

end if
this version has only two conditions instead of three, the second condition is nested inside the else section of the first condition, the third option is nested in the else section of the second condition
when condition 1 becomes true, the first option is executed and the second and third are skipped
when condition 1 becomes false, the first option is skipped and the second condition is checked
when condition 2 becomes true, the second option is executed and the third option is skipped
when condition 2 becomes false, the second option is skipped and the third option is executed
the key word else marks the end of the first option and the start of the alternative options
the alternative option is a second if-else construct nested within the first and contains the second condition
the code has fewer lines and avoids unnecessary checking of a second condition

learning-point

3-way option using nested if-else construct
understand that the condition evaluates to either true or false and this boolean result determines which one choice of two alternative option to execute and the second option is a nested if-else construct nested within the first
use if (condition) {nested instruction} else { 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 (temperature > 5) {
2 System.out.println("temperature is normal");
3 } else {

4 if (temperature < -5) {
5 System.out.println("temperature is very cold");
6 } else {
7 System.out.println("temperature is near freezing");
8 }

9 }
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 condition 1 is true
line 4-8 is the nested if-else construct that implements the second condition which selects either the second or third option to execute

expected output

example output 1
What is the temperature: 7
temperature is normal
example output 2
What is the temperature: -6
temperature is very cold
example output 3
What is the temperature: -5
temperature is near freezing
in the first example the test value is 7
the comparison (temperature > 5) becomes (7 > 5) which is true
so the first nested instruction is executed
in the second example the test value is -6,
the comparison (temperature > 5) becomes (-6 > 5) which is false
then the next comparison (temperature < -5) becomes (-6 < -5) which is true
so the second nested instruction is executed
in the third example the test value is -5,
the comparison (temperature > 5) becomes (-5 > 5) which is false
then the next comparison (temperature < -5) becomes (-5 < -5) which is false
so the third nested instruction is executed

developer task

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.
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

program

Loading…

PRIMM modify

Ask the user to enter the precipitation percentage as a fraction so 15% would be 0.15. When the precipitation is below 0.07 (7%) then it will be dry weather. When the precipitation is above 0.6 (60%) then it is likely to rain. Otherwise the forecast is a risk of rain.
Create a program to allow the forecaster to enter the precipitation percentage as a fraction. The program should then display one of the messages:
the forecast is dry weather
the forecast is a chance of rain
the forecast is that rain is likely

Please enter the precipitation: 0.45
The forecast is a chance of rain

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.