in , ,

Swift switch Statement

Swift switch Statement
Swift switch Statement

Swift switch Statement: In this tutorial, you will learn to use switch control statements to control the flow of your program’s execution.

The switch statement allows us to execute a block of code among numerous other options.

The Switch statement is used as a substitute for long if-else-if statement while matching with complex patterns. It provides numerous cases to perform various activities as per various conditions.

The syntax of the switch statement in Swift is:

switch (expression)  {
  case value1:
    // statements 

  case value2:
    // statements 

  ...
  ...
        
  default:
    // statements
}
  1. The switch statement evaluates an expression inside brackets ().
  2. In the event that the result of the expression is equivalent to value1, statements of the case value1: are executed.
  3. On the off chance that the result of the expression is equivalent to value2, statements of the case value2: are executed.
  4. On the off chance that there is no match, statements of the default case are executed.

Note: We can do exactly the same thing with the if…else…if ladder. Notwithstanding, the syntax of the switch statement is cleaner and a lot simpler to read and write.


Example 1: Simple program using Switch Statement

// program to find the day of the week 

let dayOfWeek = 4

switch dayOfWeek {
  case 1:
    print("Sunday")
	    
  case 2:
    print("Monday")
	    
  case 3:
    print("Tuesday")
	    
  case 4:
    print("Wednesday")
	    
  case 5:
    print("Thursday")
	    
  case 6:
    print("Friday")
	    
  case 7:
    print("Saturday")
	    
  default:
    print("Invalid day")
}

Output

Wednesday

In the above example, we have assigned 4 to the dayOfWeek variable. Presently, the variable is compared and the worth of each case statement.

Since the value matches with case 4, the statement print(“Wednesday”) inside the case is executed, and the program is ended.


Switch Statement with fallthrough

In the event that we use the fallthrough watchword inside the case statement, the control continues to the next case even if the case value doesn’t coordinate with the switch expression. For instance,

// program to find the day of the week 

let dayOfWeek = 4

switch dayOfWeek {
  case 1:
    print("Sunday")
	    
  case 2:
    print("Monday")
	    
  case 3:
    print("Tuesday")
	    
  case 4:
    print("Wednesday")
    fallthrough
	    
  case 5:
    print("Thursday")
	    
  case 6:
    print("Friday")
	    
  case 7:
    print("Saturday")
	    
  default:
    print("Invalid day")
}

Output

Wednesday
Thursday

In the above example, since the value matches with case 4, the statement print(“Wednesday”) inside the case is executed

We have additionally used the watchword fallthrough. Thus, the statement print(“Thursday”) inside case 5 is executed even if the case doesn’t coordinate with the case statement.


Switch Statement with Range

let ageGroup = 33

switch ageGroup {
  case 0...16:
    print("Child")

  case 17...30:
    print("Young Adults")

  case 31...45:
    print("Middle-aged Adults")

  default:
    print("Old-aged Adults")
}

Output

Middle-aged Adults

In the above example, rather than a single value, we have used numeric ranges for each case: case 0…16, case 17…30, and case 31…45.

Here, the value of ageGroup is 33 that falls under the range 32…45. Consequently, the statement print(“Middle-matured Adults”) is executed.


Tuple in Swift Switch Statement

A Tuple is an assortment of numerous data items. For instance,

let info = ("Swift",2014)

Here, we have created a tuple named info with a string value Swift and an integer value 2014.

In Swift, we can likewise use tuples in switch statements. For instance,

let info = ("Sohail", 38)

// match complete tuple values
switch info {
  case ("Sohail", 38): 
    print("Sohail is 38 years old")

  case ("Haroon", 46): 
    print("Haroon is 46 years old")

  default:
    print("Not known")
}

Output

Sohail is 38 years old

In the above example, we have created a tuple named info with values: “Sohail” and 38. Here, rather than a single value, each case statement additionally incorporates tuples:

case (“Sohail”, 38) and case (“Haroon”, 46).

Presently, the value of info is compared, and each case statement. Since the value matches with the case (“Sohail”, 38), the statement print(“Sohail is 38 years of age”) is executed.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Swift if else Statement

Swift if, if…else Statement

Swift for-in Loop

Swift for-in Loop