×

Swift if..else Statement


Swift if..else Statement

Swift if..else statement is used to check the condition inside 'if' block first and if that condition is satisfied then if block will be executed otherwise else block will be ex

Syntax :-

if(condition)
{
   if..block statements
}
else
{
   else block statements
}

Example :-

import UIKit

var a:Int = 12

if(a == 10)
{
 print("a = \(a)")
}
else
{
 print("a is not equal to 10")
}

Output :-

a is not equal to 10

In this swift program we first initialised a variable 'a' with value '12' . Then if condition checks wether 'a == 10' or not . Since value does not matches, block inside else is executed. That's why output 'a is not equal to 10' .