Menu
Coddy logo textTech

The `switch` Statement

Part of the Fundamentals section of Coddy's GO journey — lesson 39 of 109.

Let's learn about the switch statement in Go, which provides a cleaner way to check multiple conditions compared to using many if/else if statements.

First, let's create a simple program that evaluates a day number and prints the corresponding day name:

day := 3
    
switch day {
case 1:
    fmt.Println("Monday")
case 2:
    fmt.Println("Tuesday")
case 3:
    fmt.Println("Wednesday")
default:
    fmt.Println("Another day")
}

After executing this code, the program will check the value of day (which is 3) and match it with the appropriate case. Since day equals 3, the program will execute the code under case 3 and show on the screen:

Wednesday

The switch statement checks each case in order until it finds a match. If no match is found, it executes the code under the default case. This makes your code much cleaner than writing multiple if/else if statements.

challenge icon

Challenge

Beginner
In this challenge, you'll practice using the switch statement in Go. You'll be given a variable representing a day of the week, and you need to complete the switch statement to display whether it's a weekday or weekend.

Cheat sheet

The switch statement in Go provides a cleaner way to check multiple conditions compared to using many if/else if statements.

Basic syntax:

switch variable {
case value1:
    // code to execute
case value2:
    // code to execute
default:
    // code to execute if no match
}

Example:

day := 3
    
switch day {
case 1:
    fmt.Println("Monday")
case 2:
    fmt.Println("Tuesday")
case 3:
    fmt.Println("Wednesday")
default:
    fmt.Println("Another day")
}

The switch statement checks each case in order until it finds a match. If no match is found, it executes the code under the default case.

Try it yourself

package main

import "fmt"

func main() {
	// This variable represents a day of the week
	day := "Saturday"
	
	// TODO: Complete the switch statement to check if the day is a weekday or weekend
	switch day {
	// Add cases for weekend days (Saturday and Sunday) and print "It's the weekend!"
	
	// Add a default case for weekdays and print "It's a weekday."
	
	}
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals