Swift program to create case with multiple values in the switch block

Here, we are going to learn how to create case with multiple values in the switch block in Swift programming language?
Submitted by Nidhi, on June 02, 2021

Problem Solution:

Here, we will create a case with multiple values in the switch block. Values in created case separated by a comma "," operator.

Program/Source Code:

The source code to create a case with multiple values in the switch block is given below. The given program is compiled and executed successfully.

// Swift program to create case with 
// multiple values in switch block

var Num:Int=2;

switch Num {
    case 1:
    	print("One");
    case 2,3:
    	print("Two or Three");
    case 4:
    	print("Four");
    case 5:
    	print("Five");
    case 6:
    	print("Six");
    case 7:
    	print("Seven");
    default:
    	print("Unknown number");
}

Output:

Two or Three

...Program finished with exit code 0
Press ENTER to exit console.

Explanation:

In the above program, we imported a package Swift to use the print() function using the below statement,

import Swift;

Here, we implemented a case with multiple values in the switch block and, we also created an integer variable Num with initial value 2. Then we printed the appropriate message based on the value of the variable Num on the console screen.

Swift Switch Statement Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.