Swift - Decision Making



What is Decision Making Structure?

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 be false. Using decision-making structure programmers can able to create flexible and responsive programs, which allows the program to take different paths according to the condition.

Decision Making Structure

Type of Decision-Making Statement in Swift

Swift provides the following types of decision-making statements.

Statement Description
if statement An if statement allows you to execute a block of code if the given expression is true.
if…else statement The if-else statement allows you to execute a block of code when the if condition is true. Otherwise else block will execute.
if…else if…else statements The if…else if…else statements are used to check multiple conditions in sequence and execute the block of statement associated to the first true condition.
Nested if statement Nested if statement is used to specify statement inside another statement.
Switch statement A switch statement allows a variable to be tested for equality against a list of values.

Example

Swift program to demonstrate the use of switch statements.

import Foundation

var index = 10

switch index {
   case 100 :
      print( "Value of index is 100")
   case 10,15 :
      print( "Value of index is either 10 or 15")
   case 5 :
      print( "Value of index is 5")
   default :
      print( "default case")
}

Output

It will produce the following output −

Value of index is either 10 or 15

Example

Swift program to demonstrate the use of if-else statements.

import Foundation

let X = 40
let Y = 80

// Comparing two numbers
if X > Y {
   print("X is greater than Y.")
} else {
   print("Y is greater than or equal to X.")
}

Output

It will produce the following output −

Y is greater than or equal to X.

Swift Ternary Conditional Operator

The ternary conditional operator is the shorthand representation of an if-else statement. It has three parts condition, result1 and result2. If the condition is true then it will execute the result1 and return its value. Otherwise, it will execute result2 and return its value.

Syntax

Following is the syntax of the ternary conditional Operator −

Exp1 ? Exp2 : Exp3

Where Exp1, Exp2, and Exp3 are expressions. 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.

Example

Swift program to check whether the number is odd or even using ternary conditional operator.

import Foundation
let num = 34

// Checking whether the given number is odd or even
// Using ternary conditional operator
let result = num % 2 == 0 ? "Even" : "Odd"
print("The number is \(result).")

Output

It will produce the following output −

The number is Even.

Example

Swift program to check the number is positive or negative using ternary conditional operator.

import Foundation

let num = 34

// Checking whether the given number is positive or negative
// Using ternary conditional operator
let result = num >= 0 ? "Positive" : "Negative"
print("Given number is \(result).")

Output

It will produce the following output −

Given number is Positive.
Advertisements