Open In App

Swift – Nested if-else Statement

Last Updated : 11 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Swift, a situation comes when we have to check an if condition inside an if-else statement then a term comes named nested if-else statement. It means an if-else statement inside another if statement. Or in simple words first, there is an outer if statement, and inside it another if – else statement is present and such type of statement is known as nested if statement. We can use one if or else if statement inside another if or else if statements.

Syntax: 

// Outer if condition
if (condition 1) {
   // inner if condition
   if (condition 2) {
       // Block of Code and Statements
   }
   // inner else condition
   else {
       // Block of Code and Statements
   }
}
// Outer else statement
else {
   // inner if condition
   if (condition 3) {
       // Block of Code and Statements
   }
   // inner else condition
   else {
       // Block of Code and Statements
   }
}

Here, if the outer condition1 is true, then it will check for inner condition 2. If inner condition is true then it will proceed to statement 1 otherwise statement 2 will proceed. Similarly in outer else part will execute.

Notes: We can add else and else if statements to the inner if statement whenever required. Also, we can nest multiple layers of if-else statements inside a if statement.

Flowchart: 

Example 1: 

Swift




// Swift program illustrate the use
// of Nested if-else statement
 
import Swift
var a = 100
var b = 200
var c = 300
 
// outer if statement
if (a > b) {
 
  // inner if statement
  if (a > c) {
     
      // Print statement 1
      print("100 is Greater")
  }
 
  // inner else statement
  else {
      // Print statement 2
      print("300 is Greater");
  }
}
 
// outer else statement
else {
   
  // inner if statement
  if (b > c) {
     
      // Print statement 3
      print("200 is Greater")
  }
 
  // inner else statement
  else {
     
      // Print statement 4
      print("300 is Greater");
  }
}


Output: 

300 is Greater 

Explanation : In above example, First it will check for outer if condition. If outer if condition is true then it will check for inner if statement. If outer if statement is false it then will move to outer else part and so on. Here outer if condition is false. Next it will move to outer else part and check for inner if statement i.e. if b is greater than c ( b > c ). Here it evaluates false. Hence it will move to inner else part and prints statement 4.

Example 2 : 

Swift




// Swift program illustrate the use of
// Nested if-else-if statement
import Swift
 
 
var number = 5
 
// outer if statement
if (number >= 0) {
 
  // inner if statement
  if (number == 0) {
     
      // Print statement 1
      print("Number is 0")
  }
 
  // inner else statement
  else {
     
      // Print statement 2
      print("Number is greater than 0");
  }
}
 
// outer else statement
else {
   
    // Print statement 3
    print("Number is smaller than 0");
}


Output : 

Number is greater than 0

Explanation : Here we have declared a variable number with value 15. Then we have used if-else construct. If the outer if condition is true then and only then it will execute the inner if condition. In this example, the outer if condition is true hence the inner if block is processed. In the inner if condition, we again have a condition that checks if number is equals to 0 or not. When inner condition is true, then it will process the print statement 1  and If inner if statement is false then it will process the print statement 2. In this case, the inner if condition is false  hence it will move to else part and prints statement 2 and the value is printed on the output screen.



Previous Article
Next Article

Similar Reads

Swift - If-else Statement
Just like other programming languages in Swift language also support the if-else statement. In the if-else statement, when the given condition is true then the code present inside the if condition will execute, and when the given condition is false then the code present inside the else condition will execute. Or in other words if "if" statement is
3 min read
Swift - If-else-if Statement
In Swift, the if-else if-else condition is used to execute a block of code among multiple options. It gives a choice between more than two alternative conditions as soon as one of the conditions is true, the statement associated with that if is executed. If all conditions are false or not satisfied then the final else statement will be executed in
3 min read
Swift - Convert String to Int Swift
Swift provides a number of ways to convert a string value into an integer value. Though, we can convert a numeric string only into an integer value. In this article, we will see the two most common methods used to convert a string to an integer value. A string is a collection of characters. Swift provides String data type with the help of which we
3 min read
Swift - Break Statement
The break statement is a loop control statement that is used to end the execution of an entire control flow statement immediately when it is encountered. When the break condition is true, the loop stops its iterations, and control returns immediately from the loop to the first statement after the loop. In simple words, break statement breaks the cu
6 min read
Swift - If Statement
Just like other programming languages in Swift language also if statement is used to execute the program based on the evaluation of one or more conditions or in other words if statement is used to run a piece of code only when the given condition is true. There are also known as branch statement. For example, let us consider a case where you’re goi
3 min read
Swift - Fallthrough Statement
Just like other languages in Swift also switch statement is very useful and powerful. It takes a value and then compare it with several possible matching pattern or we can say cases and stops its execution when it successfully found its first matching case statement and then execute the code present in the matched statement. It optimise code by avo
4 min read
Swift - Switch Statement
In Swift, a switch statement is a type of control mechanism that permits a program to change its flow of control and take some decisions on the basis of some conditions imposed on the value of a variable. The control flow of a program comes out of the switch statement block as soon as the matching condition is met. It also provides a default condit
10 min read
Swift - Guard Statement
Swift provides a special type of statement referred to as "guard" statement. A guard statement is capable to transfer the flow of control of a program if a certain condition(s) aren't met within the program. Or we can say, if a condition expression evaluates true, then the body of the guard statement is not executed. In other words, the control flo
15 min read
Swift - Nested Function
A function is a collection of statements grouped together as a block to perform a specific task. The return type of a function decides the type of value returned by the function. For example, if we want to get an integer value from a function then the return type of the function must be Int, if we want to get decimal value from a function then the
5 min read
Swift - Typealias
A variable is a named container that is used to store a value. Now each value has a type to which it belongs and is known as data type. A data type is a classification of data that tells the compiler how a programmer wants to use the data. Data types can be broadly classified into three categories, primitive, user-defined, and derived data types. T
7 min read
Article Tags :