33

Following is the given example for using break statements in switch:

let numberSymbol: Character = "三"  // Simplified Chinese for the number 3
var possibleIntegerValue: Int?
switch numberSymbol {
case "1", "١", "一", "๑":
    possibleIntegerValue = 1
case "2", "٢", "二", "๒":
    possibleIntegerValue = 2
case "3", "٣", "三", "๓":
    possibleIntegerValue = 3
case "4", "٤", "四", "๔":
    possibleIntegerValue = 4
default:
    break
}
if let integerValue = possibleIntegerValue {
    println("The integer value of \(numberSymbol) is \(integerValue).")
} else {
    println("An integer value could not be found for \(numberSymbol).")
}

The possibleIntegerValue is optional Int, so I really don't find this as a better example of using breaks in switch. Instead of break, even possibleIntegerValue = nil also works.

let numberSymbol: Character = "三"  // Simplified Chinese for the number 3
var possibleIntegerValue: Int?
switch numberSymbol {
case "1", "١", "一", "๑":
    possibleIntegerValue = 1
case "2", "٢", "二", "๒":
    possibleIntegerValue = 2
case "3", "٣", "三", "๓":
    possibleIntegerValue = 3
case "4", "٤", "四", "๔":
    possibleIntegerValue = 4
default:
    possibleIntegerValue = nil
}
if let integerValue = possibleIntegerValue {
    println("The integer value of \(numberSymbol) is \(integerValue).")
} else {
    println("An integer value could not be found for \(numberSymbol).")
}

So in this case break is not required at all. Can anyone give me a proper example of using breaks in switch, where I purposely have to ignore some cases?

The book says:

This behavior can be used to match and ignore one or more cases in a switch statement. Because Swift’s switch statement is exhaustive and does not allow empty cases, it is sometimes necessary to deliberately match and ignore a case in order to make your intentions explicit. You do this by writing the break statement as the entire body of the case you want to ignore. When that case is matched by the switch statement, the break statement inside the case ends the switch statement’s execution immediately.

avi
  • 9,292
  • 11
  • 47
  • 84
  • There is a great section on this in the Swift programming guide iBook – Jack Jun 10 '14 at 14:21
  • @NateCook Probably because this is quite fundamental to the language and emphasized heavily in all related session videos and documentation – Jack Jun 10 '14 at 14:22
  • 9
    Switches are emphasized heavily, but the break statement behavior is fairly non-standard compared to switch/break in most other languages. I think it's fair. – Nate Cook Jun 10 '14 at 14:28

1 Answers1

62

The break statement inside a switch can be used when you don't need a case to have any actual functionality, but want to include it to make your logic easier or clearer. Suppose for example you want to use a switch statement to determine if a given year is a leap year or not. (This is a bit of a contrived example.)

func isLeapYear(year: Int) -> Bool {
    switch (year) {
    case let x where (x % 100) == 0 && (x % 400) != 0:
        break
    case let x where (x % 4) == 0:
        return true
    default:
        break
    }

    return false
}

isLeapYear(2014)    // false
isLeapYear(2000)    // true
isLeapYear(1900)    // false

The first case of the switch statement inside isLeapYear lets you trap the cases where the year is both divisible by 100 and not divisible by 400, since those are sort of the exceptional non-leap years. The break statement in that case means "do nothing".

Nate Cook
  • 92,417
  • 32
  • 217
  • 178
  • 2
    this code makes better sense than default one given by Apple. Thank you! (though if-else would be much better for this ;-) – avi Jun 10 '14 at 14:33
  • 3
    Parantheses around the switch is not needed. Thus `switch year {` is enough. – Jonny May 18 '18 at 01:58