## Introduction to If-Else Statement in Matlab

- 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.

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

4.5 (2,649 ratings)

View Course

**Output: **

a =10

b =15

b is maximum

Screen 2 shows the Matlab implementation of example 2.

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.

Screen 3 A: Matlab implementation of 3 A

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

### Conclusion

- Use of if-else statement makes complicated conditional problems easy and simple as well as we can create nested if operations to give condition inside one condition.
- In this ,expression includes logical operators like ‘ > ’ ( greater than) , ‘ < ’ ( less than ) , ‘ = = ’ (equal to) , ‘ > = ’ (greater than equal l to) , ‘ < = ’ ( less than equal to) , ‘ && ’ ( logical and ) , ‘ =! ’( not equal to) , ‘ || ’ ( logical or ) , etc.

### Recommended Articles

This is a guide to IF-Else Statement in Matlab. Here we discuss the various examples of the if-else statements in Matlab along with different conditions and code implementation. You may also look at the following articles to learn more –