The Swift programming language has only 3 decision statements (a.k.a branching statements) you can use in a class file. This chapter discus them. In this workshop you will learn how to use them.
Before you begin, I assume you have done the following: |
The If else statement
Swift’s if else statement is for executing a block of code if a condition is true or false. Figure 1 shows a flowchart and Swift code for the if else statement. The condition in the flowchart is evaluated. If it evaluates to true, one or more Swift statements in the if block are executed; otherwise one or more statements in the else block are executed. By the way, the else block of an if else statement is can be left out if the program logic doesn’t require it. Parenthesis () are not required around the condition as shown in the code example below; however brackets { } are required around the statements.
The Else if statement
To test several conditions, you chain else if statements. Figure 2 below shows a flowchart and Swift code for the else if statement, which declare and initialize two variables test three conditions.
Since the taxCode variable is initialized to the number 2. Code block for the third condition is executed; resulting in this output in the playground’s Result sidebar:
2.246
“Sales tax is $2.25”
Switch Statement
The Swift switch statement is for testing a value against a list of possible matching values. Each value is called a case, and the value being “switched on” is checked for each case. If a case value matches the switch value, a block of code is executed. You could replace an “else if” statement with a switch statement. Figure 3 below shows a flowchart and Swift code of a switch statement.
The switch statement in Swift is very versatile and has a lot of features. Here is a list of some of them:
- A switch statement doesn’t require a break statement after each case. That’s because the entire switch statement finishes its execution as soon as the first matching switch case is completed, without requiring an explicit break statement. You can still break out of a matched switch case before that case has completed its execution if you need to. Just add the break keyword in the body of the case you want to ignore. When that case is matched by the switch statement, the break keyword inside the case terminate execution of the switch statement.
- Cases in a switch statement are not limited to integer values. You can match against any values such as: String, Int, Double or any object for that matter. The switch statement must match against every value possible and you must have a default case.
- If no code is to be executed in the default, you simple add the break keyword.
- If you don’t provide a case for every value or a default then you will get a compiler error saying: “switch must be exhaustive”.
- Use the fallthrough keyword to execute all case blocks following the case containing the fallthrough keyword.
- You can use the … and .. range operator in a case. For example; case 1..30 or 30…40.
- You can use the where clause in a case to check for additional conditions, like this:
case let word where word.hasSuffix("ed")