The `continue` Keyword
Part of the Fundamentals section of Coddy's GO journey — lesson 48 of 109.
The continue keyword allows us to skip the current iteration of a loop and move directly to the next iteration. This is useful when we want to skip certain values in our loop processing.
Let's create a program that prints only odd numbers from 1 to 10:
for i := 1; i <= 10; i++ {
if i%2 == 0 {
continue
}
fmt.Println(i)
}After executing this code, only the odd numbers will show on the screen:
1
3
5
7
9What happened here? Our loop runs from 1 to 10. For each number, we check if it's even by using i%2 == 0 (which tests if dividing by 2 leaves no remainder).
When we find an even number, the continue statement immediately jumps to the next iteration, skipping the fmt.Println(i) line. This is why only odd numbers appear in our output.
Challenge
Beginnercontinue keyword in a for loop. The program should print all numbers from 1 to 10, except for numbers that are divisible by 3 (3, 6, 9). Use the continue keyword to skip these numbers.Cheat sheet
The continue keyword skips the current iteration of a loop and moves directly to the next iteration.
Example - printing only odd numbers from 1 to 10:
for i := 1; i <= 10; i++ {
if i%2 == 0 {
continue
}
fmt.Println(i)
}When the condition is met, continue skips the remaining code in the current iteration and jumps to the next loop iteration.
Try it yourself
package main
import "fmt"
func main() {
// Loop through numbers 1 to 10
for i := 1; i <= 10; i++ {
// TODO: Use the continue keyword to skip numbers that are divisible by 3
// Hint: Use the modulo operator (%) to check if i is divisible by 3
fmt.Println(i)
}
}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