Categories
2. Control Statements

If-Else Statement

  • If the statement executes code or statement block only when the condition is true. It is a conditional programming keyword used to give conditions to the program on Matlab.
  • It has three parts if statement, else statement and else if statement if-else statement in Matlab.
  • If the first expression or condition is true then ‘ if ’ statement executes. If the expression is false then else statement executes. And if there are multiple conditions in code then else if the statement is used in Matlab.

Syntax:

If (condition)
Statement
Else
Statement
end

Examples of If-Else Statement in Matlab

Here are some examples of the if-else statement in Matlab which are given below:

Example 1 – Simple If-Else Statements

let us consider an example to find a large or less than a specific number.

If a = 5 then we will find the number a is less than 10 or not.

Code:

a = 5
if ( a < 10 ) - - - - - condition 1
disp ( ' number is less than 10 ' ) - - - - - condition 1 is true
else
disp ( ' number is large than 10 ' ) - - - - - condition 1 is false
end

Output:

a  =  5

the number is less than 10

Screen 1 shows the Matlab implementation of example 1.

IF-Else Statement in Matlab example1
IF-Else Statement in Matlab example2

Screen 1: Matlab implementation of example  1

Example 2 – Comparison of Two Numbers

Consider the second example to find out the maximum of two numbers. Let us take two number  ‘ a ’ and  ‘ b ’.

Code:

a = 10 and b = 15
clc ;
a = 10
b = 15
if ( a > b ) - - - - - condition 1
disp ( ' a is maximum ' ) - - - - - condition 1 is true
else
disp (' b is minimum ' ) - - - - - -condition 1 is false
end

Output: 

a =10

b =15

b is maximum

Screen 2 shows the Matlab implementation of example 2.

IF-Else Statement in Matlab example2

Screen 2: Matlab implementation of example 2

Example 3 – Use of Nested if  Statement

In this example, we will see a maximum of three numbers, let us consider three numbers a, b  and c. a = 10 , b = 15 and c = 20.

Code:

clc ;
a = 10
b = 15
c = 20
if ( a > b ) - - - -condition 1
if ( a > c ) - - - nested if condition 2
disp ( ' a is maximum ' ) . . . .if condition 2 is true
else
disp ( ' c is maximum ' ) - - - -if condition 2 is false
end
end
if ( a < b ) - - - - - -condition 3
if ( b > c ) - - - - nested if condition 4
disp ( ' b is max ' ) if condition 4 is true
else
disp('c is max') if condition 4 is false
end
end

Output:

a  =10

b =15

c =20

Ans  =1

c is max

Screen 3 A shows the Matlab code of example 3 and screen 3 B shows the output of example 3.

Matlab example3

Screen 3 A: Matlab implementation of 3 A

Matlab example3.1

Screen 3 B: the output of example 3

Example 4 – Use of Logical Operators

Now let us consider one example to check the given number is within range or not.

In this example, we will see the use of the logical expression in if-else statements.

Code:

1. If a = 10

Clc ;
a = 10
min = 2
max = 20
if ( a > = min ) & & ( a < = max )
disp ( ' a is within range ' )
elseif ( a < = min )
disp ( ' a is less than minimum ' )
else
disp ( ' a is more than maximum value ' )
end

Output:

a =10

min =2

max =20

a is within range

2. If the value of a = 50

Code:

clc ;
a = 50
min = 2
max = 20
if ( a >= min ) & & ( a < = max )
disp ( ' a is within range ' )
elseif ( a < = min )
disp ( ' a is less than minimum ' )
else
disp ( ' a is more than maximum value ' )
end

Output:

a =50

min =2

max =20

a is more than the maximum value

3. If the value of a = 1

Code:

clc ;
a = 1
min = 2
max = 20
if ( a > = min ) & & ( a < = max )
disp ( 'a is within range ' )
elseif ( a < = min )
disp ( ' a is less than minimum ' )
else
disp ( ' a is more than maximum value ' )
end

Output: 

a =1

min =2

max =20

a is less than a minimum

Leave a Reply

Your email address will not be published. Required fields are marked *