Bird
Raised Fist0
Goprogramming~20 mins

Nested conditional statements in Go - Practice Problems & Coding Challenges

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

Practice

(1/5)
1.

What does a nested conditional statement in Go allow you to do?

Example: if x > 0 { if x < 10 { ... } }

easy
A. Declare multiple variables in one line
B. Run multiple loops at the same time
C. Check one condition inside another condition
D. Create a function inside another function

Solution

  1. Step 1: Understand the structure of nested conditionals

    A nested conditional means putting one if statement inside another to check multiple conditions step by step.
  2. Step 2: Identify the purpose of the example code

    The example checks if x is greater than 0, then inside that block checks if x is less than 10.
  3. Final Answer:

    Check one condition inside another condition -> Option C
  4. Quick Check:

    Nested conditionals = conditions inside conditions [OK]
Hint: Nested means inside another condition [OK]
Common Mistakes:
  • Confusing nested conditionals with loops
  • Thinking nested means multiple variables
  • Mixing nested conditionals with function declarations
2.

Which of the following is the correct syntax for a nested if statement in Go?

if x > 0 {
    if x < 10 {
        fmt.Println("x is between 1 and 9")
    }
}
easy
A. if x > 0 { if x < 10 { fmt.Println("x is between 1 and 9") } }
B. if x > 0 if x < 10 fmt.Println("x is between 1 and 9")
C. if (x > 0) then if (x < 10) { fmt.Println("x is between 1 and 9") }
D. if x > 0 { if x < 10 { fmt.Println("x is between 1 and 9") } }

Solution

  1. Step 1: Check Go syntax for nested if

    Go requires braces {} around blocks and proper indentation for readability.
  2. Step 2: Compare options with correct syntax

    if x > 0 { if x < 10 { fmt.Println("x is between 1 and 9") } } uses braces and indentation correctly, matching Go style.
  3. Final Answer:

    if x > 0 { if x < 10 { fmt.Println("x is between 1 and 9") } } -> Option A
  4. Quick Check:

    Correct braces and indentation = if x > 0 { if x < 10 { fmt.Println("x is between 1 and 9") } } [OK]
Hint: Use braces {} and indent nested if blocks [OK]
Common Mistakes:
  • Omitting braces {} around if blocks
  • Using incorrect keywords like 'then'
  • Not indenting nested blocks properly
3.

What will be the output of this Go code?

package main
import "fmt"
func main() {
    x := 7
    if x > 5 {
        if x < 10 {
            fmt.Println("A")
        } else {
            fmt.Println("B")
        }
    } else {
        fmt.Println("C")
    }
}
medium
A. B
B. No output
C. C
D. A

Solution

  1. Step 1: Evaluate the first condition (x > 5)

    x is 7, which is greater than 5, so the first if block runs.
  2. Step 2: Evaluate the nested condition (x < 10)

    7 is less than 10, so it prints "A" inside the nested if.
  3. Final Answer:

    A -> Option D
  4. Quick Check:

    7 > 5 and 7 < 10 = "A" [OK]
Hint: Check conditions stepwise from outer to inner [OK]
Common Mistakes:
  • Ignoring nested else block
  • Confusing output with 'B' or 'C'
  • Assuming no output if conditions fail
4.

Find the error in this Go code snippet:

package main
import "fmt"
func main() {
    x := 3
    if x > 0
        if x < 5 {
            fmt.Println("x is between 1 and 4")
        }
}
medium
A. Missing import statement for fmt
B. Missing braces {} around the first if block
C. Incorrect variable declaration for x
D. Using > and < instead of > and <

Solution

  1. Step 1: Check syntax of the first if statement

    The first if statement lacks braces {} and a closing brace for the block.
  2. Step 2: Understand Go requires braces for if blocks

    In Go, if statements must have braces {} even if the block is one line or nested.
  3. Final Answer:

    Missing braces {} around the first if block -> Option B
  4. Quick Check:

    Go if statements need braces {} [OK]
Hint: Always use braces {} for if blocks in Go [OK]
Common Mistakes:
  • Forgetting braces {} on if statements
  • Thinking > and < must be escaped
  • Assuming import fmt is missing
5.

Write a nested conditional in Go that prints "Small positive" if a number n is greater than 0 and less than 10, prints "Large positive" if it is 10 or more, and prints "Non-positive" otherwise. What is the correct code?

hard
A. if n > 0 { if n < 10 { fmt.Println("Small positive") } else { fmt.Println("Large positive") } } else { fmt.Println("Non-positive") }
B. if n > 0 && n < 10 { fmt.Println("Small positive") } else if n > 10 { fmt.Println("Large positive") } else { fmt.Println("Non-positive") }
C. if n <= 10 { if n > 0 { fmt.Println("Small positive") } else { fmt.Println("Non-positive") } } else { fmt.Println("Large positive") }
D. if n > 0 { if n > 10 { fmt.Println("Large positive") } else { fmt.Println("Small positive") } } else { fmt.Println("Non-positive") }

Solution

  1. Step 1: Understand the conditions to check

    We check if n is greater than 0 first, then inside that check if n is less than 10 for "Small positive" or else "Large positive".
  2. Step 2: Match the nested conditional structure

    if n > 0 { if n < 10 { fmt.Println("Small positive") } else { fmt.Println("Large positive") } } else { fmt.Println("Non-positive") } correctly nests the second if inside the first and prints the right messages for each case.
  3. Final Answer:

    if n > 0 { if n < 10 { fmt.Println("Small positive") } else { fmt.Println("Large positive") } } else { fmt.Println("Non-positive") } -> Option A
  4. Quick Check:

    Nested if for ranges = if n > 0 { if n < 10 { fmt.Println("Small positive") } else { fmt.Println("Large positive") } } else { fmt.Println("Non-positive") } [OK]
Hint: Check outer condition first, then inner for ranges [OK]
Common Mistakes:
  • Swapping conditions order
  • Using else if instead of nested if
  • Mixing up the printed messages
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