Switch Statement in Swift

Level 2106 keystrokes

Matches a String against cases with switch, writing each case on one line; "A" matches the first case and prints "Excellent".

Swift code

// The grade to match
1let grade = "A"
// Compares grade with each case in order
2switch grade {
// Matches, so Excellent is printed and the switch ends (no fallthrough)
3case "A": print("Excellent")
4case "B": print("Good")
// Required: a switch on String must cover all other values
5default: print("OK")
6}

Output

Excellent
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.

  • "× 12
  • :× 3
  • (× 3
  • )× 3
  • {× 1
  • }× 1
  • =× 1

More Swift examples at level 2

← Swift typing practice