Bird
Raised Fist0
Goprogramming~10 mins

Switch statement in Go - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Switch statement
Start
↓
Evaluate expression
↓
Compare with case 1?
No→Compare with case 2?
↓
Execute case 1
↓
Exit switch statement
The switch statement evaluates an expression once and compares it to each case value in order. When a match is found, it executes that case's code and then exits the switch.
Execution Sample
Go
package main
import "fmt"
func main() {
  day := 3
  switch day {
  case 1: fmt.Println("Monday")
  case 2: fmt.Println("Tuesday")
  case 3: fmt.Println("Wednesday")
  default: fmt.Println("Unknown day")
  }
}
This code checks the value of 'day' and prints the matching weekday name or 'Unknown day' if no match.
Execution Table
StepExpression ValueCase CheckedMatch?ActionOutput
13case 1NoCheck next case
23case 2NoCheck next case
33case 3YesExecute case 3Wednesday
4---Exit switch
💡 After matching case 3, switch exits without checking further cases.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
day33333
Key Moments - 2 Insights
Why does the switch stop checking cases after finding a match?
In the execution_table at Step 3, once case 3 matches, the switch executes that case and immediately exits, so no further cases are checked.
What happens if no case matches the expression value?
If no case matches, the default case runs as a fallback. In this example, default would print 'Unknown day' if day was not 1, 2, or 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at Step 3?
A"Tuesday"
B"Monday"
C"Wednesday"
D"Unknown day"
💡 Hint
Check the Output column at Step 3 in the execution_table.
At which step does the switch statement stop checking further cases?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the Match? and Action columns in the execution_table to see when a case matches and switch exits.
If day was 5, which case would run according to the execution flow?
Acase 3
Bdefault
Ccase 1
DNo case runs
💡 Hint
Recall that default runs when no case matches; see key_moments about default case.
Concept Snapshot
switch expression {
  case value1:
    // code
  case value2:
    // code
  default:
    // code
}

- Evaluates expression once
- Compares to each case in order
- Executes first matching case
- Exits switch after match
- default runs if no match
Full Transcript
The switch statement in Go evaluates an expression once and compares it to each case value in order. When a case matches, it executes that case's code and then exits the switch without checking further cases. If no case matches, the default case runs as a fallback. In the example, the variable 'day' is 3, so the switch checks case 1 (no match), case 2 (no match), then case 3 (match), prints 'Wednesday', and exits. This behavior helps avoid writing multiple if-else statements and makes code clearer when checking one value against many possibilities.

Practice

(1/5)
1. What does a switch statement do in Go?
easy
A. It selects one case to run based on a value.
B. It repeats code multiple times like a loop.
C. It defines a new function.
D. It declares a variable.

Solution

  1. Step 1: Understand the purpose of switch

    A switch statement chooses one path among many based on a value.
  2. Step 2: Compare with other options

    Loops repeat code, functions define behavior, variables store data. Switch picks cases.
  3. Final Answer:

    It selects one case to run based on a value. -> Option A
  4. Quick Check:

    Switch = Select case [OK]
Hint: Switch picks one matching case to run [OK]
Common Mistakes:
  • Confusing switch with loops
  • Thinking switch defines functions
  • Believing switch declares variables
2. Which of the following is the correct syntax for a basic switch statement in Go?
easy
A. switch (x) { case 1: fmt.Println("One") default: fmt.Println("Other") }
B. switch x { case 1: fmt.Println("One") default: fmt.Println("Other") }
C. switch x: case 1: fmt.Println("One") default: fmt.Println("Other")
D. switch x { case 1 => fmt.Println("One") default => fmt.Println("Other") }

Solution

  1. Step 1: Recall Go switch syntax

    Go uses switch variable { case value: ... default: ... } without parentheses.
  2. Step 2: Check each option

    switch x { case 1: fmt.Println("One") default: fmt.Println("Other") } matches correct syntax. switch (x) { case 1: fmt.Println("One") default: fmt.Println("Other") } wrongly uses parentheses. switch x: case 1: fmt.Println("One") default: fmt.Println("Other") uses colon after switch. switch x { case 1 => fmt.Println("One") default => fmt.Println("Other") } uses => which is invalid.
  3. Final Answer:

    switch x { case 1: fmt.Println("One") default: fmt.Println("Other") } -> Option B
  4. Quick Check:

    Correct syntax = switch x { case 1: fmt.Println("One") default: fmt.Println("Other") } [OK]
Hint: No parentheses around switch variable in Go [OK]
Common Mistakes:
  • Adding parentheses around switch variable
  • Using colon after switch keyword
  • Using => instead of colon in cases
3. What will be the output of this Go code?
package main
import "fmt"
func main() {
  x := 2
  switch x {
  case 1:
    fmt.Println("One")
  case 2:
    fmt.Println("Two")
  case 3:
    fmt.Println("Three")
  default:
    fmt.Println("Other")
  }
}
medium
A. Two
B. Three
C. One
D. Other

Solution

  1. Step 1: Identify the switch variable and value

    The variable x is 2.
  2. Step 2: Match the case with value 2

    The case 2 matches and prints "Two".
  3. Final Answer:

    Two -> Option A
  4. Quick Check:

    Switch x=2 prints "Two" [OK]
Hint: Match switch value to case label [OK]
Common Mistakes:
  • Choosing default instead of matching case
  • Confusing case numbers
  • Ignoring switch variable value
4. Find the error in this Go switch code:
package main
import "fmt"
func main() {
  x := 3
  switch x {
  case 1
    fmt.Println("One")
  case 2:
    fmt.Println("Two")
  default:
    fmt.Println("Other")
  }
}
medium
A. fmt.Println syntax error
B. Missing default case
C. Switch variable must be string
D. Missing colon after case 1

Solution

  1. Step 1: Check case syntax

    Each case must end with a colon (:). Here, case 1 misses the colon.
  2. Step 2: Verify other parts

    Default case exists, switch variable can be int, fmt.Println is correct.
  3. Final Answer:

    Missing colon after case 1 -> Option D
  4. Quick Check:

    Case syntax requires colon [OK]
Hint: Each case must end with a colon (:) [OK]
Common Mistakes:
  • Omitting colon after case
  • Thinking default is mandatory
  • Assuming switch variable must be string
5. How can you write a Go switch statement that prints "Weekend" for Saturday and Sunday, and "Weekday" for other days (1=Monday, 7=Sunday)?
hard
A. switch day { case day == 6 || day == 7: fmt.Println("Weekend") default: fmt.Println("Weekday") }
B. switch day { case 6: fmt.Println("Weekend") case 7: fmt.Println("Weekday") default: fmt.Println("Weekday") }
C. switch day { case 6, 7: fmt.Println("Weekend") default: fmt.Println("Weekday") }
D. switch day { case 6: fmt.Println("Weekend") case 7: fmt.Println("Sunday") default: fmt.Println("Weekday") }

Solution

  1. Step 1: Group weekend days in one case

    Using case 6, 7: groups Saturday and Sunday together.
  2. Step 2: Use default for other days

    Default prints "Weekday" for days 1 to 5.
  3. Final Answer:

    switch day { case 6, 7: fmt.Println("Weekend") default: fmt.Println("Weekday") } -> Option C
  4. Quick Check:

    Multiple values in one case = switch day { case 6, 7: fmt.Println("Weekend") default: fmt.Println("Weekday") } [OK]
Hint: Use commas to group cases in Go switch [OK]
Common Mistakes:
  • Separating weekend days into different cases with wrong output
  • Using logical OR inside switch expression incorrectly
  • Missing default case for weekdays
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard