Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using break statement in switch

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.

like image 935
avi Avatar asked Jun 10 '14 13:06

avi


People also ask

What is a break switch statement?

4) The break statement is used inside the switch to terminate a statement sequence. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. 5) The break statement is optional. If omitted, execution will continue on into the next case.

Do I need break in switch?

Technically, the final break is not required because flow falls out of the switch statement. Using a break is recommended so that modifying the code is easier and less error prone. The default section handles all values that are not explicitly handled by one of the case sections.

Why do I need break in switch case?

The case statements in a switch statements are simply labels. When you switch on a value, the switch statement essentially does a goto to the label with the matching value. This means that the break is necessary to avoid passing through to the code under the next label.

What is the use of break and default statement in switch?

Answer. break statement is used to bring the program control out of the switch expression while default statement is the last statement of the switch case.it is an optional statement.It executes only when the value stored in switch expression does not match with any case.


1 Answers

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

like image 84
Nate Cook Avatar answered Oct 08 '22 17:10

Nate Cook