If Else Statement In Swift Programming Language

 


Swift provides the following types of decision making statements.

Swift – If Statement

Example

import Cocoa    var A:Int = 10;    /* Check the boolean condition using if statement */  if A < 20 {     /* If condition is true then print the following */     println("A is less than 20");  }  println("Value of variable A is \(A)");

When we run the above program using playground, we get the following result −

Ais less than 20  Value of variable A is 10

Swift – If… Else Statement

Example

var varA:Int = 100;    /* Check the boolean condition using if statement */  if varA < 20 {     /* If condition is true then print the following */     println("varA is less than 20");  } else {     /* If condition is false then print the following */     println("varA is not less than 20");  }    println("Value of variable varA is \(varA)");  

When the above code is compiled and executed, it produces the following result −

varA is not less than 20  Value of variable varA is 100

Swift – if…else if…else Statement

Example

import Cocoa    var varA:Int = 100;    /* Check the boolean condition using if statement */  if varA == 20 {     /* If condition is true then print the following */     println("varA is equal to than 20");  } else if varA == 50 {     /* If condition is true then print the following */     println("varA is equal to than 50");  } else {     /* If condition is false then print the following */     println("None of the values is matching");  }  println("Value of variable varA is \(varA)");

When the above code is compiled and executed, it produces the following result −

None of the values is matching  Value of variable varA is 100

Swift – Nested If Statements

Example

import Cocoa    var varA:Int = 100;  var varB:Int = 200;    /* Check the boolean condition using if statement */  if varA == 100 {     /* If condition is true then print the following */     println("First condition is satisfied");  	     if varB == 200 {        /* If condition is true then print the following */        println("Second condition is also satisfied");     }   }  println("Value of variable varA is \(varA)");  println("Value of variable varB is \(varB)");

When the above code is compiled and executed, it produces the following result −

First condition is satisfied  Second condition is also satisfied  Value of variable varA is 100  Value of variable varB is 200

Swift – Switch Statement

xample 1

Following is an example of switch statement in Swift programming without using fallthrough −

import Cocoa    var index = 10    switch index {     case 100  :        println( "Value of index is 100")     case 10,15  :        println( "Value of index is either 10 or 15")     case 5  :        println( "Value of index is 5")     default :        println( "default case")  }

When the above code is compiled and executed, it produces the following result −

Value of index is either 10 or 15  

Example 2

Following is an example of switch statement in Swift programming with fallthrough −

import Cocoa    var index = 10    switch index {     case 100  :        println( "Value of index is 100")        fallthrough     case 10,15  :        println( "Value of index is either 10 or 15")        fallthrough     case 5  :        println( "Value of index is 5")     default :        println( "default case")  }

When the above code is compiled and executed, it produces the following result −

Value of index is either 10 or 15  Value of index is 5

Inline If and Else by using ? : Operator

We have covered conditional operator ? : in the previous chapter which can be used to replace if…else statements. It has the following general form −

Exp1 ? Exp2 : Exp3;  

Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.

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.

(Visited 16 times, 1 visits today)
Introduction To Swift Programming

Share This:

Related posts:

Comments

comments

Leave a Comment