Challenge - 5 Problems
Loop Control Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of loop with break statement
What is the output of this Go program that uses a break statement inside a loop?
Go
package main import "fmt" func main() { for i := 1; i <= 5; i++ { if i == 3 { break } fmt.Print(i, " ") } }
Attempts:
2 left
๐ก Hint
The break statement stops the loop immediately when the condition is met.
โ Incorrect
The loop runs from 1 to 5, but when i equals 3, the break statement stops the loop. So only 1 and 2 are printed.
โ Predict Output
intermediate2:00remaining
Output of loop with continue statement
What will this Go program print when it uses continue inside a loop?
Go
package main import "fmt" func main() { for i := 1; i <= 5; i++ { if i == 3 { continue } fmt.Print(i, " ") } }
Attempts:
2 left
๐ก Hint
The continue statement skips the current loop iteration when the condition is true.
โ Incorrect
When i is 3, continue skips printing it and moves to the next iteration. So 3 is missing in the output.
๐ง Conceptual
advanced2:00remaining
Why is loop control important in programming?
Why do programmers use loop control statements like break and continue in loops?
Attempts:
2 left
๐ก Hint
Think about how loops can be controlled to avoid unnecessary work or infinite loops.
โ Incorrect
Loop control statements help stop loops early or skip certain steps, making programs more efficient and easier to manage.
โ Predict Output
advanced2:00remaining
Output of nested loops with break
What is the output of this Go program with nested loops and a break statement?
Go
package main import "fmt" func main() { for i := 1; i <= 3; i++ { for j := 1; j <= 3; j++ { if j == 2 { break } fmt.Printf("%d%d ", i, j) } } }
Attempts:
2 left
๐ก Hint
The break only stops the inner loop when j equals 2.
โ Incorrect
For each i, the inner loop prints j=1 then breaks when j=2, so only pairs with j=1 are printed.
โ Predict Output
expert2:00remaining
Value of counter after loop with continue and break
What is the value of variable count after running this Go program?
Go
package main import "fmt" func main() { count := 0 for i := 1; i <= 5; i++ { if i == 2 { continue } if i == 4 { break } count++ } fmt.Println(count) }
Attempts:
2 left
๐ก Hint
Count increments only when continue and break conditions are not met.
โ Incorrect
i=1 increments count (count=1), i=2 skips increment (continue), i=3 increments count (count=2), i=4 breaks loop, so final count is 2.