0
0
Goprogramming~10 mins

If–else 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 print "Even" if the number is divisible by 2.

Go
package main
import "fmt"
func main() {
    num := 4
    if num [1] 2 == 0 {
        fmt.Println("Even")
    }
}
Drag options to blanks, or click blank then click option'
A/
B*
C+
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using division operator '/' instead of remainder operator '%'.
Using multiplication '*' or addition '+' operators incorrectly.
2fill in blank
medium

Complete the code to print "Positive" if the number is greater than zero.

Go
package main
import "fmt"
func main() {
    num := 10
    if num [1] 0 {
        fmt.Println("Positive")
    }
}
Drag options to blanks, or click blank then click option'
A==
B<
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which checks if number is less than zero.
Using '==' which checks for equality.
3fill in blank
hard

Fix the error in the if–else statement to print "Odd" when the number is not divisible by 2.

Go
package main
import "fmt"
func main() {
    num := 7
    if num % 2 == 0 {
        fmt.Println("Even")
    } [1] {
        fmt.Println("Odd")
    }
}
Drag options to blanks, or click blank then click option'
Aelse
Belseif
Celse if
Delif
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'elseif' or 'else if' which are not valid in Go.
Using 'elif' which is from other languages.
4fill in blank
hard

Complete the code to check if a number is negative or zero and print the correct message.

Go
package main
import "fmt"
func main() {
    num := -3
    if num {BLANK_1}} 0 { {
        fmt.Println("Negative or zero")
    } else {
        fmt.Println("Positive")
    }
}
Drag options to blanks, or click blank then click option'
A<=
B==
C{
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '<=' for the condition.
Forgetting the opening brace '{' after the condition.
5fill in blank
hard

Fill all three blanks to create a nested if–else that prints "Positive even", "Positive odd", or "Non-positive".

Go
package main
import "fmt"
func main() {
    num := 8
    if num [1] 0 {
        if num [2] 2 [3] 0 {
            fmt.Println("Positive even")
        } else {
            fmt.Println("Positive odd")
        }
    } else {
        fmt.Println("Non-positive")
    }
}
Drag options to blanks, or click blank then click option'
A>
B%
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for positive check.
Using '=' instead of '==' for comparison.
Using '/' instead of '%' for remainder.