The switch
statement’s ability to use multiple values in a single case is also noteworthy. They’re called switch statement compound cases in Swift. Each value within a compound case will be matched to the given expression using the switch
statement.
Using a switch
statement, the following code checks the value of nation
and determines the continent on which it is located. Compound cases are useful because a continent may contain multiple nations:
let nation = "Pakistan";
switch nation {
case "Brazil", "Argentina", "Peru":
print("\(nation) is in South America.");
case "Nigeria", "Ethiopia", "Egypt":
print("\(nation) is in Africa.");
case "Nepal", "Maldives", "Pakistan":
print("\(nation) is in South Asia.");
default:
print("This nation is located somewhere on the world!");
}
// Prints: Pakistan is in South Asia.
Take notice of how:
nation
within the String
of the print()
statement.Here are some useful tools to help you along your journey!
Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.