0
0
Goprogramming~10 mins

Infinite loops 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 an infinite loop that prints "Hello" repeatedly.

Go
package main

import "fmt"

func main() {
    for [1] {
        fmt.Println("Hello")
    }
}
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
Ci < 5
Di == 0
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a condition that becomes false eventually, so the loop stops.
Using false which never runs the loop.
2fill in blank
medium

Complete the code to create an infinite loop using the classic for loop syntax.

Go
package main

import "fmt"

func main() {
    for [1] {
        fmt.Println("Looping forever")
    }
}
Drag options to blanks, or click blank then click option'
Ai := 0; i < 10; i++
Bi == 0
Ci < 5
D;;
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Putting a condition that eventually becomes false.
Using a normal for loop with limits.
3fill in blank
hard

Fix the error in the infinite loop code so it compiles and runs correctly.

Go
package main

import "fmt"

func main() {
    for [1] {
        fmt.Println("Running")
    }
}
Drag options to blanks, or click blank then click option'
A1 == 1
BTrue
Ctrue
Dfor
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using uppercase True instead of lowercase true.
Putting for inside the loop condition.
4fill in blank
hard

Fill both blanks to create an infinite loop that prints numbers starting from 1.

Go
package main

import "fmt"

func main() {
    i := 1
    for [1] {
        fmt.Println(i)
        i[2]
    }
}
Drag options to blanks, or click blank then click option'
Atrue
B++
C+=
D--
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using ++ which is a statement, not an expression.
Using -- which decreases the number.
5fill in blank
hard

Fill all three blanks to create an infinite loop that prints "Go!" every second.

Go
package main

import (
    "fmt"
    "time"
)

func main() {
    for [1] {
        fmt.Println([2])
        time.Sleep([3])
    }
}
Drag options to blanks, or click blank then click option'
Atrue
B"Go!"
Ctime.Second
Dfalse
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using false which stops the loop immediately.
Forgetting to import time package.
Using a string without quotes.