Switch without Expression
Part of the Fundamentals section of Coddy's GO journey — lesson 41 of 109.
In Go, we can use a switch statement without an expression after the keyword. This is useful when we want to evaluate multiple conditions in a clean way.
Let's create a program that categorizes a person based on their age:
age := 25
switch {
case age < 13:
fmt.Println("Child")
case age < 20:
fmt.Println("Teenager")
case age < 30:
fmt.Println("Young Adult")
default:
fmt.Println("Adult")
}After executing this code, the program will check each case condition in order. Since our age is 25, it's not less than 13 or 20, but it is less than 30, so the third case matches. The text "Young Adult" will show on the screen:
Young AdultNotice that without an expression after switch, each case becomes a boolean condition that evaluates to either true or false. The first case that evaluates to true will execute its code block.
If none of the cases match, the default case would execute. In our example, if age was 35, the output would be "Adult".
Challenge
EasyIn this challenge, you'll practice using a switch statement without an expression. This type of switch is useful when you want to evaluate multiple conditions without repeating if-else statements.
We've provided a variable timeOfDay that represents the current hour (in 24-hour format). Your task is to complete the switch statement without an expression to print an appropriate greeting based on the time of day.
Cheat sheet
A switch statement without an expression allows you to evaluate multiple conditions cleanly:
age := 25
switch {
case age < 13:
fmt.Println("Child")
case age < 20:
fmt.Println("Teenager")
case age < 30:
fmt.Println("Young Adult")
default:
fmt.Println("Adult")
}Each case becomes a boolean condition that evaluates to true or false. The first case that evaluates to true executes its code block. If no cases match, the default case executes.
Try it yourself
package main
import "fmt"
func main() {
// This represents the current hour in 24-hour format (0-23)
timeOfDay := 14 // This is 2:00 PM
// TODO: Complete the switch statement without an expression
// to print the appropriate greeting based on the time of day
switch {
// Add your cases here
default:
fmt.Println("Hello!")
}
}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