0
0
Goprogramming~20 mins

Why loop control is required in Go - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Loop Control Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2: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, " ")
    }
}
A1 2
B1 2 3
C1 2 3 4 5
D3 4 5
Attempts:
2 left
๐Ÿ’ก Hint
The break statement stops the loop immediately when the condition is met.
โ“ Predict Output
intermediate
2: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, " ")
    }
}
A1 2 4 5
B1 2 3 4 5
C3
D1 2
Attempts:
2 left
๐Ÿ’ก Hint
The continue statement skips the current loop iteration when the condition is true.
๐Ÿง  Conceptual
advanced
2:00remaining
Why is loop control important in programming?
Why do programmers use loop control statements like break and continue in loops?
ATo print output faster without delays
BTo stop or skip parts of the loop based on conditions, improving efficiency and control
CTo create new variables inside the loop automatically
DTo make the loop run forever without stopping
Attempts:
2 left
๐Ÿ’ก Hint
Think about how loops can be controlled to avoid unnecessary work or infinite loops.
โ“ Predict Output
advanced
2: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)
        }
    }
}
A11 12 21 22 31 32
B11 21 22 31 32
C11 12 13 21 22 23 31 32 33
D11 21 31
Attempts:
2 left
๐Ÿ’ก Hint
The break only stops the inner loop when j equals 2.
โ“ Predict Output
expert
2: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)
}
A4
B3
C2
D5
Attempts:
2 left
๐Ÿ’ก Hint
Count increments only when continue and break conditions are not met.