Challenge - 5 Problems
For Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of a simple for loop with continue
What is the output of this Go program?
Go
package main import "fmt" func main() { for i := 0; i < 5; i++ { if i == 2 { continue } fmt.Print(i) } }
Attempts:
2 left
๐ก Hint
Remember that continue skips the current loop iteration.
โ Incorrect
The loop prints numbers from 0 to 4 but skips printing 2 because of continue.
โ Predict Output
intermediate2:00remaining
For loop with range over slice
What will this Go program print?
Go
package main import "fmt" func main() { nums := []int{10, 20, 30} sum := 0 for _, v := range nums { sum += v } fmt.Println(sum) }
Attempts:
2 left
๐ก Hint
The loop adds all numbers in the slice.
โ Incorrect
The sum of 10 + 20 + 30 is 60, so the program prints 60.
๐ง Debug
advanced2:00remaining
Identify the error in this for loop
What error will this Go code produce when compiled?
Go
package main func main() { for i := 0; i < 5; i++ { println(i) } }
Attempts:
2 left
๐ก Hint
Check the syntax rules for for loops in Go.
โ Incorrect
Go requires braces {} around the loop body; missing them causes a syntax error.
โ Predict Output
advanced2:00remaining
Nested for loops output
What does this Go program print?
Go
package main import "fmt" func main() { for i := 1; i <= 2; i++ { for j := 1; j <= 3; j++ { fmt.Print(i * j) } } }
Attempts:
2 left
๐ก Hint
Multiply i and j for each iteration and print without spaces.
โ Incorrect
First outer loop i=1: prints 1*1=1,1*2=2,1*3=3 โ 123; second i=2: 2*1=2,2*2=4,2*3=6 โ 246; combined: 123246.
โ Predict Output
expert2:00remaining
For loop with break and label
What is the output of this Go program?
Go
package main import "fmt" func main() { Outer: for i := 1; i <= 3; i++ { for j := 1; j <= 3; j++ { if i*j >= 4 { break Outer } fmt.Print(i, j, " ") } } }
Attempts:
2 left
๐ก Hint
The break with label stops both loops when i*j >= 4.
โ Incorrect
Prints for (1,1):11 , (1,2):12 , (1,3):13 , (2,1):21 then at i=2 j=2 (4>=4) breaks Outer without printing more.