Recall & Review
beginner
What is an infinite loop in Go?
An infinite loop is a loop that never stops running because its exit condition is never met or it has no exit condition.
Click to reveal answer
beginner
How do you write a simple infinite loop in Go?
You can write an infinite loop using
for {} without any condition inside the braces.Click to reveal answer
beginner
Why should you be careful with infinite loops?
Because they can cause your program to freeze or use too much CPU if not controlled or stopped properly.
Click to reveal answer
intermediate
How can you stop an infinite loop in Go?
You can use a
break statement inside the loop when a certain condition is met to exit the loop.Click to reveal answer
beginner
What is the output of this Go code?<br>
for {
fmt.Println("Hello")
break
}The output is:<br>
Hello<br>The loop prints "Hello" once and then stops because of the
break.Click to reveal answer
Which of the following is a correct infinite loop in Go?
✗ Incorrect
In Go,
for {} creates an infinite loop. There is no while or loop keyword.What keyword can you use to exit an infinite loop in Go?
✗ Incorrect
The
break keyword stops the nearest enclosing loop immediately.What happens if an infinite loop has no
break or exit condition?✗ Incorrect
Without a way to stop, the loop runs forever, which can freeze or slow down the program.
Which of these is NOT a reason to use an infinite loop?
✗ Incorrect
Calculating a fixed sum once does not need an infinite loop; infinite loops are for repeated or continuous tasks.
How can you safely use an infinite loop in Go?
✗ Incorrect
Using
for {} with a break lets you control when to stop the loop safely.Explain what an infinite loop is and how to create one in Go.
Think about a loop that never ends unless you tell it to stop.
You got /3 concepts.
Describe how to safely stop an infinite loop in Go and why it is important.
Consider what happens if a loop never stops and how to prevent that.
You got /3 concepts.