Lesson 5 of 15

Switch

Switch Statements

Swift's switch is more powerful than C's. Cases don't fall through by default, and no explicit break is needed:

let day = 3

switch day {
case 1:
    print("Monday")
case 2:
    print("Tuesday")
case 3:
    print("Wednesday")
default:
    print("Other")
}
// Wednesday

Multiple Values Per Case

switch month {
case 12, 1, 2:
    print("winter")
case 3, 4, 5:
    print("spring")
default:
    print("other")
}

Your Task

Write a function season that takes a month number (1-12) and returns the season:

  • "winter" for months 12, 1, 2
  • "spring" for months 3, 4, 5
  • "summer" for months 6, 7, 8
  • "autumn" for months 9, 10, 11
JS Transpiler loading...
Loading...
Click "Run" to execute your code.