0
0
Goprogramming~10 mins

For loop as while 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 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'
Ai < 5
Bi <= 5
Ci > 5
Di == 5
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.
2fill in blank
medium

Complete 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'
Ai < 3
Bi <= 3
Ci >= 3
Di == 3
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.
3fill in blank
hard

Fix 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'
Acount >= 4
Bcount > 4
Ccount < 4
Dcount == 4
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.
4fill in blank
hard

Fill 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'
Anum <= 6
Bnum < 6
C++
D++num
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'num < 6' which stops before printing 6.
Using '++num' which is invalid syntax in Go.
5fill in blank
hard

Fill 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'
Ai <= 4
B+=
C-=
D<=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<=' as an operator in the increment step.
Using '-=' which decreases i instead of increasing.