Open In App

Swift – If-else-if Statement

Last Updated : 11 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Swift, the if-else if-else condition is used to execute a block of code among multiple options. It gives a choice between more than two alternative conditions as soon as one of the conditions is true, the statement associated with that if is executed. If all conditions are false or not satisfied then the final else statement will be executed in the program. 

Syntax: 

if (condition1){
    // Block of code and Statements
}
else if (condition2){
    // Block of code and Statements
}
else if (condition3){
    // Block of code and Statements
}
.
.
.
else {
    // Block of code and Statements
}

Here, If condition1 is true, then statement 1 is executed. If condition1 is false, then it will go to else if statement and evaluate condition 2. If condition 2 is true, statement 2 will be executed. If condition 2 is false, then it will go to else if statement and evaluate condition 3 likewise so on. If neither condition is true it will move to the else part and then the final else statement will be executed.

Flowchart: 

Example 1: Swift program illustrate the use of if-else-if statement.

Swift




let number = 85
 
if (number >= 90){
    print("Grade A")
}
else if (number >= 75) {
    print("Grade B")
}
else if (number >= 60) {
    print("Grade c")
}
else {
    print("Grade D")
}


Output:

Grade B

Explanation: In the above example, first we create a variable named “number”. Now using the if-else-if statement we check if the number is greater than or equal to 90, assigning grade A. Or if the number is greater than or equal to 75, assign grade B. Or if the number is greater than or equal to 60, assign grade C. So here the output will be Grade B because number = 85 which satisfies the condition “number is greater than or equal to 75”.

Example 2:

Swift




let number = 20
 
// Condition 1
if (number == 10){
    // Print statement
    print("Number is 10")
}
// Condition 2
else if (number == 15){
    // Print statement
    print("Number is 15")
}
// Condition 3
else if (number == 20){
    // Print statement
    print("Number is 20")
}
else{
    // Print statement
    print("Number is not present")
}


Output: 

Number is 20 

Explanation: In the above example, we created a variable that holds an expression and we have three condition expressions:  

  • if (number == 10) : checks if number equal to 10
  • else if (number == 15) : checks if number is equal to 15.
  • else if (number == 20) : checks if number is equal to 20.

Here, both conditions1 and condition2 are false. Hence the statement will move to condition3(condition3 is true) and statement 3 is executed.



Previous Article
Next Article

Similar Reads

Swift - If-else Statement
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
3 min read
Swift - Nested if-else Statement
In Swift, a situation comes when we have to check an if condition inside an if-else statement then a term comes named nested if-else statement. It means an if-else statement inside another if statement. Or in simple words first, there is an outer if statement, and inside it another if - else statement is present and such type of statement is known
4 min read
Swift - Convert String to Int Swift
Swift provides a number of ways to convert a string value into an integer value. Though, we can convert a numeric string only into an integer value. In this article, we will see the two most common methods used to convert a string to an integer value. A string is a collection of characters. Swift provides String data type with the help of which we
3 min read
Swift - Break Statement
The break statement is a loop control statement that is used to end the execution of an entire control flow statement immediately when it is encountered. When the break condition is true, the loop stops its iterations, and control returns immediately from the loop to the first statement after the loop. In simple words, break statement breaks the cu
6 min read
Swift - If Statement
Just like other programming languages in Swift language also if statement is used to execute the program based on the evaluation of one or more conditions or in other words if statement is used to run a piece of code only when the given condition is true. There are also known as branch statement. For example, let us consider a case where you’re goi
3 min read
Swift - Fallthrough Statement
Just like other languages in Swift also switch statement is very useful and powerful. It takes a value and then compare it with several possible matching pattern or we can say cases and stops its execution when it successfully found its first matching case statement and then execute the code present in the matched statement. It optimise code by avo
4 min read
Swift - Switch Statement
In Swift, a switch statement is a type of control mechanism that permits a program to change its flow of control and take some decisions on the basis of some conditions imposed on the value of a variable. The control flow of a program comes out of the switch statement block as soon as the matching condition is met. It also provides a default condit
10 min read
Swift - Guard Statement
Swift provides a special type of statement referred to as "guard" statement. A guard statement is capable to transfer the flow of control of a program if a certain condition(s) aren't met within the program. Or we can say, if a condition expression evaluates true, then the body of the guard statement is not executed. In other words, the control flo
15 min read
Swift - Typealias
A variable is a named container that is used to store a value. Now each value has a type to which it belongs and is known as data type. A data type is a classification of data that tells the compiler how a programmer wants to use the data. Data types can be broadly classified into three categories, primitive, user-defined, and derived data types. T
7 min read
Swift - Difference Between Function and Method
Some folks use function and method interchangeably and they think function and method are the same in swift. But, function and method both are different things and both have their advantages. In the code, Both function and method can be used again but methods are one of these: classes, structs, and enums part, whereas functions do not belong to any
4 min read
Article Tags :
three90RightbarBannerImg