Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a loop that runs while i is less than 5.
Go
package main import "fmt" func main() { i := 0 for [1] { fmt.Println(i) i++ } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 'i > 5' which never runs the loop initially.
Using 'i == 5' which runs only when i equals 5.
โ Incorrect
The condition i < 5 makes the loop run while i is less than 5.
2fill in blank
mediumComplete the code to print numbers from 1 to 3 using a for loop as while.
Go
package main import "fmt" func main() { i := 1 for [1] { fmt.Println(i) i++ } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 'i < 3' which stops before printing 3.
Using 'i == 3' which runs only once when i equals 3.
โ Incorrect
The condition i <= 3 makes the loop run while i is 1, 2, and 3.
3fill in blank
hardFix the error in the loop condition to run while count is less than 4.
Go
package main import "fmt" func main() { count := 0 for [1] { fmt.Println(count) count++ } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 'count > 4' which never runs the loop initially.
Using 'count == 4' which runs only once when count equals 4.
โ Incorrect
The condition count < 4 runs the loop while count is 0, 1, 2, and 3.
4fill in blank
hardFill both blanks to create a loop that prints numbers from 2 to 6.
Go
package main import "fmt" func main() { num := 2 for [1] { fmt.Println(num) num [2] } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 'num < 6' which stops before printing 6.
Using '++num' which is invalid syntax in Go.
โ Incorrect
The condition num <= 6 keeps the loop running until 6. The increment num++ increases num by 1 each time.
5fill in blank
hardFill all three blanks to create a loop that prints even numbers from 0 to 4.
Go
package main import "fmt" func main() { i := 0 for [1] { fmt.Println(i) i [2] 2 } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using '<=' as an operator in the increment step.
Using '-=' which decreases i instead of increasing.
โ Incorrect
The condition i <= 4 runs the loop while i is 0, 2, and 4. The operator += adds 2 to i each time.