0
0
Goprogramming~20 mins

Nested conditional statements in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Nested Conditionals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of nested if-else with integers
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")
  }
}
AC
BB
CNo output
DA
Attempts:
2 left
๐Ÿ’ก Hint
Check the conditions step by step starting from the outer if.
โ“ Predict Output
intermediate
2:00remaining
Nested if-else with strings
What will this Go program print?
Go
package main
import "fmt"
func main() {
  status := "active"
  if status == "active" {
    if len(status) >= 5 {
      fmt.Println("Long active")
    } else {
      fmt.Println("Active")
    }
  } else {
    fmt.Println("Inactive")
  }
}
AInactive
BActive
CLong active
DNo output
Attempts:
2 left
๐Ÿ’ก Hint
Check the length of the string "active".
โ“ Predict Output
advanced
2:00remaining
Nested if with multiple conditions
What is the output of this Go code?
Go
package main
import "fmt"
func main() {
  a, b := 3, 7
  if a < 5 {
    if b > 10 {
      fmt.Println("X")
    } else if b > 5 {
      fmt.Println("Y")
    } else {
      fmt.Println("Z")
    }
  } else {
    fmt.Println("W")
  }
}
AX
BY
CZ
DW
Attempts:
2 left
๐Ÿ’ก Hint
Check the values of a and b and follow the nested conditions carefully.
โ“ Predict Output
advanced
2:00remaining
Nested if with boolean variables
What will this Go program output?
Go
package main
import "fmt"
func main() {
  isRaining := false
  hasUmbrella := true
  if isRaining {
    if hasUmbrella {
      fmt.Println("Go outside")
    } else {
      fmt.Println("Stay inside")
    }
  } else {
    fmt.Println("Enjoy the sun")
  }
}
AEnjoy the sun
BStay inside
CGo outside
DNo output
Attempts:
2 left
๐Ÿ’ก Hint
Check the value of isRaining first.
โ“ Predict Output
expert
3:00remaining
Nested if with variable shadowing
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
  x := 5
  if x > 0 {
    x := 10
    if x > 5 {
      fmt.Println(x)
    } else {
      fmt.Println(0)
    }
  } else {
    fmt.Println(-1)
  }
  fmt.Println(x)
}
A
10
5
B
10
10
C
5
5
D
-1
5
Attempts:
2 left
๐Ÿ’ก Hint
Remember that the inner x shadows the outer x inside the if block.