What The `for` Loop Explained
Part of the Fundamentals section of Coddy's GO journey — lesson 44 of 109.
The for loop is Go's only looping construct, used to repeat a block of code multiple times. Let's explore how it works step by step.
First, let's create a simple program that counts from 1 to 5 using a for loop:
for i := 1; i <= 5; i++ {
fmt.Println(i)
}After executing this code, the numbers 1 through 5 will show on the screen, one per line:
1
2
3
4
5Let's understand what's happening in our for loop:
The for loop has three main parts:
1. i := 1 - This is the initialization part where we create a variable and set its starting value
2. i <= 5 - This is the condition that's checked before each loop iteration; the loop continues as long as this is true
3. i++ - This is the post statement that runs after each loop iteration (it adds 1 to i)
The loop works by first initializing i to 1, then checking if i is less or equal to 5. If true, it runs the code inside the loop (printing the value), then executes i++ to increase the value by 1. This process repeats until the condition becomes false (when i becomes 6).
Challenge
BeginnerIn this challenge, you'll practice using a basic for loop to count and print numbers.
Your task is to write a for loop that counts from 1 to 10 and prints each number on a new line.
This will help you understand how to use the for loop for simple counting tasks.
Cheat sheet
The for loop is Go's only looping construct, used to repeat a block of code multiple times.
Basic for loop syntax:
for i := 1; i <= 5; i++ {
fmt.Println(i)
}The for loop has three main parts:
1. i := 1 - Initialization: creates a variable and sets its starting value
2. i <= 5 - Condition: checked before each iteration; loop continues while true
3. i++ - Post statement: runs after each iteration (increments i by 1)
Try it yourself
package main
import "fmt"
func main() {
// TODO: Write a for loop that counts from 1 to 10
// and prints each number on a new line
}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