Decisions and Loops in Go: if, for, and switch
How Go makes decisions with if and switch, and why Go has only one loop keyword (for) that handles counting, while-loops, and infinite loops.
You'll learn how if, else, switch, and for control the flow of a Go program, why parentheses are omitted around conditions, and how Go replaces while with for. Assumes basic Go variables.
1. Go keeps control flow brutally minimal
Most languages give you for, while, do-while, and forEach. Go looked at all of those and said: "You only need for."
And unlike C or JavaScript, Go drops the parentheses around conditions—if x > 5 { instead of if (x > 5) {—while requiring curly braces { ... } even for a one-line body.
package main
import "fmt"
func main() {
coffees := 3
if coffees == 0 {
fmt.Println("Send help.")
} else if coffees < 3 {
fmt.Println("Functional, barely.")
} else {
fmt.Println("Vibrating at a higher frequency.")
}
}Vibrating at a higher frequency.2. Three ways to write for
Because Go has no while keyword, for wears three different hats:
package main
import "fmt"
func main() {
// 1. Counting loop (init; condition; post)
for i := 1; i <= 3; i++ {
fmt.Println("Attempt", i)
}
// 2. While-style loop (condition only)
tries := 2
for tries > 0 {
fmt.Println("Tries left:", tries)
tries--
}
// 3. Infinite loop (until break)
for {
fmt.Println("Running once, then escaping.")
break
}
}Attempt 1
Attempt 2
Attempt 3
Tries left: 2
Tries left: 1
Running once, then escaping.Use continue to skip to the next iteration, and break to exit the loop immediately.
Write a for loop that prints only even numbers from 2 to 10 using i += 2.
3. switch without accidental fallthrough
In C and JavaScript, forgetting break inside a switch causes execution to accidentally tumble into the next case. In Go, every case breaks automatically:
status := "Monday"
switch status {
case "Saturday", "Sunday":
fmt.Println("Weekend!")
case "Monday":
fmt.Println("My condolences.")
default:
fmt.Println("Just another day.")
}My condolences.You can also list multiple values on one case separated by commas ("Saturday", "Sunday"), or write switch { with no value to replace a long chain of if / else if.
Common mistakes
| Error | What it means | Fix |
|---|---|---|
Used while keyword | Came from Python/JS | Replace while cond with for cond |
Put else on a new line after } | Go's automatic semicolon insertion ended the if statement | Keep } else { on the same line |
Parentheses around if (x > 0) | Not an error, just unidiomatic | Run gofmt -w . or drop the parentheses |
Practice
- How do you write an infinite loop in Go?
- Does Go's
switchneed abreakat the end of everycase?
Answers:
for { ... }.- No—Go breaks automatically at the end of each
case.
What you learned
ifandforskip parentheses around conditions and always require{ ... }braces.forhandles counting loops,while-style condition loops, andfor {}infinite loops.switchbreaks automatically after each matchingcaseand supports comma-separated values.
Video walkthrough
YouTubeClick play to load video (YouTube)