Skip to content
Lesson 6 of 14

Step 1 of 5 · Reading · ~4 min

Learn

Control Flow

If, Else, Switch

By the end of this lesson you can branch with if / else if / else, write both forms of switch, and order overlapping conditions so the branch you meant is the branch that actually runs.

if takes a bool, and only a bool

go

Output: hot.

Two syntax rules, both enforced by the compiler:

  • No parentheses around the condition. if (temp > 30) does compile, but gofmt strips the parens as noise.
  • Braces always, even for a one-line body. Go has no brace-less if, so the classic "the second indented line was never actually inside the if" bug cannot be written.

And one type rule with teeth: the condition must be a bool — a value that is only ever true or false. if temp { is a compile error (non-bool temp (type int) used as if condition). No truthiness means there is nothing to memorise about which values count as false.

Chains, and why order is logic

An else if chain is read top to bottom and stops at the first condition that is true:

go

Output: freight. Swap those first two branches and the same 25 prints parcel instead: weight > 5 is already true, so the freight branch is never reached at all. A broad condition placed early swallows the narrower ones below it.

The same trap bites hardest with a combined condition (&& means both must hold, || means either will do):

go

Output: member rate. The second branch describes a stricter situation than the first, so it can never win. Written the other way round, the && branch first, those same two values print member weekend rate. Most specific first, always.

if with an initialiser

if can run one short statement before the condition, scoping its variables to the if/else:

go

With budget := 50 and spent := 62, that prints over by 12. It is the shape that carries Go's error handling in chapter 4.

switch, two ways

Switching on a value:

go
  • Cases take lists (case "Sat", "Sun"), which kills a whole family of duplicated branches.
  • No fallthrough by default: each case ends where the next one begins, so a forgotten break cannot hurt you. An explicit fallthrough keyword exists for the rare day you want C's behaviour.

Leave the value out and switch becomes a tidier else if chain, with the same top-to-bottom, first-true-wins rule:

go

Reach for this form any time you stack three or more else ifs.

Common mistakes

  • Broad branch first. A value that satisfies two rules reports whichever rule you tested first, and the combined answer never prints — exactly the member rate bug above.
  • || where both conditions must hold. With ||, a value matching only one of the two rules falls into the both-rules branch and gets the combined label.
  • No final else or default. Values that match nothing print nothing at all: the program ends with empty output, and every such test fails rather than just one.
  • Assuming the input is positive. Go's % keeps the sign of the left operand: -9 % 3 is 0, so divisibility tests still work on negatives, but -8 % 3 is -2, not 1. Guarding a branch with n > 0 silently drops negative input into the wrong branch.

Your exercise

Print one of four labels for a single number, following the divisibility rules given in the exercise text. The starter already reads n and prints it unconditionally; replace that print with your branch chain — if/else or a bare switch, either is fine. The mistake that costs most people the run is the ordering one above: a number satisfying two rules must reach the branch that reports both. Hidden tests include zero and negative numbers.

Up nextLoops with forControl Flow

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…