In computer programming, we use the if
statement to run a block code only when a certain condition is met.
For example, assigning grades (A, B, C) based on marks obtained by a student.
In Swift, there are three forms of the if...else
statement.
if
statementif...else
statementif...else if...else
statementThe syntax of if
statement in Swift is:
if (condition) {
// body of if statement
}
The if
statement evaluates condition
inside the parenthesis ()
.
condition
is evaluated to true
, the code inside the body of if
is executed.condition
is evaluated to false
, the code inside the body of if
is skipped.Note: The code inside { }
is the body of the if
statement.
Working of if statement
let number = 10
// check if number is greater than 0
if (number > 0) {
print("Number is positive.")
}
print("The if statement is easy")
Output
Number is positive.
The if statement is easy
In the above example, we have created a variable named number
. Notice the test condition,
number > 0
Here, since number is greater than 0, the condition evaluates true
.
If we change the variable to a negative integer. Let's say -5.
let number = -5
Now, when we run the program, the output will be:
The if statement is easy
This is because the value of number
is less than 0. Hence, the condition evaluates to false
. And, the body of if
block is skipped.
An if
statement can have an optional else
clause.
The syntax of if-else
statement is:
if (condition) {
// block of code if condition is true
}
else {
// block of code if condition is false
}
The if...else
statement evaluates the condition
inside the parenthesis.
If the condition evaluates to true,
if
is executedelse
is skippedIf the condition evaluates to false,
else
is executedif
is skippedWorking of if...else statement
let number = 10
if (number > 0) {
print("Number is positive.")
}
else {
print("Number is negative.")
}
print("This statement is always executed.")
Output
Number is positive.
This statement is always executed.
In the above example, we have created a variable named number
. Here, the test expression
number > 0
Since the value of number
is 10
, the test expression evaluates to true
. Hence code inside the body of if
is executed.
If we change the variable to a negative integer. Let's say -5.
let number = -5
Now if we run the program, the output will be:
Number is negative. This statement is always executed.
Here, the test expression evaluates to false
. Hence code inside the body of else
is executed.
The if...else
statement is used to execute a block of code among two alternatives.
However, if you need to make a choice between more than two alternatives, then we use the if...else if...else
statement.
The syntax of the if...else if...else
statement is:
if (condition1) {
// code block 1
}
else if (condition2){
// code block 2
}
else {
// code block 3
}
Here,
true
, code block 1 is executed.false
, then condition2 is evaluated.true
, code block 2 is executed.false
, code block 3 is executed.Working of if...else if statement
// check whether a number is positive, negative, or 0.
let number = 0
if (number > 0) {
print("Number is positive.")
}
else if (number < 0) {
print("Number is negative")
}
else {
print("Number is 0.")
}
print("This statement is always executed")
Output
Number is 0.
In the above example, we have created a variable named number with the value 0. Here, we have two condition expressions:
if (number > 0)
- checks if number
is greater than 0
else if (number < 0)
- checks if number
is less than 0
Here, both the conditions evaluate to false
. Hence the statement inside the body of else
is executed.
You can also use an if
statement inside of an if
statement. This is known as a nested if statement.
The syntax of nested if statement is:
// outer if statement
if (condition1) {
// statements
// inner if statement
if (condition2) {
// statements
}
}
Notes:
else
and else if
statements to the inner if
statement as required.if
statement inside the outer else
or else if
statements(if they exist)if
statements.var number = 5
// outer if statement
if (number >= 0) {
// inner if statement
if (number == 0) {
print("Number is 0")
}
// inner else statement
else {
print("Number is positive");
}
}
// outer else statement
else {
print("Number is negative");
}
Output
Number is positive
In the above example, we have used a nested if statement to check whether the given number is positive, negative, or 0.