If-Else Statement in Swift

Level 299 keystrokes

Checks conditions from top to bottom with if / else if / else and prints the grade for a score of 85: "B".

Swift code

// The score to grade
1let score = 85
// 85 >= 90 is false, so this block is skipped (no parentheses needed around the condition)
2if score >= 90 {
3 print("A")
// 85 >= 80 is true, so B is printed
4} else if score >= 80 {
5 print("B")
// Runs only when no condition above was true
6} else {
7 print("C")
8}

Output

B
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Swift code and how often they appear.

  • "× 6
  • (× 3
  • )× 3
  • {× 3
  • }× 3
  • =× 3
  • >× 2

More Swift examples at level 2

← Swift typing practice