Swift If, Else If, Else Statement

  Total Views : 343
  Comments : 0
  Keywords : Swift if else if else statement is used to execute if condition when it true else check for another if condition otherwise execute else condition with examples

Here we will learn if, else-if, else conditions or statements in swift with examples and how to use swift if else-if else statements to execute set of statements if anyone of defined condition is TRUE otherwise execute else condition statements with examples.

Swift If Else-If Else Statement

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 the it will execute else statements.

 

If we want to add multiple condition checks in 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.

 

Now we will see the functionality of if else-if else statement in swift programming language using algorithm diagram.

Swift If Else-if Else Statement Flow Diagram

Following is the swift if else-if else statement flow diagram to represent how the functionality of if else-if else statement will work in swift programming language.

 

Swift If Else-If Else Statement flowchart diagram with examples

 

If you observe above swift if else-if else statement flow diagram first it will evaluates the first condition whether is it TRUE or FALSE. In case if first condition is TRUE then it will execute the code block within if statement. In case the given condition is FLASE then it will go for another condition and check for whether it’s TRUE or FALSE based on the status and it will execute required statements.

 

In case if all defined if and else-if conditions fails then the else condition statements will execute and reach the end of statement.

Syntax of Swift If Else-If Else Statement

Following is the syntax of if else-if else statement in swift programming language.

 

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

}

Now we will see how to use if else-if else statement in swift programming language with examples.

Swift If Else-if Else Statement Example

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

 

var num = 200

if num == 10 {

print("Given Number Equal to 10.")

}

else if num == 200 {

print("Given Number Equal to 200")

}

else {

print("Fail")

}

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

 

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

 

Given Number Equal to 200

This is how we can use swift if else-if else statements in swift programming language to define multiple conditions based on our requirements.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User