Recall & Review
beginner
What is an else-if ladder in Go?
An else-if ladder is a way to check multiple conditions one after another. It uses if, else if, and else blocks to decide which code to run based on conditions.
Click to reveal answer
beginner
How do you write an else-if ladder in Go?
You start with an if statement, then add one or more else if statements, and optionally end with an else statement. Each checks a condition in order.
Click to reveal answer
beginner
What happens if none of the conditions in an else-if ladder are true?
If none of the if or else if conditions are true, the code inside the else block runs. If there is no else block, nothing happens.
Click to reveal answer
beginner
Can you have multiple else if blocks in a Go else-if ladder?
Yes, you can have as many else if blocks as you want. Go checks each condition in order until one is true.
Click to reveal answer
beginner
Write a simple else-if ladder in Go that prints "small", "medium", or "large" based on a number variable.
Example:
var size int = 10
if size < 5 {
fmt.Println("small")
} else if size < 15 {
fmt.Println("medium")
} else {
fmt.Println("large")
}
Click to reveal answer
In Go, what keyword starts an else-if ladder?
✗ Incorrect
The else-if ladder always starts with an if statement in Go.
What happens if multiple else-if conditions are true in Go?
✗ Incorrect
Go runs only the first block whose condition is true in an else-if ladder.
Which of these is the correct else-if syntax in Go?
✗ Incorrect
Go uses 'else if' with a space, not 'elseif' or 'elif'.
Is the else block mandatory in an else-if ladder in Go?
✗ Incorrect
The else block is optional; you can have just if and else if blocks.
What will this code print if x = 20?
if x < 10 {
fmt.Println("Low")
} else if x < 30 {
fmt.Println("Medium")
} else {
fmt.Println("High")
}
✗ Incorrect
Since 20 is less than 30 but not less than 10, the else if block runs and prints "Medium".
Explain how an else-if ladder works in Go and why it is useful.
Think about checking different cases one by one.
You got /6 concepts.
Write a Go program snippet using an else-if ladder to categorize an integer score into grades: A, B, C, or F.
Use if, else if, and else blocks with comparison operators.
You got /6 concepts.