Selection how to make decisions
by Owen
Published on: Wed, Apr 1, 2015
Lesson Number: 8
Key stage: KS2
Key Stage Level: Lower
Teacher Notes: Selection
Lesson Plan: Selection
Slides: Selection
Category: Fundamentals
Concepts: Selection
What You are Going to Learn
In the last lesson we introduced the concept of keyboard input and showed you how the input can be stored in a variable.
But what if you wanted the computer to make a decision, based on what you had typed? How do you do this?
In this lesson we are going to show you how you can tell a computer program to
make a decision. We will introduce the pattern for if
statements as selection
is more commonly known.
Decisions, Decisions
So far all of the programs you have seen have not required the computer to make a decision. We are going to show you how a computer program can make a decision.
Once you understand how a program makes a decision you can use it to write more interesting programs. You will then be able to test what a user types so that your program can take different actions depending on the user input.
Important
Make sure you have completed the two lessons about variables before you try this lesson. You will need to understand variables first.
You will also need to have completed the keyboard input lesson.
Decisions and Tests
Computers have to make choices. Think about this web page for a minute. How does the computer know when to make this text bigger? The answer is the web browser performs a test and makes a decision. If the test is true then the we browser makes the text bigger.
Think about a laptop or a mobile phone or a tablet computer. What happens when the battery runs low? When the battery is low the computer displays a low power warning message. But how does the computer know when to do this? The answer is it tests the battery level. When the battery level falls below a certain, critical, level the computer displays the low power warning message.
Computer programs make decision all of the time.
Selection and Variables
When a computer makes a decision it has to answer a question and then make a selection or a choice based on the result of the test.
The question has to be written by the programmer. The question is part of the computer program. The computer will answer the question when the program runs.
When the program runs the computer can only answer true or false to the question.
This means that you have to write a very precise question!
But what questions can you ask? The question is always a test and the test involves variables. You can compare the value of two variables, or you can compare a variable to a number. This is another use for variables.
As we will show you, this is not a limiting as it seems. You can write almost any question.
Comparisons
If you wanted to compare two numbers in maths you would use the less than, <, or greater than, >, symbols. Remember the crocodile rule, the larger number is the open end of the arrow. The less than symbol is typed Shift+,. The greater than symbol is typed Shift+..
So if you had
12 > 5
you know that is true: 12 is greater than 5.
Or if you had
34 < 48
you also know that that is true: thirty four is less than forty eight.
Go is exactly the same. You use the same less than or greater than symbols to compare numbers and they mean the same as maths. The only difference is that you can compare the value of variables by using the variable name, like this
var areaOfSquare int
var areaOfTriangle int
areaOfSquare = 16
areaOfSquare = 8
areaOfTriangle < areaOfSquare
That’s less than and greater than covered, what about equals?
Equals is slightly different. It means the same as maths but we use
a different symbol. The symbol Go uses is ==
, typed as two equals
signs. You can not use one equals sign, because we have already used
that for variable assignment.
Go has two more symbols that combine equals with less than or equals with greater than.
The first one is less than or equal too, typed <=
. This is true if
the number on the left if less than the number on the right. Just like
less than. But it is also true if the number on the left is equal to the
number on the right. Just like equals. It is false if the number on
the left is greater than the number on the right.
The second one is the greater than or equal to, typed >=
. This is
true if the number on the left is greater than the number on the right.
Just like greater than. This is also true if the number on the left
is equal to the number on the right. Just like equals. It is false if
the number on the left is less than the number on the right.
Here’s a summary of the comparison operators.
== | equals to |
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
The result of each of these operators is either true or false.
Selection in Go
Go calls selection an if
statement. The selection statement starts
with the keyword if
. The pattern for an if
statement is
if condition {
statement-block
}
The condition
can be any expression that results in a true or false answer.
This can be an expression involving any of the comparison operators.
The condition can compare the value of two variables or the value of a variable
to a fixed number. Brackets are not required around the expression but may be used within as
required.
The statement-block
is any sequence of Go statements. For example
Println
statements, statements to read user input, or variable assignments
or further if
statements. The statement-block
must be surrounded by a pair
of braces, {
and }
. The opening brace, {
must be on the same line as the
if
keyword. The closing brace, }
should be on a line on its own.
If the condition
is true then the statement-block
, between the
{
and }
is executed. Execution then continues with the next statement after
the closing }
.
If the condition is false then the statement-block
, between the
{
and }
is skipped and execution continues with the next statement after
the closing }
.
The result of the condition determines which instructions will be executed next. This is how selection varies the sequence within a program.
Lets look at an example of an if
statement in Go.
1var temperature int
2
3if temperature > 20 {
4 fmt.Println("Today is hot!")
5}
6fmt.Print("Today the temperature was ")
7fmt.Println(temperature)
temperature
is a variable that has previously been declared in the
program and is of type int
.
The condition is an example of comparing the value of a variable to a fixed number in this case 20.
If the value of temperature is less than 20 then the condition is false so line 4 is skipped and only lines 6 and 7 are executed. For example if the value of temperature was 15 the output would be:
Today the temperature was 15
If the value if temperature was greater than 20 then the condition is true so line 4 is executed along with lines 6 and 7. If the value of temperature was 25 then the output would be
Today is hot!
Today the temperature was 25
If the value of temperature was exactly 20 then the condition is false, so line 4 would be skipped and only lines 6 and 7 executed.
The worldtemperature
Program
Lets look at the worldtemperature
program.
The worldtemperature
program asks the user to input the current temperature
and then compares this to the temperature in various capital cities printing
out if the temperature is hotter or colder than the other cities.
Notes
The temperatures in the capital cities was correct when the program was written. These temperates will not update. The values are hard coded into the program.
The intent of the program is to show the pattern of an if
statement and what
happens when the condition is true or false.
The output from the program, if run with a temperature of 15, is
The worldtemperature program tells you which cities are
hotter or colder than where you live.
Enter the temperature in degrees Celsius today:
15
European Cities
Hotter than Reykjavik in Iceland which is
1 degrees Celsius
Hotter than Helsinki in Finland which is
7 degrees Celsius
Colder than Athens in Greece which is
17 degrees Celsius
Hotter than Moscow in Russia which is
12 degrees Celsius
Asian Cities
Colder or the same as Tokyo in Japan which is
16 degrees Celsius
North American Cities
Cities in Africa
Cities in South America
Colder than Caracas in Venezuela which is
32 degrees Celsius
Lets look at the key points of the program.
Lines 10-23 are the variable declarations. The temperature
variable is the
variable that the user will set when the program runs.
Lines 25-36 are the variable assignments that set the temperatures for each city. Both sets of lines should now be recognisable as following the patterns for variable declaration and variable assignment.
Line 42 is the user input line. The user is asked to enter the temperate via
the Println
statement on line 41. Line 42 reads a number from the keyboard
and assigns the number to the temperature variable. The simpleio
package
is used to read the number that the user types in. The simpleio
package is
imported into the program on line 6. These lines follow the two part
pattern to read input from the user.
The first if
statement spans lines 45-49, reproduced below.
if temperature > temperatureInReykjavik {
fmt.Println("Hotter than Reykjavik in Iceland which is ")
fmt.Print(temperatureInReykjavik)
fmt.Println(" degrees Celsius")
}
The if
statement follows the pattern for if
statements.
Breaking the lines down the condition
is
temperature > temperatureInReykjavik
This condition is an example of comparing the value of two variables.
Both variables are of the same type, in this case they are both int
types.
The statement-block
is
fmt.Println("Hotter than Reykjavik in Iceland which is ")
fmt.Print(temperatureInReykjavik)
fmt.Println(" degrees Celsius")
If the condition is true then these lines will be executed. If the user inputs 15, as in the example, then the condition becomes
15 > temperatureInReykjavik
which becomes
15 > 1
because the value of the variable temperatureInReykjavik
is 1. So the condition
is true and the statement-block
is executed. As can be seen in the output
Hotter than Reykjavik in Iceland which is
1 degrees Celsius
Execution then continues at line 50 which is another if
statement.
The remainder of the program is a similar series of if
statements with
the condition varying each time.
Line 55 and 81 use a less than or equal to test, <=
. Line 87 uses a
greater then or equal to test, >=
. Line 60 uses an equals to test, ==
.
You need to enter a value of 11 for the temperature if you want to see the equals test on line 60 working.