Infinite loops run forever until you stop them. They are useful when you want a program to keep working without stopping.
Infinite loops in Go
Introduction
Syntax
Go
for { // code to repeat forever }
The for keyword with empty conditions creates an infinite loop.
You can stop the loop using break or by stopping the program.
Examples
break.Go
for { println("Hello") break // stops after one print }
Go
for { // do something forever }
Sample Program
This program runs an infinite loop but stops after counting to 3 using break. It prints the count every half second.
Go
package main import ( "fmt" "time" ) func main() { count := 0 for { fmt.Println("Loop count:", count) count++ if count >= 3 { fmt.Println("Stopping loop") break } time.Sleep(500 * time.Millisecond) // wait half a second } }
Important Notes
Infinite loops can freeze your program if you don't stop them properly.
Use break inside the loop to exit when needed.
Adding a small delay like time.Sleep can prevent high CPU usage.
Summary
Infinite loops run forever unless stopped.
Use for { } in Go to create an infinite loop.
Always plan how to stop or control infinite loops to avoid problems.
Practice
1. What does the following Go code do?
for { }easy
Solution
Step 1: Understand the loop syntax
The code usesfor { }which is Go's way to write an infinite loop without conditions.Step 2: Analyze loop behavior
Since there is no break or condition, the loop runs forever without stopping.Final Answer:
Runs an infinite loop that never stops -> Option AQuick Check:
for { } means infinite loop [OK]
Hint: for { } always means infinite loop in Go [OK]
Common Mistakes:
- Thinking it runs a fixed number of times
- Assuming it causes a syntax error
- Confusing with loops that have conditions
2. Which of the following is the correct syntax for an infinite loop in Go?
easy
Solution
Step 1: Review Go loop syntax
Go does not have awhileorloopkeyword; infinite loops usefor { }.Step 2: Identify correct infinite loop
for { } usesfor { }, which is the correct infinite loop syntax in Go.Final Answer:
for { } -> Option CQuick Check:
Go infinite loop syntax = for { } [OK]
Hint: Remember: Go uses for { } for infinite loops [OK]
Common Mistakes:
- Using while or loop keywords (not in Go)
- Confusing for loops with conditions as infinite
- Syntax errors from invalid keywords
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
count := 0
for {
if count == 3 {
break
}
fmt.Println(count)
count++
}
}medium
Solution
Step 1: Understand loop and break condition
The loop runs infinitely but breaks when count equals 3.Step 2: Trace output values
It prints count values 0, 1, 2 then breaks before printing 3.Final Answer:
0 1 2 -> Option AQuick Check:
Loop breaks at count==3, prints 0 to 2 [OK]
Hint: Break stops infinite loop at condition [OK]
Common Mistakes:
- Expecting 3 to be printed
- Thinking loop runs forever
- Ignoring break statement
4. Identify the problem in this Go code that causes an infinite loop and fix it:
package main
import "fmt"
func main() {
i := 0
for i < 5 {
fmt.Println(i)
}
}medium
Solution
Step 1: Identify loop condition and variable
The loop runs while i < 5 but i is never changed inside the loop.Step 2: Fix infinite loop by incrementing i
Addingi++inside the loop updates i, allowing the loop to end.Final Answer:
Add i++ inside the loop to increment i -> Option DQuick Check:
Increment loop variable to avoid infinite loop [OK]
Hint: Always update loop variable to prevent infinite loops [OK]
Common Mistakes:
- Forgetting to increment loop variable
- Adding break without changing i causes infinite loop
- Changing loop to infinite loop syntax accidentally
5. How can you safely implement an infinite loop in Go that stops after 5 seconds?
hard
Solution
Step 1: Understand infinite loop with timeout
Usefor { }for infinite loop andtime.Afterto wait 5 seconds.Step 2: Break loop after 5 seconds
Inside loop, check if 5 seconds passed and usebreakto stop loop safely.Final Answer:
Use for { } with time.After and break when 5 seconds pass -> Option BQuick Check:
Infinite loop + timer + break = safe stop [OK]
Hint: Combine for { } with timer and break to stop [OK]
Common Mistakes:
- Using invalid while syntax
- Not breaking loop causes infinite run
- Using fixed count loop instead of infinite
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
