Problem overview
The program holds a numeric score and a var grade that starts as empty text. It must turn the score into a letter and print it after Grade: . The scale is: 90 and above is "A", 80 to 89 is "B", 70 to 79 is "C", 60 to 69 is "D", and anything below 60 is "F". The boundaries belong to the higher grade, so exactly 90 is an A and 59 is an F.
With score = 85 the program prints Grade: B. Earlier versions computed numbers; this one makes a decision, which is the job of if.
Approach: a ladder from the top down
Five ranges, five outcomes. A chain of if, else if and else tests the conditions in order and runs only the first block whose condition is true. Everything after that block is skipped.
That order is the whole idea. If you start from the highest grade, a branch is reached only when all the higher tests have failed. When the >= 80 test runs, the score is already known to be below 90, so you don't need to write score >= 80 && score < 90. Each test needs only a lower bound, and the final else needs no test at all: it takes whatever is left, which is everything below 60.
Step by step
- Open with the top grade.
if score >= 90setsgradeto"A". Close the block with}on the same line where the next branch begins. - Add
else if score >= 80for"B". It only runs when the first test failed, so it covers 80 to 89. - Add
else if score >= 70for"C", covering 70 to 79. - Add
else if score >= 60for"D", covering 60 to 69. - Finish with a plain
elsethat sets"F", and close the chain. - Print after the chain. The
printline sits outside all the braces, so it runs once, after a branch has chosen the grade.
The solution in Swift
let score = 85
var grade = ""
if score >= 90 {
grade = "A"
} else if score >= 80 {
grade = "B"
} else if score >= 70 {
grade = "C"
} else if score >= 60 {
grade = "D"
} else {
grade = "F"
}
print("Grade: \(grade)")
Swift specifics:
- No parentheses around the condition. Swift writes
if score >= 90 {. Parentheses are allowed but unusual. - Braces are mandatory. Even a one-line branch needs
{ }, so there is no dangling-statement trap. - The condition must be a
Bool.if score {is not allowed for an integer; you always write a comparison. letandvar.scorenever changes, so it is alet.gradeis assigned later, so it is avar. Starting it as""gives it the typeString, and the chain then replaces the empty text with a letter.
Following the chain with score = 85
The program first asks whether 85 is at least 90. It is not, so the block for "A" is skipped and the next condition is tried. Is 85 at least 80? Yes, so grade becomes "B". The remaining conditions, 70 and 60, are never evaluated, and neither is the final else. Control jumps past the end of the chain and reaches the print line, which shows Grade: B.
Now change the score to 55. The tests for 90, 80, 70 and 60 all fail one after another, and the else block runs, setting "F". In every case exactly one block runs, because the chain ends with an else that catches the rest. That guarantee is why grade never keeps its starting empty text.
The same idea works for any set of ranges: order the conditions from the most restrictive to the least, and let each branch rely on the ones before it.
Edge cases and common mistakes
- Using
>instead of>=. With> 90, a score of exactly 90 falls through to the B branch. Every boundary in the statement is inclusive. - Testing the lowest range first. If the chain starts with
score >= 60, a score of 95 matches immediately and the program prints D. The ladder only works from the top down. - Using separate
ifstatements. Five independentifblocks would all run for a high score, and the last one that matches would win.else ifkeeps the choices exclusive. - Printing inside a branch. Put the
printafter the chain, once. Copying it into each branch works, but it repeats the line five times and is easy to get wrong when you change the format. - Declaring
gradewithlet. Withlet grade = "", every later assignment in the chain is a compile error, because a constant that already has a value can't be changed. The starting code usesvarfor that reason. - Scores with decimals.
scoreis anInthere, so a value like 89.5 can't be stored in it. Handling such scores would need aDoubleand a decision about where 89.5 belongs; this lesson avoids the question.
Testing
Each result comes from running the same chain with a different score.
| score | output |
|---|---|
| 100 | Grade: A |
| 90 | Grade: A |
| 89 | Grade: B |
| 80 | Grade: B |
| 79 | Grade: C |
| 70 | Grade: C |
| 69 | Grade: D |
| 60 | Grade: D |
| 59 | Grade: F |
| 0 | Grade: F |
The pairs on each side of a boundary (90 and 89, 80 and 79, and so on) are the ones that catch off-by-one mistakes. The next version builds on this ladder.
