Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
โ Incorrect
The variable i is commonly used as the loop counter in Go for loops.
2fill in blank
mediumComplete 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'
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.
โ Incorrect
The loop should run until i is less than or equal to 5 to include 5 in the sum.
3fill in blank
hardFix 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'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using greater than operator causing the loop to never run.
Using greater than or equal causing no output.
โ Incorrect
The loop should continue while i is less than 10 to print numbers up to 8.
4fill in blank
hardFill 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'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using less than operator causing the loop to never run.
Starting at 0 instead of 5.
โ Incorrect
The loop starts at 5 and continues while i is greater than 0, decreasing by 1 each time.
5fill in blank
hardFill 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'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using different variable names causing errors.
Using a variable not declared in the loop.
โ Incorrect
The loop variable i is used consistently in initialization, condition, and increment.