Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Swift – If-else Statement

  • Last Updated : 18 Oct, 2021

Just like other programming languages in Swift language also support the if-else statement. In the if-else statement, when the given condition is true then the code present inside the if condition will execute, and when the given condition is false then the code present inside the else condition will execute. Or in other words if “if” statement is having an optional “else” statement also that provides an alternative task to perform in case the condition of “if” is false, then it is better known as if…else statement. Such type of statement is very useful in programming because they allow programmers to implement conditional behavior into their programs. For example, consider a case where you’re going to go to the market, and your father tells you, “if they have Biscuits on sale, then buy Biscuits else buy Chips”. Now it is a conditional statement where you’ll execute some action (“buy some Biscuits”) only if the condition (“they have Biscuits on sale”) is true. If the condition is false (i.e. “they don’t have Biscuits on sale”) you’ll execute another action (i.e. “buy some Chips”).

Syntax:

span

if (condition)

{



    // Body of if statement

}

else 

{

    // Body of else statement

}

The if…else statement evaluates the condition inside the parenthesis () and the code present inside the curly braces{}.

  • When the condition of the if statement is true, then the code present inside the if statement will execute and the control jumps to the next statement present after the else block.
  • When the condition of the if statement is false, then the code present inside the else statement will execute and the control jumps to the next statement present after the else block. Here the code present inside the if statement will not execute.

It can be better understood via visual diagram as follows :



Example 1:

Swift

// Swift program to demonstrate the use of if-else statement

// Declare and initialize a variable
let val = 40
// Check if number is equal to 40
if (val == 40)
{
print(“Both the numbers are equal”)
}

// Else block
else
{
print(“Both the numbers are not equal”)

}

// Code after if…else statement
// This statement is always executed
print(“Learning if…else statement in Swift.”)

Output :

Both the numbers are equal
Learning if...else statement in Swift.

Explanation: In the above example, we have created a variable named “val”. Observe the test condition, “val == 40”. Here, since val is equal to 40, the condition evaluates true hence control flow of the program enters the body of if statement and executes code/statements written inside it. Now if we change the val = 45 and run the program the condition evaluates false hence control flow of the program enters the body of the else statement and executes code/statements written inside it, i.e., “Both the numbers are not”.

Example 2:

Swift

// Swift program to demonstrate the use of if-else statement

// Declare and initialize a variable
let age = 80

// Checking if age is greater than equal to 18
if (age >= 18)
{
print(“This person is eligible for voting”)
}

// Else block
// It will execute when the condition is false
else
{
print(“This person is not eligible for voting”)
}

print(“Only 18+ peoples are eligible for voting”)

Output:

This person is eligible for voting
Only 18+ peoples are eligible for voting
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!