Q:

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

belongs to collection: Swift Switch Statement Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Swift program to create case with multiple values ... >>
<< Swift program to demonstrate the fallthrough state...