Using break statement in switch

avi picture avi · Jun 10, 2014 · Viewed 24.9k times · Source

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.

Answer

Nate Cook picture Nate Cook · Jun 10, 2014

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