Recall & Review
beginner
What is the basic structure of a for loop in Go?
A for loop in Go has three parts separated by semicolons: initialization, condition, and post statement. It looks like: <br>
for i := 0; i < 5; i++ {<br> // code to repeat<br>}Click to reveal answer
beginner
How do you write an infinite loop using for in Go?
You write a for loop without any condition or statements: <br>
for {<br> // code runs forever<br>}Click to reveal answer
beginner
Can a for loop in Go be used like a while loop?
Yes! If you only provide a condition without initialization and post statements, it works like a while loop: <br>
for i < 10 {<br> // code<br>}Click to reveal answer
beginner
What happens if the condition in a for loop is false at the start?
The loop body does not run at all. The program skips the loop and continues after it.
Click to reveal answer
beginner
How do you stop a for loop early in Go?
Use the
break statement inside the loop to exit it immediately.Click to reveal answer
Which part of the for loop runs first in Go?
✗ Incorrect
The initialization runs first, then the condition is checked before running the loop body.
What does this loop do? <br>
for i := 0; i < 3; i++ { fmt.Println(i) }✗ Incorrect
The loop starts at 0 and runs while i is less than 3, printing 0, 1, and 2.
How do you write a loop that runs forever in Go?
✗ Incorrect
In Go, a bare `for` loop (`for {}`) runs forever.
What keyword stops a for loop immediately?
✗ Incorrect
break exits the loop immediately. continue skips to the next iteration.Which of these is a valid for loop condition in Go?
✗ Incorrect
Go uses := for initialization and semicolons to separate parts. Parentheses are not used.
Explain how a for loop works in Go and write a simple example that prints numbers 1 to 5.
Think about how you start, check, and update the loop variable.
You got /5 concepts.
Describe how to create an infinite loop in Go and how to stop it safely.
Remember the simplest for loop form and how to exit loops.
You got /3 concepts.