For Loop - Condition Only
Part of the Fundamentals section of Coddy's GO journey — lesson 46 of 109.
Let's learn about the condition-only for loop in Go. This type of loop works like a while loop in other languages, using just a condition without initialization or post statements.
First, let's create a simple counter program that counts from 1 to 3:
i := 1
for i <= 3 {
fmt.Println(i)
i++
}After executing this code, the numbers 1, 2, and 3 will show on the screen one after another:
1
2
3The loop works by checking if i is less than or equal to 3 before each iteration. Inside the loop, we print the current value of i and then increase it by 1 with i++. The loop continues running as long as the condition i <= 3 remains true, and stops when i becomes 4.
Unlike the standard for loop, we need to manually increment our counter variable inside the loop body, otherwise we'd create an infinite loop.
Challenge
BeginnerIn this challenge, you'll practice using a for loop with only a condition (no initialization or post statement). This is similar to a while loop in other languages.
We've provided a boolean variable keepRunning set to true and a counter variable count set to 0. Your task is to complete the for loop that will print numbers from 0 to 4 and then stop.
Inside the loop, we increment count and check if it reaches 5. When it does, we set keepRunning to false to exit the loop.
Cheat sheet
The condition-only for loop in Go works like a while loop, using just a condition without initialization or post statements:
i := 1
for i <= 3 {
fmt.Println(i)
i++
}The loop checks the condition before each iteration and continues as long as it remains true. You must manually increment the counter variable inside the loop body to avoid infinite loops.
Try it yourself
package main
import "fmt"
func main() {
// These variables are already set up for you
keepRunning := true
count := 0
// TODO: Complete the for loop that uses only a condition (like a while loop)
// The loop should continue as long as keepRunning is true
for {
// Print the current count
fmt.Println(count)
// Increment count
count++
// Check if count is 5, and if so, set keepRunning to false
if count >= 5 {
keepRunning = false
}
}
}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