- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
Swift - Switch Statement
A switch statement is the most powerful control statement which allows us to represent multi-branch logic in a readable manner. It provides a way to evaluate a value against the multiple given case and execute the block of statements present in the first matching case.
A switch statement is an alternative to an if-else statement because a switch handles multiple conditions efficiently as compared to an if-else statement.
Syntax
Following is the syntax of the switch statement −
switch expression{ case 1: // Statement fallthrough // Optional case 2: // Statement break // Optional default: // Statement
Example
The following is the example of the switch statement −
import Foundation var index = 10 switch index { case 100 : print( "Value of index is 100") case 10,15 : print( "Value of index is either 10 or 15") case 5 : print( "Value of index is 5") default : print( "default case") }
Output
It will produce the following output −
Value of index is either 10 or 15
Swift Break Statement in Switch Statement
The break statement is used to terminate the switch block immediately after executing the matched case statement block and the controls transfer to the statement present just after the switch block. Break statements are used in case blocks.
Syntax
Following is the syntax for the break statement −
switch expression{ case x: // Statement break default: // Statement }
Example
The following is the example for the break statement −
import Foundation let day = 4 switch day { // Case with the break statements case 1: print("Monday") break case 2: print("Tuesday") break case 3: print("Wednesday") break case 4: print("Thursday") break case 5: print("Friday") break default: print("Weekend") }
Output
It will produce the following output −
Thursday
Swift Fallthrough in Switch Statement
A switch statement ends its execution as soon as it encounters its first matching case, even if we use a break statement or not. Control does not fallthrough the bottom of subsequent cases like in C and C++ programming.
If we want to achieve the fallthrough feature in Swift, then we have to explicitly use the fallthrough statement in each case. The fallthrough statement allows controls to pass down to the next case even if the condition for that case is not matched.
Syntax
Following is the syntax for the fallthrough statement −
switch expression{ case x: // Statement fallthrough default: // Statement }
Example
The following is the example for the fallthrough statement −
import Foundation let number = 2 switch number { // Case statement with fall through case 1: print("Hello i am case 1") fallthrough case 2: print("Hello i am case 2") fallthrough case 3: print("Hello i am case 3") default: print("Not found") }
Output
It will produce the following output −
Hello i am case 2 Hello i am case 3
Swift Ranges in Switch Statement
In Swift, we are allowed to use ranges in the case statements to match a value against a range of elements. It is commonly used when we want to handle cases where the value falls in the given range.
Syntax
Following is the syntax for ranges in case statement −
switch expression{ case x…y: // Statement default: // Statement }
Example
The following is the example for ranges in case statement −
import Foundation let temp = 27 switch temp { // Case statement with ranges case ..<0: print("Freezing.") case 0...20: print("Cold.") case 21...30: print("Moderate.") case 31...: print("Hot") default: print("Please enter valid temperature") }
Output
It will produce the following output −
Moderate.
Swift Tuple Matching in Switch Statement
As we know a tuple is used to store multiple values together, whether they are of the same type or not. So we are allowed to use tuple as a variable expression in the switch statement and can also use tuple to test multiple values in the case statement. We can also use the wildcard pattern "_" in the tuple.
Syntax
Following is the syntax for tuple matching −
switch (x1, x2){ case (y, x1): // Statement default: // Statement }
Example
The following is the example for tuple matching −
import Foundation // Switch expression in tuple let number = (num1: 25, num2: 10, num3: 11) switch number { // Case expression in tuple case (25, 10, 0): print("Numbers are (25, 10, 0)") case (13, _, 11): print("Numbers are (13, 11)") case (25, 10, 11): print("Number are (25, 10, 11)") default: print("Number is not found") }
Output
It will produce the following output −
Number are (25, 10, 11)
Swift Value Binding in Switch Statement
Swift provides a new property named value binding. Using this property, we are allowed to initialize a temporary variable in the case statement and this variable is accessible only in the body of the case in which it is defined. Or we can say that it binds the match value to a temporary variable or constant inside the related block.
Syntax
Following is the syntax for value binding −
switch expression{ case (let x, let y): // Statement default: // Statement }
Example
The following is the example for value binding −
import Foundation let num = 17 switch num { // Value binding case let y where y % 2 == 0: print("\(num) is an even number.") case let z where z % 2 != 0: print("\(num) is an odd number.") default: print("\(num) is invalid number.") }
Output
It will produce the following output −
17 is an odd number.
Swift Switch Statement with where Clause
We can also use the where clause with case conditions to check for extra conditions. It is used when we want to filter the case according to the additional condition.
Syntax
Following is the syntax for the case with where clause −
switch expression{ case "0"…"8" where expression.isNumber: // Statement default: // Statement }
Example
The following is the example for the case with where clause −
import Foundation let marks = 77 switch marks { case 0...59: print("Sorry! You are Failed") // Case with where clause case 60...79 where marks < 75: print("Congratulations! You are Passed") case 60...79 where marks >= 75: print("Congratulations! You are Passed with distinction") case 80...100: print("Excellent! You topped the Class") default: print("Not a valid score") }
Output
It will produce the following output −
Congratulations! You are Passed with distinction
Compound Cases in Swift
In the switch statement, we are allowed to use multiple patterns in a single case, where all the patterns present in a case are separated by commas and share the same block of code.
If any of the given pattern matches, then the block of code present in that case will execute. Compound cases are commonly used when we want to execute the same block of code for various cases.
Syntax
Following is the syntax for compound case −
switch expression{ case a, b, c: // Statement case x, y, z, w: // Statement default: // Statement }
Example
The following is the example for compound case −
import Foundation let number = 20 switch number { // Compound case case 10, 20, 30: print("Selected number is 10, 20 or 30") case 40, 50, 60: print("Selected number is 40, 50, 60") default: print("Value not found") }
Output
It will produce the following output −
Selected number is 10, 20 or 30