0
0
Goprogramming~10 mins

Why loops are needed in Go - Test Your Understanding

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

Complete the code to print numbers from 1 to 3 using a loop.

Go
for i := 1; i <= [1]; i++ {
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
A5
B3
C0
D10
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a number too large or too small for the loop condition.
2fill in blank
medium

Complete the code to sum numbers from 1 to 4 using a loop.

Go
sum := 0
for i := 1; i <= 4; [1] {
    sum += i
}
fmt.Println(sum)
Drag options to blanks, or click blank then click option'
Ai++
Bi += 2
Ci = i
Di--
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using i-- which decreases the variable and causes an infinite loop.
3fill in blank
hard

Fix the error in the loop to print numbers from 1 to 5.

Go
for i := 1; i < [1]; i++ {
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
A5
B4
C6
D1
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using i < 5 which stops before printing 5.
4fill in blank
hard

Fill both blanks to create a loop that prints even numbers from 2 to 8.

Go
for i := [1]; i <= [2]; i += 2 {
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
A2
B1
C8
D10
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Starting at 1 or stopping before 8.
5fill in blank
hard

Fill all three blanks to create a loop that prints numbers from 10 down to 6.

Go
for i := [1]; i [2] [3]; i-- {
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
A10
B>=
C6
D<=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using <= instead of >= in the condition.