Switch with `fallthrough`
Part of the Logic & Flow section of Coddy's GO journey — lesson 1 of 68.
In Go, switch statements normally execute only the matching case and then exit automatically. However, the fallthrough keyword changes this behavior by forcing execution to continue to the next case, regardless of whether it matches.
When you use fallthrough at the end of a case, Go will execute the next case's code without checking its condition. This is different from languages like C where you need break to prevent falling through.
grade := "B"
switch grade {
case "A":
fmt.Println("Excellent!")
fallthrough
case "B":
fmt.Println("Good job!")
fallthrough
case "C":
fmt.Println("Keep trying!")
}In this example, if grade is "B", it will print both "Good job!" and "Keep trying!" because of the fallthrough statement. This allows you to execute multiple related actions for a single matching case.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyIn this challenge, you'll practice using the fallthrough keyword in a switch statement to create a priority alert system. When a high-priority alert is triggered, the system should display multiple warning messages in sequence.
You will receive a string input representing an alert level. Create a switch statement that handles different alert levels and uses fallthrough to display cascading messages for higher priority alerts.
The input will be one of these alert levels: "critical", "high", "medium", or "low".
Your switch statement should work as follows:
- For
"critical": Print "CRITICAL: System shutdown imminent!" and fall through to print the high priority message as well - For
"high": Print "HIGH: Immediate attention required!" and fall through to print the medium priority message as well - For
"medium": Print "MEDIUM: Please review when possible" - For
"low": Print "LOW: Informational only"
Use the fallthrough keyword so that critical alerts display both critical and high priority messages, and high alerts display both high and medium priority messages.
Cheat sheet
The fallthrough keyword in Go switch statements forces execution to continue to the next case without checking its condition:
grade := "B"
switch grade {
case "A":
fmt.Println("Excellent!")
fallthrough
case "B":
fmt.Println("Good job!")
fallthrough
case "C":
fmt.Println("Keep trying!")
}When grade is "B", this will print both "Good job!" and "Keep trying!" because of the fallthrough statement. Unlike languages like C, Go switch statements automatically exit after a matching case unless fallthrough is used.
Try it yourself
package main
import "fmt"
func main() {
// Read input
var alertLevel string
fmt.Scanln(&alertLevel)
// TODO: Write your code below
// Create a switch statement with fallthrough for the alert system
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Control Flow
Switch with `fallthrough`Breaking from Nested LoopsContinuing a Specific LoopThe `goto` StatementRecap - Advanced Loop Control4Project: Simple Task List
Project SetupAdding a Task2Structs and Methods
Defining Methods on StructsValue ReceiversPointer ReceiversChoosing ReceiversMethods vs FunctionsRecap - Struct Behavior5Maps In-Depth
Maps of StructsPointers as Map ValuesTesting for Nil MapsComparing MapsRecap - Word Frequency Counter3Interfaces (The Basics)
What is an Interface?Defining an InterfaceImplementing an InterfaceUsing Interface TypesEmpty InterfaceType AssertionsType SwitchRecap - Shapes and Behaviors