Learn/Go/4. Making decisions
Lesson 4 of 15
Sign in

Go, lesson 4

Making decisions

Programs make choices all the time: is the password long enough, is the player out of lives, is it a weekend? In Go you make those choices with if and switch.

if, else if, else

An if runs its block only when its condition is true. Go does not use parentheses around the condition, but the braces are always required, even for a one-line body.

Go checks the conditions from top to bottom and runs the first block whose condition is true. The others are skipped. The else must sit on the same line as the closing brace before it.

The condition must be a real bool. Unlike some languages, Go does not treat 0 or "" as false; if count { ... } is a compile error. Write instead.

2 exercises to try what you just read.

Exercise 1

What to wear

Press Ctrl Enter or Check to test your code.

Exercise 2

Leap year

Press Ctrl Enter or Check to test your code.

if count != 0

Comparison operators

OperatorMeaning
==equal
!=not equal
<, <=less than, less or equal
>, >=greater than, greater or equal

Strings compare too: == checks that they are identical, and < compares them in dictionary (byte) order, so "apple" < "banana" is true.

Boolean operators

Combine conditions with && (and), || (or) and ! (not):

Both && and || short-circuit: if the left side already decides the answer, the right side is not evaluated. That lets you write if n != 0 && total/n > 10 without dividing by zero.

if with a short statement

An if can start with a short statement, separated by a semicolon. Variables declared there exist only inside the if and its else branches. This is common Go style for checking a result right where you compute it:

switch

When you compare one value against many options, switch reads better than a long else if chain. Each case can list several values. Go runs only the matching case; there is no fall-through by default, so you do not need break.

A switch with no value after it tests each case as its own condition. It is a tidy way to write ranges:

Further reading

  • A Tour of Go: If
  • A Tour of Go: Switch
  • Effective Go: Control structures

Read a temperature in degrees Celsius (a whole number) and print one word of advice:

  • coat if it is below 5
  • jacket if it is from 5 up to 17 (inclusive)
  • t-shirt if it is from 18 up to 29 (inclusive)
  • shade if it is 30 or more

Sample input:

Sample output:

Read a year and print leap or common. A year is a leap year if it is divisible by 4, except years divisible by 100, which are leap years only if they are also divisible by 400. So 2024 and 2000 are leap years, but 1900 is not.

Sample input:

Sample output:

if temp > 30 {
	fmt.Println("Hot!")
} else if temp > 15 {
	fmt.Println("Mild.")
} else {
	fmt.Println("Cold.")
}
if age >= 13 && age <= 19 {
	fmt.Println("teenager")
}
if day == "Sat" || day == "Sun" {
	fmt.Println("weekend")
}
if !raining {
	fmt.Println("no umbrella needed")
}
if rem := n % 2; rem == 0 {
	fmt.Println("even")
} else {
	fmt.Println("odd")
}
switch day {
case "Sat", "Sun":
	fmt.Println("weekend")
case "Fri":
	fmt.Println("almost there")
default:
	fmt.Println("workday")
}
switch {
case score >= 90:
	fmt.Println("A")
case score >= 80:
	fmt.Println("B")
default:
	fmt.Println("keep going")
}
12
jacket
2024
leap