×

Swift Nested if Statement


Nested if Statement

Swift nested if includes one if block inside another if block.

Syntax :-

if(condition 1)
{
  if(condition 2)
  {
      statements
      if(condition 3)
      {
 
      }
  }
}

Example :-

import UIKit

var a:Int = 6

if(a > 5)
{
   if(a == 6)
   {
     print("a = \(a)")
   }
}

Output :-

a = 6

In this swift program we first initialised a variable 'a' with value '6' . Then if condition checks wether 'a is greater than 6' or not . Since value is greater , block inside if is executed. Then it checks for condition 'a == 6' and since 'a == 6', block inside inner if statement is executed . That's why output 'a = 6' .