Why chain conditions
A single if/else chooses between two outcomes. Many decisions have more: a grade can be failing, passing, good or perfect. Go handles this with else if, which adds another condition to the chain, optionally followed by a final else for everything left over.
if first {
// runs when first is true
} else if second {
// runs when first is false and second is true
} else {
// runs when both are false
}
Go checks the conditions from top to bottom and runs the body of the first one that is true. Once a branch has run, the rest of the chain is skipped without being evaluated. If no condition is true, the else body runs, and if there is no else, nothing runs.
The lesson's exercise
The code starts with one variable:
grade := 8
The program has to print a message that depends on the grade: below 5 is a fail, 5 to 7 is a pass, 8 and 9 are good, and anything higher is perfect.
Step 1: the first branch
Start with the lowest range: if grade < 5 prints "Failed the test". Putting this check first lets every later branch assume that the grade is at least 5.
Step 2: two else if branches
The next branch is else if grade <= 7, which prints "Passed the test". Because it is only reached when grade < 5 was false, it does not have to say "5 or more"; the chain already guarantees that. The branch after it, else if grade <= 9, prints "Good Job" and is reached only for grades of 8 and above. Each condition is a single upper limit, and the order of the chain supplies the lower limit.
Step 3: the final else
Finish with a plain else that prints "Perfect". It has no condition and catches every grade above 9, so every possible grade produces some output.
The solution in Go
grade := 8
if grade < 5 {
fmt.Println("Failed the test")
} else if grade <= 7 {
fmt.Println("Passed the test")
} else if grade <= 9 {
fmt.Println("Good Job")
} else {
fmt.Println("Perfect")
}
For grade := 8: 8 < 5 is false, 8 <= 7 is false, 8 <= 9 is true, so the program prints Good Job and skips the final else. Trying other grades gives these results:
| grade | output |
|---|---|
| 3 | Failed the test |
| 5 | Passed the test |
| 7 | Passed the test |
| 8 | Good Job |
| 9 | Good Job |
| 10 | Perfect |
The boundary grades 5, 7 and 9 are the ones to check by hand. Each belongs to the branch whose condition includes it with <=, or, for 5, to the first branch that does not match < 5.
Chain or separate ifs?
A chain is different from several independent if statements in a row. With separate statements, every condition is checked, and more than one body can run:
grade := 6
if grade <= 7 {
fmt.Println("Passed the test")
}
if grade <= 9 {
fmt.Println("Good Job")
}
With a grade of 6, both conditions are true, so this prints both lines. In an else if chain only the first match runs, so the same grade would print just Passed the test. Use a chain when the outcomes are mutually exclusive.
Go details worth knowing
- Order is part of the logic. If the chain tested
grade <= 9first, a grade of 3 would printGood Job, because 3 is less than or equal to 9 and the later branches would never be reached. Put the most specific or most restrictive checks first. - Layout is enforced. The
else ifandelsekeywords must sit on the same line as the closing brace before them. Starting one on a new line is a syntax error. - Braces and parentheses. Braces are always required, and the conditions take no parentheses.
- An
ifcan start with a short statement.if g := grade * 2; g > 10 {declaresgand tests it, andgstays available in everyelse ifandelsebranch of that chain, and nowhere after it. This lesson doesn't need it, but you will see it in Go code. - There is no ternary operator. Go has no
condition ? a : b, so anifchain is the way to pick between values.
Common mistakes
- Repeating the lower bound. Writing
grade >= 5 && grade <= 7works but is redundant after the first branch; the chain already excludes anything below 5. - Wrong comparison at a boundary. The wording "up to 7" includes 7, so use
<=. With< 7, a grade of 7 would fall through and printGood Job. - Using
elsewith a condition.else grade > 9 {is invalid. A condition needselse if. - Starting a second chain by accident. A fresh
ifafter a chain begins a new, independent test.
Key takeaways
- Conditions in an
if/else if/elsechain are checked in order, and only the first true branch runs. - Earlier branches narrow the cases for later ones, so later conditions can be shorter.
- A final
elsecatches everything that is left. - Write
} else if cond {and} else {on the line of the closing brace.