The `fallthrough` Keyword
Part of the Fundamentals section of Coddy's GO journey — lesson 42 of 109.
In Go, the fallthrough keyword allows us to continue execution to the next case in a switch statement, even after a match is found. Let's explore how this works.
First, let's create a simple switch statement that uses the fallthrough keyword to describe a number:
num := 15
switch {
case num < 20:
fmt.Print("Less than 20 ")
fallthrough
case num > 10:
fmt.Print("Greater than 10 ")
fallthrough
case num > 0:
fmt.Print("Positive number")
}After executing this code, we'll see all three messages printed on the screen:
Less than 20 Greater than 10 Positive numberThis happens because the first case matches (15 is less than 20), and the fallthrough keyword forces Go to also execute the second case. Then the second case also uses fallthrough, which makes Go execute the third case as well. Without fallthrough, only the first matching case would execute.
Remember that fallthrough will execute the next case regardless of whether its condition is true or not. In our example, all conditions happen to be true for the number 15, but the next case would execute even if its condition was false.
Challenge
BeginnerIn this challenge, you'll practice using the fallthrough keyword in a switch statement. The fallthrough keyword allows execution to continue to the next case even after a match is found.
You have a variable representing a day of the week. Your task is to add the fallthrough keyword in the appropriate place to make the program print both 'It's a weekday!' and 'Time to work!' when the day is 'Monday'.
Cheat sheet
The fallthrough keyword allows execution to continue to the next case in a switch statement, even after a match is found:
num := 15
switch {
case num < 20:
fmt.Print("Less than 20 ")
fallthrough
case num > 10:
fmt.Print("Greater than 10 ")
fallthrough
case num > 0:
fmt.Print("Positive number")
}This will output: Less than 20 Greater than 10 Positive number
fallthrough executes the next case regardless of whether its condition is true or false.
Try it yourself
package main
import "fmt"
func main() {
// A variable representing a day of the week
day := "Monday"
// Switch statement to check the day
switch day {
case "Monday":
fmt.Println("It's a weekday!")
// TODO: Add the fallthrough keyword here to continue to the next case
case "Tuesday", "Wednesday", "Thursday", "Friday":
fmt.Println("Time to work!")
case "Saturday", "Sunday":
fmt.Println("It's the weekend!")
default:
fmt.Println("Invalid day!")
}
}
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