Nested conditional statements help you make decisions inside other decisions. This lets your program check many things step by step.
Nested conditional statements in Go
if condition1 { if condition2 { // code if both condition1 and condition2 are true } else { // code if condition1 is true but condition2 is false } } else { // code if condition1 is false }
Each if or else block can contain another if statement.
Indent your code inside blocks to keep it clear and easy to read.
if age >= 18 { if age >= 21 { fmt.Println("You can drink alcohol in the US.") } else { fmt.Println("You can vote but not drink alcohol in the US.") } } else { fmt.Println("You are too young to vote or drink alcohol.") }
if score >= 50 { if score >= 90 { fmt.Println("Grade: A") } else { fmt.Println("Grade: Pass") } } else { fmt.Println("Grade: Fail") }
This program checks a person's age. It first checks if the person is 18 or older. If yes, it then checks if the person is 21 or older to decide what message to print.
package main import "fmt" func main() { age := 20 if age >= 18 { if age >= 21 { fmt.Println("You can drink alcohol in the US.") } else { fmt.Println("You can vote but not drink alcohol in the US.") } } else { fmt.Println("You are too young to vote or drink alcohol.") } }
Remember to use curly braces {} to group your code blocks.
Nested conditions can get hard to read if too deep. Try to keep them simple.
Nested conditionals let you check one condition inside another.
Use them to make detailed decisions step by step.
Keep your code clean and well indented for easy reading.
Practice
What does a nested conditional statement in Go allow you to do?
Example: if x > 0 { if x < 10 { ... } }
Solution
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.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.Final Answer:
Check one condition inside another condition -> Option CQuick Check:
Nested conditionals = conditions inside conditions [OK]
- Confusing nested conditionals with loops
- Thinking nested means multiple variables
- Mixing nested conditionals with function declarations
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")
}
}Solution
Step 1: Check Go syntax for nested if
Go requires braces {} around blocks and proper indentation for readability.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.Final Answer:
if x > 0 { if x < 10 { fmt.Println("x is between 1 and 9") } } -> Option AQuick Check:
Correct braces and indentation = if x > 0 { if x < 10 { fmt.Println("x is between 1 and 9") } } [OK]
- Omitting braces {} around if blocks
- Using incorrect keywords like 'then'
- Not indenting nested blocks properly
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")
}
}Solution
Step 1: Evaluate the first condition (x > 5)
x is 7, which is greater than 5, so the first if block runs.Step 2: Evaluate the nested condition (x < 10)
7 is less than 10, so it prints "A" inside the nested if.Final Answer:
A -> Option DQuick Check:
7 > 5 and 7 < 10 = "A" [OK]
- Ignoring nested else block
- Confusing output with 'B' or 'C'
- Assuming no output if conditions fail
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")
}
}Solution
Step 1: Check syntax of the first if statement
The first if statement lacks braces {} and a closing brace for the block.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.Final Answer:
Missing braces {} around the first if block -> Option BQuick Check:
Go if statements need braces {} [OK]
- Forgetting braces {} on if statements
- Thinking > and < must be escaped
- Assuming import fmt is missing
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?
Solution
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".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.Final Answer:
if n > 0 { if n < 10 { fmt.Println("Small positive") } else { fmt.Println("Large positive") } } else { fmt.Println("Non-positive") } -> Option AQuick 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]
- Swapping conditions order
- Using else if instead of nested if
- Mixing up the printed messages
Ready to prove it?
Pass the test and this pattern moves unknown
