Using the where statement in a switch

suggest change

where can add extra criteria to a switch case. temperature below is an Int. The cases match a range and also require an even number:

switch (temperature) {
      case 0...49 where temperature % 2 == 0:
        print("Cold and even")

      case 50...79 where temperature % 2 == 0:
        print("Warm and even")

      case 80...110 where temperature % 2 == 0:
        print("Hot and even")

      default:
        print("Temperature out of range or odd")
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents