Menu
Coddy logo textTech

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 Adult

Notice 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 icon

Challenge

Easy

In 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!")
	}
}
quiz iconTest yourself

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

All lessons in Fundamentals