position: fixed; top: auto !important; margin-left: 112px;

Switch Decision Making

Sometimes in a program, we need to test a condition. If the condition is true we perform the first action and if the condition is false we perform the second action.

Now, what does that mean?

1) Simple if Statement

if statement is used to test a given condition. If given condition is satisfied then only block inside if statement will get executed.

Example:

  import UIKit

var a:Int = 10

if(a == 10)
{
 print("a = \(a)")
}

Output :-

a = 10

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

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' .

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' .

if..else if..else Statement

Swift if..else if..else is used to check multiple conditions if previous if block condition is not satisfied.

Syntax :-

if(condition 1)
{
  
}
else if (condition 2)
{

}
else 
{

}

Example :-

import UIKit

var a:Int = 4


if(a > 5)
{
    print("a is greater than 5")
}
else if(a < 5)
{
print("a is less than 5")
}
else
{
 print("Try for another value")
}

Output :-

a is less than 5

In this swift program, we first initialized a variable 'a' with value '4' . Then if condition checks whether 'a is greater than 4' or not. Since value is not greater, block inside first if is not executed. Then control flows to else if block. And since the value of 'a' is less than 5 , block inside second else if the condition is executed. That's why output 'a is less than 5'.


Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.