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
Beginnerelse 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
}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