Challenge - 5 Problems
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of a simple for loop in Go
What is the output of this Go program?
Go
package main import "fmt" func main() { for i := 1; i <= 3; i++ { fmt.Print(i, " ") } }
Attempts:
2 left
๐ก Hint
Look at the loop start and end values and what is printed inside the loop.
โ Incorrect
The loop starts at 1 and runs while i is less than or equal to 3, printing i and a space each time. So it prints '1 2 3 '.
๐ง Conceptual
intermediate1:30remaining
Why use loops instead of repeating code?
Why do programmers use loops instead of writing the same code many times?
Attempts:
2 left
๐ก Hint
Think about what happens if you want to do the same thing many times.
โ Incorrect
Loops help repeat tasks without copying code multiple times, making programs shorter and easier to change.
โ Predict Output
advanced2:30remaining
Output of nested loops in Go
What does this Go program print?
Go
package main import "fmt" func main() { for i := 1; i <= 2; i++ { for j := 1; j <= 2; j++ { fmt.Print(i*j, " ") } } }
Attempts:
2 left
๐ก Hint
Multiply i and j for each pair in the nested loops.
โ Incorrect
The outer loop runs i=1,2. For each i, inner loop runs j=1,2. The products are 1*1=1, 1*2=2, 2*1=2, 2*2=4 printed in order.
๐ง Debug
advanced2:00remaining
Identify the error in this Go loop
What error does this Go code cause?
Go
package main import "fmt" func main() { for i := 0; i < 3; i++ fmt.Println(i) }
Attempts:
2 left
๐ก Hint
Check if the loop body is properly enclosed.
โ Incorrect
In Go, the for loop body must be inside braces {}. Without them, the code causes a syntax error.
๐ Application
expert2:30remaining
Count how many times a loop runs
How many times does this loop run?
Go
package main import "fmt" func main() { count := 0 for i := 10; i > 0; i -= 3 { count++ } fmt.Println(count) }
Attempts:
2 left
๐ก Hint
Start at 10 and subtract 3 each time until i is no longer greater than 0.
โ Incorrect
The loop runs while i > 0: i=10,7,4,1 then stops when i= -2. So it runs 4 times.