Swift - Decision Making

Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to befalse.
Following is the general from of a typical decision making structure found in most of the programming languages −
Decision Making
Swift provides the following types of decision making statements. Click the following links to check their detail.
StatementDescription
if statement
An if statement consists of a Boolean expression followed by one or more statements.
An if statement consists of a Boolean expression followed by one or more statements.

Syntax

The syntax of an if statement in Swift is as follows −
if boolean_expression {
   /* statement(s) will execute if the boolean expression is true */
}
If the Boolean expression evaluates to true, then the block of code inside the ifstatement will be executed. If Boolean expression evaluates to false, then the first set of code after the end of the if statement(after the closing curly brace) will be executed.

Flow Diagram

If Statement

Example

import Cocoa

var varA:Int = 10;

/* Check the boolean condition using if statement */
if varA < 20 {
   /* If condition is true then print the following */
   println("varA is less than 20");
}
println("Value of variable varA is \(varA)");
When we run the above program using playground, we get the following result −
varA is less than 20
Value of variable varA is 10
if...else statement
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

Syntax

The syntax of an if...else statement in Swift is as follows −
if boolean_expression {
   /* statement(s) will execute if the boolean expression is true */
} else {
   /* statement(s) will execute if the boolean expression is false */
}
If the Boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.

Flow Diagram

Example

var varA:Int = 100;

/* Check the boolean condition using if statement */
if varA < 20 {
   /* If condition is true then print the following */
   println("varA is less than 20");
} else {
   /* If condition is false then print the following */
   println("varA is not less than 20");
}

println("Value of variable varA is \(varA)");
When the above code is compiled and executed, it produces the following result −
varA is not less than 20
Value of variable varA is 100
if...else if...else Statement
An if statement can be followed by an optional else if...elsestatement, which is very useful to test various conditions using single if...else if statement.
An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
When using ifelse ifelse statements, there are a few points to keep in mind.
  • An if can have zero or one else's and it must come after any else if's.
  • An if can have zero to many else if's and they must come before the else.
  • Once an else if succeeds, none of the remaining else if's or else's will be tested.

Syntax

The syntax of an if...else if...else statement in Swift is as follows −
if boolean_expression_1 {
   /* Executes when the boolean expression 1 is true */
} else if boolean_expression_2 {
   /* Executes when the boolean expression 2 is true */
} else if boolean_expression_3 {
   /* Executes when the boolean expression 3 is true */
} else {
   /* Executes when the none of the above condition is true */
}

Example

import Cocoa

var varA:Int = 100;

/* Check the boolean condition using if statement */
if varA == 20 {
   /* If condition is true then print the following */
   println("varA is equal to than 20");
} else if varA == 50 {
   /* If condition is true then print the following */
   println("varA is equal to than 50");
} else {
   /* If condition is false then print the following */
   println("None of the values is matching");
}
println("Value of variable varA is \(varA)");
When the above code is compiled and executed, it produces the following result −
None of the values is matching
Value of variable varA is 100
nested if statements
You can use one if or else if statement inside another if or else if statement(s).
It is always legal in Swift to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).

Syntax

The syntax for a nested if statement is as follows −
if boolean_expression_1 {
   /* Executes when the boolean expression 1 is true */
   if boolean_expression_2 {
      /* Executes when the boolean expression 2 is true */
   }
}
You can nest else if...else in the similar way as you have nested if statement.

Example

import Cocoa

var varA:Int = 100;
var varB:Int = 200;

/* Check the boolean condition using if statement */
if varA == 100 {
   /* If condition is true then print the following */
   println("First condition is satisfied");
 
   if varB == 200 {
      /* If condition is true then print the following */
      println("Second condition is also satisfied");
   } 
}
println("Value of variable varA is \(varA)");
println("Value of variable varB is \(varB)");
When the above code is compiled and executed, it produces the following result −
First condition is satisfied
Second condition is also satisfied
Value of variable varA is 100
Value of variable varB is 200
switch statement
switch statement allows a variable to be tested for equality against a list of values.
A switch statement in Swift completes its execution as soon as the first matching case is completed instead of falling through the bottom of subsequent cases like it happens in C and C++ programing languages. Following is a generic syntax of switch statement in C and C++ −
switch(expression) {
   case constant-expression  :
      statement(s);
      break; /* optional */
   case constant-expression  :
      statement(s);
      break; /* optional */
  
   /* you can have any number of case statements */
   default : /* Optional */
      statement(s);
}
Here we need to use break statement to come out of a case statement otherwise execution control will fall through the subsequent case statements available below to matching case statement.

Syntax

Following is a generic syntax of switch statement available in Swift −
switch expression {
   case expression1  :
      statement(s)
      fallthrough /* optional */
   case expression2, expression3  :
      statement(s)
      fallthrough /* optional */
  
   default : /* Optional */
      statement(s);
}
If we do not use fallthrough statement, then the program will come out ofswitch statement after executing the matching case statement. We will take the following two examples to make its functionality clear.

Example 1

Following is an example of switch statement in Swift programming without using fallthrough −
import Cocoa

var index = 10

switch index {
   case 100  :
      println( "Value of index is 100")
   case 10,15  :
      println( "Value of index is either 10 or 15")
   case 5  :
      println( "Value of index is 5")
   default :
      println( "default case")
}
When the above code is compiled and executed, it produces the following result −
Value of index is either 10 or 15

Example 2

Following is an example of switch statement in Swift programming with fallthrough −
import Cocoa

var index = 10

switch index {
   case 100  :
      println( "Value of index is 100")
      fallthrough
   case 10,15  :
      println( "Value of index is either 10 or 15")
      fallthrough
   case 5  :
      println( "Value of index is 5")
   default :
      println( "default case")
}
When the above code is compiled and executed, it produces the following result −
Value of index is either 10 or 15
Value of index is 5

The ? : Operator

We have covered conditional operator ? : in the previous chapter which can be used to replace if...else statements. It has the following general form −
Exp1 ? Exp2 : Exp3;
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.
The value of a ? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.