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:
WednesdayThe 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
Beginnerswitch 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."
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions