EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial IF-Else Statement in Matlab
 

IF-Else Statement in Matlab

Priya Pedamkar
Article byPriya Pedamkar

IF-Else Statement in Matlab

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

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

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 –

  1. Switch Statement in Matlab
  2. MATLAB Version
  3. Matlab Operators
  4. Vectors in Matlab
  5. If Statement in Python
  6. Matlab Compiler | Applications of Matlab Compiler
  7. Switch Statement in C#

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW