The `break` Keyword
Part of the Fundamentals section of Coddy's GO journey — lesson 47 of 109.
The break keyword allows us to immediately exit a loop, even if the loop condition would normally continue running.
Let's create a program that counts from 1 to 10 but stops when it reaches 5:
for i := 1; i <= 10; i++ {
if i > 5 {
break
}
fmt.Println(i)
}
fmt.Println("Loop exited")After executing this code, the numbers 1 through 5 will show on the screen, followed by the message "Loop exited":
1
2
3
4
5
Loop exitedThe break statement causes the loop to immediately stop when i becomes greater than 5. This means the loop never prints the numbers 6 through 10. Instead, it jumps directly to the code after the loop and prints "Loop exited".
Challenge
EasyIn this challenge, you'll practice using the break keyword to exit a loop early.
You have a sequence of numbers from 1 to 10. Your task is to find the first number that is divisible by 3, print it, and then immediately exit the loop using break.
This way, once you find what you're looking for, you don't waste time continuing to search through the rest of the numbers.
Cheat sheet
The break keyword allows you to immediately exit a loop, even if the loop condition would normally continue running.
for i := 1; i <= 10; i++ {
if i > 5 {
break
}
fmt.Println(i)
}
fmt.Println("Loop exited")When break is executed, the loop stops immediately and jumps to the code after the loop.
Try it yourself
package main
import "fmt"
func main() {
// Loop through numbers 1 to 10
for i := 1; i <= 10; i++ {
fmt.Printf("Checking number: %d\n", i)
// TODO: Check if the current number is divisible by 3
// If it is, print "Found it: [number]!" and use break to exit the loop
}
fmt.Println("Search complete")
}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