Switch Statement in Swift

Level 2133 keystrokes

Uses switch with several values per case to classify "Mon" as a weekday and prints "Weekday".

Swift code

// The day to classify
1let day = "Mon"
// Compares day with each case from top to bottom
2switch day {
// A case can list several values separated by commas; "Mon" matches here
3case "Mon", "Tue", "Wed":
// Only this case runs; Swift does not fall through, so no break is needed
4 print("Weekday")
5case "Sat", "Sun":
6 print("Weekend")
// switch must be exhaustive, so a String switch needs default
7default:
8 print("Other")
9}

Output

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

  • "× 18
  • ,× 3
  • :× 3
  • (× 3
  • )× 3
  • {× 1
  • }× 1
  • =× 1

More Swift examples at level 2

← Swift typing practice