Swift Switch Case
Switch case are used to check multiple test case of single value and pattern. Inside the body of switch define test condition. If condition is match by given expression then executed case block statements will work and terminate switch automatically.
Otherwise not match any of test cases then in this situation it will execute default block of statements. default statement are compulsory when no case statement are match by given pattern Otherwise not compulsory.
switch(pattern){
case patternOne:
//Statements
case patternOne:
//Statements
default:
//Statements
}
Pattern is based on an expression or values. We are know about that every value is related to data type. Switch expression are produce an data type. And inside switch case statement are working this similar data values. For example.
//Case 1 Work of Integer values
var dataOne = 2
switch(dataOne){ //pattern are produce an integer value
//Case expression result in form of integers
case 1:
print("One")
case 2:
print("Two")
default:
//Default statement of this switch
print("Default Statement")
}
//Case 2 Work of boolean values
var dataTwo = 5
switch(dataTwo>3){ //pattern are produce an boolean value
case true:
print("\(dataTwo) is greater than 3")
case false:
print("\(dataTwo) is less then 3")
//Remove default statement
}
Output
Two
5 is greater than 3
In this example are clear that the switch expression result type should be same as to inside case statement. Otherwise compiler produce an error. See this example.
let testData=true
switch(testData){
case false:
print("False case")
case true:
print("True case")
case 1: //integer value
print("One")
}
Output
swift test.swift
test.swift:9:7: error: expression pattern of type 'Int' cannot match values of type 'Bool'
case 1: //integer value
^
So try to avoid this type of situation. Let see one more example.
let speed=120
switch(speed){
case 0:
print("Not Running")
case (1..<50):
print("Slow speed")
case (50...100):
print("Average speed")
case (101...180):
print("High Speed")
default:
print("undefine")
}
Output
High Speed
In this example range operator are produce an integer result. That is valid in swift programming.
Use tuples expression inside switch
let army=("Commando",12)
switch(army){
case (_,0...5):
print("Group A This Army contains maximum 5 commando")
case ("Commando",5...10):
print("Group B This Army contains commando in between 5-10 ")
case (let soldier, let commando):
print("Group C, This Army contains \(commando) \(soldier) ")
}
Output
Group C, This Army contains 12 Commando
Case statement using where
let army=("commando",7)
switch(army){
//Use where in case statement
case let (_,soldier) where soldier == 5 :
print("Army A is ready to revenge \(soldier) Commando")
case let (soldier,_) where soldier == "commando" :
print("Army B is ready to revenge ")
case let (_,soldier) :
print("Our army is ready to revenge of \(soldier) commando")
}
Output
Army B is ready to revenge
Effective use of break statement
let plan = "Trial"
let game = ("level",7)
switch(game){
case (_ ,(0...4)):
print("This level are free, play Now")
case (_ ,(5...10)):
if(plan == "Trial"){
print("Sorry You are not eligible to play this level")
break //Use break here
}
//inclue other statement
print("Welcome to this level")
default :
print("Default Statement")
}
Output
Sorry You are not eligible to play this level
Use break for an empty cases
let errorStatus = 0
switch(errorStatus){
case 0:
//Good code
break
case (0...100):
print("Write a better code to resolve error")
default:
print("Undefined errors")
}
fallthrough statement
let testCase = 50
switch(testCase){
case (0...100):
print("Enter test zone (0...100)")
fallthrough //execuet next case
case (100...200):
print("Enter test zone (100...200)")
case (200...500):
print("Enter test zone (200...500)")
default:
print("Default Statement")
}
Output
Enter test zone (0...100)
Enter test zone (100...200)
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment