Bird
Raised Fist0
Goprogramming~20 mins

If statement in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
If Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of nested if statements
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    x := 10
    if x > 5 {
        if x < 15 {
            fmt.Println("Inside nested if")
        }
    } else {
        fmt.Println("Outside if")
    }
}
ANo output
BOutside if
CInside nested if
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Check the conditions in the nested if statements carefully.
โ“ Predict Output
intermediate
2:00remaining
If statement with else if
What will this Go program print?
Go
package main
import "fmt"
func main() {
    score := 75
    if score >= 90 {
        fmt.Println("Grade A")
    } else if score >= 80 {
        fmt.Println("Grade B")
    } else if score >= 70 {
        fmt.Println("Grade C")
    } else {
        fmt.Println("Grade F")
    }
}
AGrade A
BGrade C
CGrade B
DGrade F
Attempts:
2 left
๐Ÿ’ก Hint
Check which condition matches the score value first.
โ“ Predict Output
advanced
2:00remaining
If statement with short statement
What is the output of this Go code?
Go
package main
import "fmt"
func main() {
    if x := 5; x > 3 {
        fmt.Println(x)
    } else {
        fmt.Println("No")
    }
}
A0
BNo
CCompilation error: x undefined
D5
Attempts:
2 left
๐Ÿ’ก Hint
Look at how the variable x is declared inside the if statement.
โ“ Predict Output
advanced
2:00remaining
If statement with multiple conditions
What will this Go program output?
Go
package main
import "fmt"
func main() {
    a, b := 4, 7
    if a > 3 && b < 10 {
        fmt.Println("Condition met")
    } else {
        fmt.Println("Condition not met")
    }
}
ACondition met
BCondition not met
CCompilation error
DNo output
Attempts:
2 left
๐Ÿ’ก Hint
Both conditions must be true for the if block to run.
โ“ Predict Output
expert
2:00remaining
If statement with variable shadowing
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    x := 10
    if x := 5; x > 3 {
        fmt.Println(x)
    }
    fmt.Println(x)
}
A5\n10
B10\n10
C5\n5
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Notice the variable x declared inside the if statement shadows the outer x.

Practice

(1/5)
1. What does an if statement do in Go?
easy
A. It runs code only if a condition is true.
B. It repeats code multiple times.
C. It stores data in a variable.
D. It defines a new function.

Solution

  1. Step 1: Understand the purpose of an if statement

    An if statement checks a condition and runs code only when that condition is true.
  2. Step 2: Compare options with the if statement behavior

    Only "It runs code only if a condition is true." describes running code based on a condition being true.
  3. Final Answer:

    It runs code only if a condition is true. -> Option A
  4. Quick Check:

    If statement = runs code if true [OK]
Hint: If checks true/false to decide running code [OK]
Common Mistakes:
  • Confusing if with loops
  • Thinking if stores data
  • Mixing if with function definitions
2. Which of these is the correct syntax for an if statement in Go?
easy
A. if (x > 10) then { fmt.Println("Big") }
B. if x > 10 { fmt.Println("Big") }
C. if x > 10: fmt.Println("Big")
D. if x > 10 { fmt.Println("Big") else }

Solution

  1. Step 1: Recall Go if statement syntax

    Go uses if condition { code } without parentheses or 'then'.
  2. Step 2: Check each option

    if x > 10 { fmt.Println("Big") } matches Go syntax exactly. Options A, C, and D have syntax errors.
  3. Final Answer:

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

    Go if syntax = if condition { } [OK]
Hint: Go if uses braces, no 'then' or colons [OK]
Common Mistakes:
  • Adding 'then' keyword
  • Using parentheses around condition
  • Using colon instead of braces
3. What is the output of this Go code?
package main
import "fmt"
func main() {
  x := 5
  if x > 3 {
    fmt.Println("Yes")
  } else {
    fmt.Println("No")
  }
}
medium
A. Syntax error
B. No
C. 5
D. Yes

Solution

  1. Step 1: Check the condition in the if statement

    The variable x is 5, and the condition is x > 3, which is true.
  2. Step 2: Determine which block runs

    Since the condition is true, the code inside the if block runs, printing "Yes".
  3. Final Answer:

    Yes -> Option D
  4. Quick Check:

    x=5 > 3 is true, prints "Yes" [OK]
Hint: If condition true runs if-block, else runs else-block [OK]
Common Mistakes:
  • Confusing which block runs
  • Thinking it prints the variable
  • Assuming syntax error due to else
4. Find the error in this Go code:
package main
import "fmt"
func main() {
  x := 10
  if x > 5
    fmt.Println("Big")
}
medium
A. Variable x not declared
B. Missing semicolon after x := 10
C. Missing braces { } after if condition
D. Wrong import statement

Solution

  1. Step 1: Check if statement syntax

    In Go, the if condition must be followed by braces { } to group the code.
  2. Step 2: Identify the missing part

    The code after if lacks braces, causing a syntax error.
  3. Final Answer:

    Missing braces { } after if condition -> Option C
  4. Quick Check:

    If needs braces { } after condition [OK]
Hint: Always use braces { } after if condition [OK]
Common Mistakes:
  • Omitting braces after if
  • Adding unnecessary semicolons
  • Misunderstanding import errors
5. You want to print "Positive", "Zero", or "Negative" based on an integer variable n. Which code correctly uses if statements to do this?
hard
A. if n > 0 { fmt.Println("Positive") } else if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") }
B. if n > 0 { fmt.Println("Positive") } if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") }
C. if n > 0 { fmt.Println("Positive") } else if n <= 0 { fmt.Println("Negative") } else { fmt.Println("Zero") }
D. if n >= 0 { fmt.Println("Positive") } else if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") }

Solution

  1. Step 1: Understand the conditions for each case

    Positive means n > 0, zero means n == 0, negative means n < 0.
  2. Step 2: Check each option's logic

    if n > 0 { fmt.Println("Positive") } else if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") } correctly checks n > 0, then n == 0, else negative. if n > 0 { fmt.Println("Positive") } if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") } has two separate ifs causing multiple prints. if n > 0 { fmt.Println("Positive") } else if n <= 0 { fmt.Println("Negative") } else { fmt.Println("Zero") } swaps zero and negative logic. if n >= 0 { fmt.Println("Positive") } else if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") } wrongly treats n >= 0 as positive.
  3. Final Answer:

    if n > 0 { fmt.Println("Positive") } else if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") } -> Option A
  4. Quick Check:

    Correct if-else chain matches conditions [OK]
Hint: Use else if to check exclusive conditions in order [OK]
Common Mistakes:
  • Using multiple ifs causing multiple outputs
  • Mixing condition order
  • Wrong comparison operators
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