0
0
Goprogramming~20 mins

If–else statement in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If–else Mastery in Go
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested if–else in Go
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("A")
    } else {
      fmt.Println("B")
    }
  } else {
    fmt.Println("C")
  }
}
AA
BB
CC
DNo output
Attempts:
2 left
💡 Hint
Check the conditions step by step from outer to inner if.
Predict Output
intermediate
2:00remaining
If–else with else if in Go
What will this Go program print?
Go
package main
import "fmt"
func main() {
  score := 75
  if score >= 90 {
    fmt.Println("Excellent")
  } else if score >= 60 {
    fmt.Println("Pass")
  } else {
    fmt.Println("Fail")
  }
}
ANo output
BExcellent
CFail
DPass
Attempts:
2 left
💡 Hint
Check which condition matches the score 75.
Predict Output
advanced
2:00remaining
If–else with short statement and variable scope
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")
  }
  // fmt.Println(x) // What if uncommented?
}
ACompilation error due to x scope
BNo
C5
D0
Attempts:
2 left
💡 Hint
The variable x is declared in the if statement header.
Predict Output
advanced
2:00remaining
If–else with multiple conditions
What does this Go program print?
Go
package main
import "fmt"
func main() {
  a, b := 3, 7
  if a > 5 && b < 10 {
    fmt.Println("X")
  } else if a > 2 || b > 10 {
    fmt.Println("Y")
  } else {
    fmt.Println("Z")
  }
}
AY
BZ
CX
DNo output
Attempts:
2 left
💡 Hint
Evaluate each condition carefully with given values.
🧠 Conceptual
expert
2:00remaining
Understanding if–else statement behavior in Go
Which statement about if–else statements in Go is TRUE?
AThe condition in an if statement must always be enclosed in parentheses.
BThe else keyword must always be on the same line as the closing brace of the if block.
CGo allows if statements without braces if there is only one statement inside.
DVariables declared in the if statement header are accessible outside the if–else blocks.
Attempts:
2 left
💡 Hint
Think about Go syntax rules for if–else formatting.