nested if else statements

420 views (last 30 days)
Cyrus on 25 Jul 2013
Commented: Walter Roberson on 30 Apr 2022
Can someone write an example of a nested if else statement? matlab keeps underlining the 'else' when I try to write a nested 'if else' statement telling me the syntax is wrong.
Also, where do I put the 'end' for each of the 'if else' statements?
Jan on 21 Aug 2017
@BISHMITA SHARMA: I assume the problem is solved already, because this thread was written 4 years ago.

Sign in to comment.

Accepted Answer

Evan on 25 Jul 2013
Edited: Evan on 25 Jul 2013
x = 5.5
if x > 6
disp('x is greater than 6')
elseif x >= 3 && x <= 6
if mod(x,1) ~= 0
disp('x is a non-integer value between 3 and 6')
disp('x is a integer value between 3 and 6')
elseif x < 3
disp('x is less than 6')
The above code checks to see what range x falls in, then enters an if/else statement that determines whether it is evenly divisible by one. If not, this means x is not an integer value and the corresponding text is displayed. Here, you could get rid of the nesting if you liked by using statements like elseif x >=3 && x <= 6 && mod(x,1) ~= 0, but eventually it would get messy and difficult to read.
%grade = [];
grade = 75;
if ~isempty(grade)
if grade > 70
disp('Assignment passed!')
disp('Assignment failed!')
disp('No grade found for this assignment!')
Here's another example in which we have to use the nesting in order to check for cases where a blank grade was given.
Jan on 26 Jul 2013
Thanks for the correction, Evan.

Sign in to comment.

More Answers (4)

Nathan Crosty
Nathan Crosty on 25 Jul 2013
Edited: Nathan Crosty on 25 Jul 2013
this = 222;
that = 111;
if this == 333
elseif that == 444
this = 555;
if this == 222
  1 Comment
Evan on 25 Jul 2013
Edited: Evan on 25 Jul 2013
Because the nested if statement contained within the else segment of the the outer statement has no option for "false" values, this would be more clear to display with an if/elseif statement rather than the nested version. Might be a bit misleading to use it as an example.

Sign in to comment.

Sohrab Abedini
Sohrab Abedini on 27 Dec 2015
Edited: Sohrab Abedini on 27 Dec 2015
The examples were great. However, I could not still find a solution to this problem of mine: I don't know how to write it in matlab code->
if n=4 then -> c=2, d=3 or c=3 , d=2.
I wondering if writing "or" is acceptable in result statement. it seems wrong but I have to do it somehow. --- The actual problem is changing a 2nd rank Tensor to a 4th rank tensor through Voigt mapping. which is quite easy vise versa and so difficult in this way. I might be able to solve it if I understand to write "or" or sth like that in if statement.

Nava  Subedi
Nava Subedi on 26 Nov 2016
Edited: Stephen23 on 26 Nov 2016
Can someone help me to what is wrong with my code:
function [l, m, n] = sort3([a b c])
if a>=b && a>=c
if b>=c
l = c;
elseif b<=c
l = b;
else l = a;
elseif b>=a && b>=c
if a>=c
m = c;
elseif a<=c
m = a;
else m = b;
else c>=a && c>=b
if a>=b
n = b;
elseif a<=b
n = a;
else n = c;
I am trying to write nested if statement.
  1 Comment
Stephen23 on 26 Nov 2016
Edited: Stephen23 on 26 Nov 2016
This is not MATLAB syntax:
function [l, m, n] = sort3([a b c])
It looks like you are trying to write Python, or something similar. The correct MATLAB syntax is shown in the online beginners tutorials, the documentation, and lots of online forums:
function [l, m, n] = sort3(a, b, c)
Anyone can read the MATLAB documentation for free. It explains how MATLAB works:

Sign in to comment.

Jobin Geevarghese Thampi
Jobin Geevarghese Thampi on 30 Apr 2022
what is the wrong with this code?. It is supposed to give 22
Walter Roberson
Walter Roberson on 30 Apr 2022
You are showing us the file prac.m but matlab is not going to know to look inside prac.m for the function summa() . You are getting some other summa() function.

Sign in to comment.


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by