Control flow with if, for, and switch
Fast recall
Quick revision
Core answer
if for decisions, for for repetition, and switch for choosing between several related cases. for is Go's only loop keyword, and a switch stops after the first matching case unless fallthrough is written explicitly. Choose the construct that expresses the rule most clearly.Whether you understand Go's basic control-flow tools and can choose the clearest one for a condition, loop, or set of cases.
Looking for separate while and do-while keywords, or adding `break` after every switch case as in some other languages.
Mention that `for` covers every loop form in Go and that an expressionless switch can replace a long if-else chain.
Interview-ready guide
Interview answer
In Go,
ifmakes a decision,forrepeats work, andswitchchooses between several related cases. These constructs control which statements run next.Use
ifwhen a block should run only if a Boolean condition is true. Go does not require parentheses around the condition, but braces are required. An optional short statement can appear before the condition, and a value declared there is scoped to the if-else chain.foris Go's only loop keyword. It supports the usual three-part form, a condition-only form similar towhile, an endless loop written asfor {}, andfor rangefor strings, arrays, slices, maps, and channels.Use
switchwhen several related cases are easier to read than a long if-else chain. Go evaluates cases from top to bottom and stops after the first match by default, so ordinary cases do not needbreak.My rule is simple: one decision suggests
if, repetition suggestsfor, and a set of named alternatives suggestsswitch. I choose the version that makes the business rule easiest for the next developer to understand.
Control flow with if, for, and switch
Direct Answer
Go uses if for decisions, for for repetition, and switch for choosing between several related cases. for is Go's only loop keyword, and a switch stops after the first matching case unless fallthrough is written explicitly. Choose the construct that expresses the rule most clearly.
Deep Dive & Architecture
Go uses if for decisions, for for repetition, and switch for choosing between several related cases.
for is Go's only loop keyword, and a switch stops after the first matching case unless fallthrough is written explicitly.
Choose the construct that expresses the rule most clearly.
Common mistakes include writing an endless loop accidentally, expecting switch cases to fall through, hiding errors in deeply nested conditions, and misunderstanding variables created inside if or switch. Keep branches shallow, make loop exits visible, and test boundary conditions.
Code Example
if age >= 18 {
fmt.Println("adult")
}
for i := 0; i < 3; i++ {
fmt.Println(i)
}
switch status {
case "ready":
start()
default:
wait()
}
Common Mistakes & Pitfalls
Looking for separate while and do-while keywords, or adding break after every switch case as in some other languages.
Check your understanding
Answer these without looking back at the article.
- When should you use switch instead of if-else in Go?
- When should you use if, for, or switch in Go?
- What common mistakes should you avoid with Go control flow?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.