0
0
Goprogramming~5 mins

Continue statement in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the continue statement do in a Go loop?
It skips the rest of the current loop iteration and moves to the next iteration immediately.
Click to reveal answer
beginner
In which types of loops can you use the continue statement in Go?
You can use continue in for loops, including traditional, range-based, and infinite loops.
Click to reveal answer
intermediate
What happens if you use continue inside a nested loop in Go?
The continue statement affects only the innermost loop where it is called, skipping to the next iteration of that loop.
Click to reveal answer
beginner
Example: What will be printed by this Go code?<br>
for i := 1; i <= 5; i++ {
  if i == 3 {
    continue
  }
  fmt.Println(i)
}
The numbers 1, 2, 4, and 5 will be printed each on a new line. The number 3 is skipped because of the continue.
Click to reveal answer
advanced
How can you use continue with a label in Go?
You can use continue with a label to skip to the next iteration of an outer loop by writing continue labelName.
Click to reveal answer
What does the continue statement do inside a Go for loop?
ASkips the rest of the current iteration and starts the next iteration
BExits the loop completely
CPauses the loop until user input
DRestarts the entire program
In Go, where can you NOT use the continue statement?
AInside a nested <code>for</code> loop
BInside a <code>for</code> loop
CInside a <code>switch</code> statement
DInside a range loop
What will happen if continue is used inside the inner loop of nested loops without a label?
AIt causes a compile error
BIt skips to the next iteration of the inner loop
CIt exits both loops
DIt skips to the next iteration of the outer loop
How do you skip the rest of the current iteration of an outer loop in Go?
AUse <code>continue</code> with the outer loop's label
BUse <code>break</code> with the outer loop's label
CUse <code>goto</code> statement
DUse <code>return</code> statement
What will this code print?<br>
for i := 1; i <= 3; i++ {
  for j := 1; j <= 3; j++ {
    if j == 2 {
      continue
    }
    fmt.Printf("%d,%d\n", i, j)
  }
}
AOnly pairs where j is 2
BNo output
COnly pairs where i is 2
DAll pairs except those where j is 2
Explain how the continue statement works in Go loops and give an example.
Think about how to skip some steps in a loop without stopping it.
You got /3 concepts.
    Describe how to use labeled continue in nested loops in Go and why it might be useful.
    Labels help control which loop to continue in nested loops.
    You got /3 concepts.