Swift Program to Implement switch statement on Strings


A switch statement is a control flow statement in which it only executes the block of code as soon as the expression given in the switch statement matches a case among the given multiple cases. If no case satisfies the given expression, then the switch statement executes the default case. In Swift, we are allowed to implement a switch statement on strings.

Syntax

switch (expression)
{
case 1:
// Block of code
case 2: 
// Block of code
.
.
.
default:
// Block of code
}

Here, the switch statement evaluates the expression and executes only the matched case. Where expression is of any type but in our case is of string type.

Algorithm

  • Step 1 − Create a variable to store string type expression.

  • Step 2 − Assign the variable to the switch statement.

  • Step 3 − Now switch statement checks if any of the given cases matched the specified expression.

  • Step 4 − If yes, then execute the block of code given in the specified case. Otherwise, execute the default statement.

  • Step 5 − Print the output.

Example 1

In the following Swift program, we will implement a switch statement on a string. So first we assign a string-type expression to a variable and then check each case. If the match is found, then execute the corresponding block of code. If no match is found, then it will run the default case. Here in our case box = “Pencil” hence the code present in the “Pencil” case will execute.

import Foundation
import Glibc

let box = "Pencil"

switch box {
case "Eraser":
   print("Geometry Box contain Eraser")
case "Pen":
   print("Geometry Box contain Pen")
case "Pencil":
   print("Geometry Box contain Pencil")
case "Ruler":
   print("Geometry Box contain Ruler")
default:
   print("Geometry Box does not contain the specified item")
}

Output

Geometry Box contain Pencil

Example 2

In the following Swift program, we will implement a switch statement on a string. So first we assign a string-type expression to a variable, then assign that variable to the switch statement and then create multiple cases for multiple literals using comma-separated format(case “Eraser”, “Pencil”:). So, if the value of the box is either “Pencil” or “Eraser”, then the code present in the corresponding case statement executes. Similarly for the other cases. If no match is found, then it will run the default case. Here in our case box = “Pencil” hence the code present in the “Eraser”, “Pencil” case statement will execute.

import Foundation
import Glibc

let box = "Pencil"

switch box {
case "Eraser", "Pencil":
   print("Geometry Box contain Eraser and Pencil")
case "Pen", "Ink":
   print("Geometry Box contain Pen and Ink")
case "Ruler", "Compass":
   print("Geometry Box contain Ruler and Compass")
default:
   print("Geometry Box does not contain the specified item")
}

Output

Geometry Box contain Eraser and Pencil

Example 3

In the following Swift program, we will implement a switch statement on a string. So first we assign a string-type expression to a variable, then assign that variable to the switch statement and then create multiple cases where in each case we specify a particular range for example “G”...”M” which represents names starting with alphabet G to M. If the name letter found in a case, then run the code present in it. If no match is found, then run the default case.

import Foundation
import Glibc

let name = "Pushpa"

switch name {
case "A"..."F":
   print("Employees whose name starts with A to F are transferred to Group MAX")
case "G"..."M":
   print("Employees whose name starts with G to M are transferred to Group MIN")
case "N"..."S":
   print("Employees whose name starts with N to S are transferred to Group MIDDLE")
case "T"..."Z":
   print("Employees whose name starts with T to Z are transferred to Group SUPER")
default:
   print("Invalid name! Try again")
}

Output

Employees whose name starts with N to S are transferred to Group MIDDLE

Conclusion

So this is how we can implement switch statements on strings. In the switch statement, we can add more cases according to our requirements. A switch statement is just like an if-else-if statement but it is more clear and readable as compared to an if-else-if statement.

Updated on: 14-Jun-2023

324 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements