在開關中使用 where 語句

where 語句可以在 switch case 匹配中使用,以新增正匹配所需的其他條件。以下示例不僅檢查範圍,還檢查數字是奇數還是偶數:

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")
}