0
0
Goprogramming~10 mins

For loop basics in Go - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 0 to 4.

Go
for [1] := 0; i < 5; i++ {
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
Ai
Bj
Cx
Dn
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a variable name that doesn't match the condition or increment part.
Using a variable name not declared in the loop.
2fill in blank
medium

Complete the code to sum numbers from 1 to 5.

Go
sum := 0
for i := 1; i <= [1]; i++ {
    sum += i
}
fmt.Println(sum)
Drag options to blanks, or click blank then click option'
A4
B10
C6
D5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a limit less than 5, so the sum misses the last number.
Using a limit greater than 5, causing extra iterations.
3fill in blank
hard

Fix the error in the loop condition to print even numbers from 0 to 8.

Go
for i := 0; i [1] 10; i += 2 {
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
A>
B<
C>=
D<=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using greater than operator causing the loop to never run.
Using greater than or equal causing no output.
4fill in blank
hard

Fill both blanks to create a loop that prints numbers from 5 down to 1.

Go
for i := [1]; i [2] 0; i-- {
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
A5
B0
C>
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using less than operator causing the loop to never run.
Starting at 0 instead of 5.
5fill in blank
hard

Fill all three blanks to create a loop that prints squares of numbers from 1 to 3.

Go
for [1] := 1; [2] <= 3; [3]++ {
    fmt.Println(i * i)
}
Drag options to blanks, or click blank then click option'
Aj
Bi
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using different variable names causing errors.
Using a variable not declared in the loop.