0
0
Goprogramming~10 mins

Why loop control is required 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 5 using a for loop.

Go
for i := 1; i <= [1]; i++ {
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
A5
B10
C0
D6
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 0 or 6 as the loop limit causes incorrect output or extra iterations.
2fill in blank
medium

Complete the code to skip printing the number 3 inside the loop.

Go
for i := 1; i <= 5; i++ {
    if i == [1] {
        continue
    }
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
A5
B4
C2
D3
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the wrong number in the if condition causes the wrong iteration to be skipped.
3fill in blank
hard

Fix the error in the loop to stop printing numbers when i equals 4.

Go
for i := 1; i <= 5; i++ {
    if i == [1] {
        break
    }
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
A4
B3
C6
D5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a number other than 4 causes the loop to stop too early or too late.
4fill in blank
hard

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

Go
for i := [1]; i <= 10; i += [2] {
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
A2
B1
D3
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Starting at 1 or increasing by 1 prints odd numbers or all numbers.
5fill in blank
hard

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

Go
for i := [1]; i [2] 1; i [3] {
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
A1
B>=
C--
D10
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using incorrect operators or starting point causes infinite loop or wrong output.