0
0
Goprogramming~10 mins

Break statement 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 exit the loop when i equals 3.

Go
for i := 1; i <= 5; i++ {
    if i == 3 {
        [1]
    }
    fmt.Println(i)
}
Drag options to blanks, or click blank then click option'
Abreak
Breturn
Ccontinue
Dexit
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'continue' instead of 'break' causes the loop to skip the current iteration but not exit.
Using 'return' inside the loop exits the function, not just the loop.
2fill in blank
medium

Complete the code to stop the loop when the character 'x' is found in the string.

Go
str := "golang"
for _, ch := range str {
    if ch == '[1]' {
        break
    }
    fmt.Printf("%c", ch)
}
Drag options to blanks, or click blank then click option'
Ax
Bo
Ca
Dg
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Choosing a character that is in the string but not intended to stop the loop.
Using double quotes instead of single quotes for a rune.
3fill in blank
hard

Fix the error in the loop to break when the number is greater than 10.

Go
nums := []int{5, 8, 12, 3}
for _, num := range nums {
    if num [1] 10 {
        break
    }
    fmt.Println(num)
}
Drag options to blanks, or click blank then click option'
A<=
B<
C==
D>
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<=' breaks the loop too early or too late.
Using '==' only breaks if the number equals 10 exactly.
4fill in blank
hard

Fill both blanks to create a map of squares for numbers greater than 2.

Go
squares := map[int]int{}
for i := 1; i <= 5; i++ {
    if i [1] 2 {
        squares[i] = i [2] i
    }
}
Drag options to blanks, or click blank then click option'
A>
B<
C**
D*
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' instead of '>' causes wrong numbers to be squared.
Using '**' is invalid in Go for exponentiation.
5fill in blank
hard

Fill the blanks to create a map of uppercase keys and values greater than 0.

Go
import "strings"
data := map[string]int{"a": 1, "b": 0, "c": 3}
result := map[string]int{}
for k, v := range data {
    if v [1] 0 {
        result[[2]] = v
    }
}

fmt.Println(result)
Drag options to blanks, or click blank then click option'
A>=
B>
Ck
Dstrings.ToUpper(k)
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '>=' includes zero values, which is not intended.
Using 'k' as key keeps keys lowercase.