Consider the following example from previous post. I have updated the first condition, which now sets upper limit of marks to 100.
var marksOfStudents = 85 if(marksOfStudents >= 75){ print("First Division") } else if(marksOfStudents > 30 && marksOfStudents < 75){ print("Second Division") }else{ print("Student Fails") }
Right now if marksOfStudents is greater than 100. We will get an output of “Student Fails” which is wrong. As marks should never be greater than 100. Therefore to handle values more than 100 or less than zero, we will have to add one more condition.
var marksOfStudents = 185 if(marksOfStudents >= 75 && marksOfStudents<=100){ print("First Division") } else if(marksOfStudents > 30 && marksOfStudents < 75){ print("Second Division") }else if(marksOfStudents>=0 && marksOfStudents<=30){ print("Student Fails") }else{ print("Out of range") }
We can see addition of one else-if block now handles the condition in proper way. And we have an output Out of range for values -10 and 185.
As conditions increase, number of else-if blocks will increase. And the code will become more complex.
In this case, which involves multiple conditions, it is better to use switch statement.
Switch statements have been designed to handle multiple conditions only. You might have seen them in Java or Python.
The syntax of switch statements in Swift is generally like this:
switch conditionalValue { case value 1: //execute this block case value 2: //execute this block case value 3, value 4: //execute this block case value5...value6: //execute this block default: otherwise, do something else
Switch statement is followed by case statements which have a block of code associated with them.
Switch compares the conditionalValue with the values given in case statement(value 1, value2, etc). When a match is found, code associated with that case is executed.
default case is used to handle values not covered in the case statements.
Unlike other languages like java or python, swift allows compound cases and ranges and a lot of other options. Switch statements in Swift are very powerful and flexible.
For example we can have same case catering to two values like value3 and value4 here. Or we can have a range of values like value5 to value6.
Let’s see some examples to better understand these.
First we will convert the above if-else code. into switch code
var marksOfStudents = 85 switch marksOfStudents { case 75...100: print("First Division") case 31...75: print("Second Division") case 0...30: print("Student Fails") default: print("Out of range") }
Since we are checking the value of marksOfStudents in the if-else block, we can use marksOfStudents as our conditional variable of switch statement.
The first if-else condition covers marks between 75 and 100 (range of values between 75 and 100). This infers our first case statement will check for values between 75 and 100 using range operator.
Similarly the second case will cater to range of 31 and 75. And the third case will take range of 0 and 30. For any values less than 0 or greater than 100 we will add a default case.
With marksOfStudents = 85 we get the output of first division as expected.
Let’s check the outputs when marksOfStudents equals 25 or 60 or 105.
Now check with marksOfStudents equals 75.
Although 75 is included in the second case too, we get the output first division. Because swift checks the conditions one by one and executes the code associated with the first match. So in our case, as soon as first case 75…100 matches, first division is printed. And the second case 31..75 is ignored.
Let’s consider another example: To count the number of vowels in a sentence.
var sentence = "I am learning swift" var countVowels = 0 for letter in sentence.characters{ switch letter { case "a","A","e","E","i","I","o","O","u","U": countVowels += 1 default: countVowels += 0 } } print("Number of vowels = \(countVowels)")
Instead of writing separate cases for each vowel in upper or lowercase letter, we can compound all the values in one case itself as shown in example.
In above example itself, we will find the number of spaces in the sentence. For this we will add a case ” ” and a counter to store number of spaces. The code will look like.
var sentence = "I am learning swift" var countVowels = 0 var countSpaces = 0 for letter in sentence.characters{ switch letter { case "a","A","e","E","i","I","o","O","u","U": countVowels += 1 case " ": countSpaces += 1 default: countVowels += 0 } } print("Number of vowels = \(countVowels)") print("Number of spaces = \(countSpaces)")
This completes switch statement. Next we will start with functions.