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.

  1package main
  2
  3import (
  4	"fmt"
  5
  6	"github.com/gophercoders/simpleio"
  7)
  8
  9func main() {
 10	var temperature int
 11
 12	var temperatureInReykjavik int
 13	var temperatureInAthens int
 14	var temperatureInBeijing int
 15	var temperatureInCaracas int
 16	var temperatureInHelsinki int
 17	var temperatureInLondon int
 18	var temperatureInMadrid int
 19	var temperatureInWashingtonDC int
 20	var temperatureInPretoria int
 21	var temperatureInSantiago int
 22	var temperatureInMoscow int
 23	var temperatureInTokyo int
 24
 25	temperatureInReykjavik = 1
 26	temperatureInAthens = 17
 27	temperatureInBeijing = 17
 28	temperatureInCaracas = 32
 29	temperatureInHelsinki = 7
 30	temperatureInLondon = 11
 31	temperatureInMadrid = 13
 32	temperatureInWashingtonDC = 16
 33	temperatureInPretoria = 15
 34	temperatureInSantiago = 25
 35	temperatureInMoscow = 12
 36	temperatureInTokyo = 16
 37
 38	fmt.Println("The worldtemperature program tells you which cities are ")
 39	fmt.Println("hotter or colder than where you live.")
 40
 41	fmt.Println("Enter the temperature in degrees Celsius today: ")
 42	temperature = simpleio.ReadNumberFromKeyboard()
 43
 44	fmt.Println("European Cities")
 45	if temperature > temperatureInReykjavik {
 46		fmt.Println("Hotter than Reykjavik in Iceland which is ")
 47		fmt.Print(temperatureInReykjavik)
 48		fmt.Println(" degrees Celsius")
 49	}
 50	if temperature > temperatureInHelsinki {
 51		fmt.Println("Hotter than Helsinki in Finland which is ")
 52		fmt.Print(temperatureInHelsinki)
 53		fmt.Println(" degrees Celsius")
 54	}
 55	if temperature <= temperatureInAthens {
 56		fmt.Println("Colder than Athens in Greece which is ")
 57		fmt.Print(temperatureInAthens)
 58		fmt.Println(" degrees Celsius")
 59	}
 60	if temperature == temperatureInLondon {
 61		fmt.Println("Exactly the same as London in the UK which is also ")
 62		fmt.Print(temperatureInLondon)
 63		fmt.Println(" degrees Celsius")
 64	}
 65	if temperature < temperatureInMadrid {
 66		fmt.Println("Colder than Madrid in Spain which is ")
 67		fmt.Print(temperatureInMadrid)
 68		fmt.Println(" degrees Celsius")
 69	}
 70	if temperature > temperatureInMoscow {
 71		fmt.Println("Hotter than Moscow in Russia which is ")
 72		fmt.Print(temperatureInMoscow)
 73		fmt.Println(" degrees Celsius")
 74	}
 75	fmt.Println("Asian Cities")
 76	if temperature > temperatureInBeijing {
 77		fmt.Println("Hotter than Beijing in China which is ")
 78		fmt.Print(temperatureInBeijing)
 79		fmt.Println(" degrees Celsius")
 80	}
 81	if temperature <= temperatureInTokyo {
 82		fmt.Println("Colder or the same as Tokyo in Japan which is ")
 83		fmt.Print(temperatureInTokyo)
 84		fmt.Println(" degrees Celsius")
 85	}
 86	fmt.Println("North American Cities")
 87	if temperature >= temperatureInWashingtonDC {
 88		fmt.Println("Warmer or the same as Washington DC in the USA which is ")
 89		fmt.Print(temperatureInWashingtonDC)
 90		fmt.Println(" degrees Celsius")
 91	}
 92	fmt.Println("Cities in Africa")
 93	if temperature > temperatureInPretoria {
 94		fmt.Println("Hotter than Pretoria in South Africa which is ")
 95		fmt.Print(temperatureInPretoria)
 96		fmt.Println(" degrees Celsius")
 97	}
 98	fmt.Println("Cities in South America")
 99	if temperature < temperatureInCaracas {
100		fmt.Println("Colder than Caracas in Venezuela which is ")
101		fmt.Print(temperatureInCaracas)
102		fmt.Println(" degrees Celsius")
103	}
104	if temperature > temperatureInSantiago {
105		fmt.Println("Hotter than Santiago in Chile which is ")
106		fmt.Print(temperatureInSantiago)
107		fmt.Println(" degrees Celsius")
108	}
109}
Fig-1. The worldtemperature code

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.

Search

Featured Lesson

Numbers

What You are Going to Learn?

Computers are used to process data. All data is made up of numbers. Yes, really! Everything is just a bunch of numbers to a computer. These are the only things they understand.

We are going to explain how numbers are used in Go programs. Then we are going to show you how to do type sums in Go.