Basic

Conditionals and Branching

Make decisions with if, else if, and else statements.

What you will see

odd

About 2 min of reading

Try It

Go decides what to run with a real boolean condition, and it uses braces for every block. You will branch on a number, scope a variable inside an if, and match a value with switch.

1

I set a score and branch on it with if, else if, and else. The first matching condition runs, so an 85 lands in the else-if branch. I print the single grade that comes out.

package main import "fmt" func main() { score := 85 if score >= 90 { fmt.Println("A") } else if score >= 80 { fmt.Println("B") } else { fmt.Println("C") } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
B
The condition must be a real boolean, and each block is wrapped in braces. The opening brace goes on the same line as the if, which gofmt enforces.
2

I declare a variable inside the if statement itself, right before the condition. It only exists inside that block, so I print a different branch to see the variable used and then disappear.

package main import "fmt" func main() { if score := 85; score >= 90 { fmt.Println("A") } else { fmt.Println("not A") } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
not A
The initializer variable is scoped to the if statement only. Reusing the same name elsewhere is fine because this one is gone once the if is done.
3

I match a weekday string with switch. Multiple values share a case with a comma, so Monday and Tuesday both print the same thing. switch breaks automatically, so there is no fall-through bug.

package main import "fmt" func main() { day := "Monday" switch day { case "Monday", "Tuesday": fmt.Println("early week") case "Friday": fmt.Println("almost weekend") default: fmt.Println("mid week") } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
early week
switch compares with == and stops at the first match. Add `fallthrough` explicitly if you ever want to continue into the next case.
4

I use switch on a string operator. A single variable is compared against each label, and the matching label runs. I print the matching word to confirm the switch picked the right case.

package main import "fmt" func main() { op := "+" switch op { case "+": fmt.Println("plus") case "-": fmt.Println("minus") default: fmt.Println("?") } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
plus
If no case matches, switch jumps to default. Here `+` matches the first case and stops immediately.
5

I combine everything into one check: a remainder test decides even or odd. Reading this line ties the lesson together and shows why the condition must be a genuine bool.

package main import "fmt" func main() { n := 7 if n%2 == 0 { fmt.Println("even") } else { fmt.Println("odd") } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
odd
Go will not treat a number as a condition; the expression must evaluate to true or false. `n%2` gives the remainder.

Data Flow

A condition is evaluated; the first true branch runs and the chain terminates.
1 Evaluate the boolean condition
↓
2 Compare true/false against each case
↓
3 Run the first matching branch
↓
4 Skip remaining branches and continue
Why this works
Braces define scope

Unlike Python, Go blocks are defined by {}:

if x > 0 {
    fmt.Println("positive")
}

The opening brace must be on the same line as the condition. The closing brace sits alone. This is a Go style rule enforced by gofmt.

Switch statements

switch is a cleaner alternative to long if/else chains:

switch op {
case "+":
    return a + b
case "-":
    return a - b
default:
    return 0
}

Each case compares with ==. Values are compared in order; the first match wins and the switch ends automatically.

Examples

Basic if/else

Branching on a condition

score := 85
if score >= 90 {
    fmt.Println("A")
} else if score >= 80 {
    fmt.Println("B")
} else {
    fmt.Println("C")
}

Go uses braces (not indentation) for blocks. The condition must be a boolean, no parentheses needed.

Why it works: the first matching branch runs, then the whole if/else chain is skipped.

The if initializer

Scoped variables

if score := 85; score >= 90 {
    fmt.Println("A")
}

You can declare a variable inside the if. It is only in scope inside that block, clean and temporary.

Why it works: the initializer runs once, then the condition is evaluated.

Matching with switch

Multiple equal cases

switch day := time.Now().Weekday(); day {
case Monday, Tuesday:
    fmt.Println("early week")
case Friday:
    fmt.Println("almost weekend")
default:
    fmt.Println("mid week")
}

switch compares day against each case. Multiple values share a case with a comma. switch auto-breaks, so no fall-through.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Primitive Types
Previous Lesson
Primitive Types
Next Lesson
Loops and Iteration
Loops and Iteration →