Menu
Coddy logo textTech

The `else` Keyword

Part of the Fundamentals section of Coddy's GO journey — lesson 35 of 109.

The else keyword lets you specify code to run when the if condition is false. It provides an alternative execution path.

Let's create a program that checks a person's age and prints different messages based on whether they're an adult or not:

age := 15
    
if age >= 18 {
    fmt.Println("You are an adult")
} else {
    fmt.Println("You are a minor")
}

After executing this code, the program checks if age is greater than or equal to 18. Since 15 is less than 18, the condition is false, so it executes the code in the else block. The message "You are a minor" will show on the screen.

The else statement always follows an if statement and its code block runs only when the if condition evaluates to false. This gives us a way to handle both possible outcomes of a condition.

challenge icon

Challenge

Beginner
In this challenge, you'll practice using the else keyword in Go. We have a variable isRaining that indicates whether it's raining outside. Your task is to complete the code to print a message based on this condition. If it's raining, the program should print "Bring an umbrella!". Otherwise, it should print "Enjoy the sunshine!".

Cheat sheet

The else keyword lets you specify code to run when the if condition is false. It provides an alternative execution path.

age := 15
    
if age >= 18 {
    fmt.Println("You are an adult")
} else {
    fmt.Println("You are a minor")
}

The else statement always follows an if statement and its code block runs only when the if condition evaluates to false.

Try it yourself

package main

import "fmt"

func main() {
	// This variable tells us if it's raining outside
	isRaining := true
	
	// TODO: Complete the if-else statement below
	if isRaining {
		fmt.Println("Bring an umbrella!")
	}
	// Add your else statement here
	
}
quiz iconTest yourself

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

All lessons in Fundamentals