If Else In Swift Programming Language

 IF ELSE IN SWIFT:-

                An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.



When using if, else if, else statements, there are a few points to keep in mind:-
  1. An if can have zero or one else's and it must come after any else if's.
  2. An if can have zero to many else if's and they must come before the else.
  3. Once an else if succeeds, none of the remaining else if's or else's will be tested.
The Swift if-else statement contains two statements: 
  1. if statement and 
  2. else statement.
example:





Output:-

This is a positive number.
This will be executed anyways.

                In swift, if else-if else statement is used to add alternative set of multiple else-if and single else statements for if condition. In case if condition fails (FALSE) then alternatively check for another condition in case if it failed for all defined conditions it will execute else statements.

                If we want to add multiple condition checks in a single program then by using swift if else-if else statement we can easily add multiple conditions. In if else-if else statement, we have a chance to add multiple else if statements but we are restricted to add only one if and else conditions in statement.

if condition1


{


//Execute when it true


}


else if condition2


{


//Execute when other conditions fail and it's true


}


else if condition3


{


//Execute when other conditions fail and it's true


}


else


{


//Execute when all other conditions fail

}

Swift If Else-if Else Statement :-

Following is the simple example using if else if else statement in swift to check the defined conditions before executing the expressions within the condition.


















If you observe above example first we are check if condition whether the value of “num” equals to 15 or not. In case if it failed it will go to next else if condition and check the value of “num” equals to 150 or not even if that condition also failed then it will execute else condition by default.

 When we execute above code in the swift playground we will get a result like as shown below.

 Given Number Equal to 150

The syntax of an if...else if...else statement in Swift is as follows −

if boolean_expression_1 {
   /* Executes when the boolean expression 1 is true */
} else if boolean_expression_2 {
   /* Executes when the boolean expression 2 is true */
} else if boolean_expression_3 {
   /* Executes when the boolean expression 3 is true */
} else {
   /* Executes when the none of the above condition is true */
}

Example:-










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

This is how we can use If Else Statement in Swift Programming Language.



Comments

Popular posts from this blog

POWER BI

Face Recognition

BOOTSTRAP