Bird
Raised Fist0
Goprogramming~10 mins

If–else statement in Go - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does an if-else statement do in Go?
easy
A. It repeats code multiple times.
B. It declares a variable.
C. It defines a new function.
D. It chooses between two paths based on a condition.

Solution

  1. Step 1: Understand the purpose of if-else

    An if-else statement checks a condition and runs code based on whether it is true or false.
  2. Step 2: Compare with other options

    Repeating code is done by loops, functions define reusable code, and variables store data, so these are not correct.
  3. Final Answer:

    It chooses between two paths based on a condition. -> Option D
  4. Quick Check:

    If-else = choose path [OK]
Hint: If-else picks true or false path fast [OK]
Common Mistakes:
  • Confusing if-else with loops
  • Thinking if-else defines functions
  • Mixing if-else with variable declaration
2. Which of these is the correct syntax for an if-else statement in Go?
easy
A. if x > 10 { fmt.Println("Big") } else { fmt.Println("Small") }
B. if (x > 10) then { fmt.Println("Big") } else { fmt.Println("Small") }
C. if x > 10: fmt.Println("Big") else: fmt.Println("Small")
D. if x > 10 { fmt.Println("Big") } elseif { fmt.Println("Small") }

Solution

  1. Step 1: Check Go if-else syntax

    Go uses braces {} for blocks and no parentheses or colons around conditions.
  2. Step 2: Identify correct syntax

    if x > 10 { fmt.Println("Big") } else { fmt.Println("Small") } matches Go syntax exactly. Options A, B, and D use wrong keywords or punctuation.
  3. Final Answer:

    if x > 10 { fmt.Println("Big") } else { fmt.Println("Small") } -> Option A
  4. Quick Check:

    Go if-else uses braces {} [OK]
Hint: Remember: Go uses braces, no then or colons [OK]
Common Mistakes:
  • Using 'then' keyword
  • Adding colons after conditions
  • Writing 'elseif' instead of 'else if'
3. What will this Go code print?
package main
import "fmt"
func main() {
  x := 5
  if x > 10 {
    fmt.Println("Greater")
  } else {
    fmt.Println("Smaller or equal")
  }
}
medium
A. Greater
B. Smaller or equal
C. 5
D. No output

Solution

  1. Step 1: Check the value of x

    x is assigned 5, which is not greater than 10.
  2. Step 2: Follow the if-else logic

    Since x > 10 is false, the else block runs, printing "Smaller or equal".
  3. Final Answer:

    Smaller or equal -> Option B
  4. Quick Check:

    5 > 10? No, print else [OK]
Hint: Check condition truth, then pick output [OK]
Common Mistakes:
  • Assuming 5 is greater than 10
  • Confusing if and else blocks
  • Expecting variable value as output
4. Find the error in this Go code:
package main
import "fmt"
func main() {
  x := 7
  if x > 5
    fmt.Println("Big")
  else
    fmt.Println("Small")
}
medium
A. Missing braces {} around if and else blocks
B. Wrong import statement
C. Variable x not declared
D. Missing semicolon after if condition

Solution

  1. Step 1: Check if-else block syntax

    In Go, if and else blocks must use braces {} to group statements.
  2. Step 2: Identify missing braces

    The code lacks braces around the print statements, causing a syntax error.
  3. Final Answer:

    Missing braces {} around if and else blocks -> Option A
  4. Quick Check:

    Go requires braces for blocks [OK]
Hint: Always use braces {} for if and else blocks [OK]
Common Mistakes:
  • Omitting braces in if-else
  • Thinking semicolons are needed after conditions
  • Confusing import errors with syntax errors
5. You want to print "Even" if a number is even, otherwise print "Odd". Which Go code correctly does this?
hard
A. if num % 2 = 0 { fmt.Println("Even") } else { fmt.Println("Odd") }
B. if num / 2 == 0 { fmt.Println("Even") } else { fmt.Println("Odd") }
C. if num % 2 == 0 { fmt.Println("Even") } else { fmt.Println("Odd") }
D. if num % 2 != 0 { fmt.Println("Even") } else { fmt.Println("Odd") }

Solution

  1. Step 1: Understand how to check even numbers

    A number is even if remainder when divided by 2 is zero (num % 2 == 0).
  2. Step 2: Check each option's condition

    if num % 2 == 0 { fmt.Println("Even") } else { fmt.Println("Odd") } uses correct equality check. if num / 2 == 0 { fmt.Println("Even") } else { fmt.Println("Odd") } divides instead of modulo. if num % 2 = 0 { fmt.Println("Even") } else { fmt.Println("Odd") } uses assignment (=) instead of comparison (==). if num % 2 != 0 { fmt.Println("Even") } else { fmt.Println("Odd") } reverses logic.
  3. Final Answer:

    if num % 2 == 0 { fmt.Println("Even") } else { fmt.Println("Odd") } -> Option C
  4. Quick Check:

    Modulo equals zero means even [OK]
Hint: Use modulo % and == 0 to check even [OK]
Common Mistakes:
  • Using / instead of %
  • Using = instead of ==
  • Reversing condition logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard