0
0
Goprogramming~20 mins

Continue statement in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go Continue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of loop with continue skipping even numbers
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    for i := 1; i <= 5; i++ {
        if i%2 == 0 {
            continue
        }
        fmt.Print(i, " ")
    }
}
A1 3 5
B2 4
C1 2 3 4 5
D1 3 4 5
Attempts:
2 left
๐Ÿ’ก Hint
The continue statement skips the rest of the loop body for even numbers.
โ“ Predict Output
intermediate
2:00remaining
Continue statement inside nested loops
What will this Go program print?
Go
package main
import "fmt"
func main() {
    for i := 1; i <= 2; i++ {
        for j := 1; j <= 3; j++ {
            if j == 2 {
                continue
            }
            fmt.Printf("%d%d ", i, j)
        }
    }
}
A11 21 31
B11 13 21 23
C12 22
D11 12 13 21 22 23
Attempts:
2 left
๐Ÿ’ก Hint
Continue skips printing when j equals 2, but only for the inner loop.
โ“ Predict Output
advanced
2:00remaining
Effect of continue in a range loop over a slice
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    nums := []int{1, 2, 3, 4, 5}
    sum := 0
    for _, n := range nums {
        if n%2 == 0 {
            continue
        }
        sum += n
    }
    fmt.Println(sum)
}
A9
B10
C15
D7
Attempts:
2 left
๐Ÿ’ก Hint
Only odd numbers are added to sum because continue skips even numbers.
โ“ Predict Output
advanced
2:00remaining
Continue statement with label in nested loops
What will this Go program print?
Go
package main
import "fmt"
func main() {
Outer:
    for i := 1; i <= 3; i++ {
        for j := 1; j <= 3; j++ {
            if j == 2 {
                continue Outer
            }
            fmt.Printf("%d%d ", i, j)
        }
    }
}
A11 31
B11 12 13 21 22 23 31 32 33
C11 13 21 23 31 33
D11 21 31
Attempts:
2 left
๐Ÿ’ก Hint
Continue with label skips to next iteration of the outer loop when j == 2.
๐Ÿง  Conceptual
expert
1:30remaining
Why use continue statement in Go loops?
Which of the following best explains the purpose of the continue statement in Go loops?
AIt pauses the loop execution until a condition is met.
BIt immediately exits the entire loop and continues execution after the loop.
CIt skips the remaining statements in the current iteration and moves to the next iteration of the loop.
DIt restarts the loop from the first iteration regardless of the current position.
Attempts:
2 left
๐Ÿ’ก Hint
Think about what happens when you want to skip some work but keep looping.