0
0
Goprogramming~10 mins

Loop execution flow 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 1 to 5 using a for loop.

Go
for i := 1; i [1] 5; i++ {
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
A<
B>
C<=
D>=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' will stop before printing 5.
Using '>' or '>=' will cause the loop not to run.
2fill in blank
medium

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

Go
sum := 0
for i := 1; i [1] 10; i++ {
    sum += i
}
fmt.Println(sum)
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' excludes 10 from the sum.
Using '>' or '>=' will not run the loop correctly.
3fill in blank
hard

Fix the error in the loop condition to print even numbers from 2 to 10.

Go
for i := 2; 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 '<' excludes 10 from printing.
Using '>' or '>=' will cause the loop not to run.
4fill in blank
hard

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

Go
for i := 10; i [1] 1; 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 '<=' will cause the loop to never run.
Using '++' will increase i and cause an infinite loop.
5fill in blank
hard

Fill all three blanks to create a loop that prints only odd numbers from 1 to 9.

Go
for i := [1]; i [2] 9; i [3] {
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
A1
B<=
Ci += 2
D0
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Starting at 0 will print even numbers.
Using '<' excludes 9 from printing.
Incrementing by 1 prints all numbers, not just odd.