Challenge - 5 Problems
Infinite Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of a simple infinite loop with break
What is the output of this Go program?
Go
package main import "fmt" func main() { i := 0 for { if i == 3 { break } fmt.Print(i) i++ } }
Attempts:
2 left
๐ก Hint
Look at when the loop stops printing.
โ Incorrect
The loop prints i starting from 0. When i reaches 3, the break stops the loop before printing 3.
โ Predict Output
intermediate2:00remaining
Output of infinite loop with continue
What is the output of this Go program?
Go
package main import "fmt" func main() { i := 0 for { i++ if i%2 == 0 { continue } if i > 5 { break } fmt.Print(i) } }
Attempts:
2 left
๐ก Hint
Only odd numbers less than or equal to 5 are printed.
โ Incorrect
The loop increments i, skips even numbers with continue, prints odd numbers, and stops when i > 5.
๐ง Debug
advanced2:00remaining
Identify the cause of infinite loop
Why does this Go program run forever without stopping?
Go
package main import "fmt" func main() { i := 0 for i < 5 { fmt.Print(i) } }
Attempts:
2 left
๐ก Hint
Check if the loop variable changes inside the loop.
โ Incorrect
The variable i stays 0 forever, so the condition i < 5 is always true, causing an infinite loop.
๐ Syntax
advanced2:00remaining
Which option causes a compile error due to infinite loop syntax?
Which of these Go loop statements will cause a compile error?
Attempts:
2 left
๐ก Hint
Check if the for loop header has all three parts separated by semicolons.
โ Incorrect
Option A misses the increment part after the second semicolon, causing a syntax error.
๐ Application
expert2:00remaining
How many times does this infinite loop print?
How many times will the following Go program print "Hello" before stopping?
Go
package main import "fmt" func main() { count := 0 for { fmt.Println("Hello") count++ if count == 10 { break } } }
Attempts:
2 left
๐ก Hint
Look at how count changes and when the break happens.
โ Incorrect
The loop prints "Hello" and increments count each time. When count reaches 10, it breaks.