nested if else statements

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?

6 comentarios

Jan
Jan el 25 de Jul. de 2013
It would be more useful, if you show us your failing code, such that we can suggest a precisely matching solution.
Simone Kolb
Simone Kolb el 5 de Oct. de 2016
you're probably missing "end" to close the conditional
Rohit Bhagwat
Rohit Bhagwat el 1 de Mzo. de 2017
Yes I have rechecked it many times, the number of 'if' and 'else' statements are perfectly matching, still it shows that one of the if statements does not find a end
Walter Roberson
Walter Roberson el 1 de Mzo. de 2017
Rohit, it is difficult for us to give an informed opinion without seeing the file in question.
BISHMITA SHARMA
BISHMITA SHARMA el 21 de Ag. de 2017
May be u are writing the expression in the next line after 'ifelse' try writing it in the same line... because there will be only one 'end', for the first 'if'
Jan
Jan el 21 de Ag. de 2017
@BISHMITA SHARMA: I assume the problem is solved already, because this thread was written 4 years ago.

Iniciar sesión para comentar.

 Respuesta aceptada

Evan
Evan el 25 de Jul. de 2013
Editada: Evan el 25 de Jul. de 2013

4 votos

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')
else
disp('x is a integer value between 3 and 6')
end
elseif x < 3
disp('x is less than 6')
end
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!')
else
disp('Assignment failed!')
end
else
disp('No grade found for this assignment!')
end
Here's another example in which we have to use the nesting in order to check for cases where a blank grade was given.

3 comentarios

Jan
Jan el 25 de Jul. de 2013
Editada: Jan el 25 de Jul. de 2013
No, if ~isempty(grade) && grade > 75 does not produce an error for empty grade. It is the purpose of the && operator to short-circuit, here not to evaluate the right part, if the left part is FALSE already.
Evan
Evan el 25 de Jul. de 2013
Oops! I guess I'm remembering incorrectly. I'll remove the incorrect portion of my code. Hmm... I must've been thinking of a scenario where the class of the variable being compared led to an error in the second condition.
Jan
Jan el 26 de Jul. de 2013
Thanks for the correction, Evan.

Iniciar sesión para comentar.

Más respuestas (4)

Nathan Crosty
Nathan Crosty el 25 de Jul. de 2013
Editada: Nathan Crosty el 25 de Jul. de 2013

0 votos

this = 222;
that = 111;
if this == 333
disp('this')
elseif that == 444
disp('that')
else
this = 555;
if this == 222
disp('this')
else
disp('that')
end
end

1 comentario

Evan
Evan el 25 de Jul. de 2013
Editada: Evan el 25 de Jul. de 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.

Iniciar sesión para comentar.

Sohrab Abedini
Sohrab Abedini el 27 de Dic. de 2015
Editada: Sohrab Abedini el 27 de Dic. de 2015

0 votos

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 el 26 de Nov. de 2016
Editada: Stephen23 el 26 de Nov. de 2016

0 votos

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;
end
elseif b>=a && b>=c
if a>=c
m = c;
elseif a<=c
m = a;
else m = b;
end
else c>=a && c>=b
if a>=b
n = b;
elseif a<=b
n = a;
else n = c;
end
end
I am trying to write nested if statement.

1 comentario

Stephen23
Stephen23 el 26 de Nov. de 2016
Editada: Stephen23 el 26 de Nov. de 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:

Iniciar sesión para comentar.

Jobin Geevarghese Thampi
Jobin Geevarghese Thampi el 30 de Abr. de 2022

0 votos

what is the wrong with this code?. It is supposed to give 22

2 comentarios

Jobin Geevarghese Thampi
Jobin Geevarghese Thampi el 30 de Abr. de 2022
Walter Roberson
Walter Roberson el 30 de Abr. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Scripts en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 25 de Jul. de 2013

Comentada:

el 30 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by