Errors Within an If Statement

12 visualizaciones (últimos 30 días)
Han
Han el 1 de Mayo de 2020
Respondida: Hussein el 20 de Ag. de 2023
I keep getting these errors and I'm not sure why. Could somebody check it over and see where I'm exactly going wrong?
function questionsasked()
question1=questdlg('Are you ready to start?',...%question box
'Questions',...
'Yes','No','No');
question2=questdlg('Do you know the keys on the piano?',...
'Questions',...
'Yes','Somewhat','No','No');
question3=questdlg('How did you learn?',...
'Questions',...
'Self-Taught','Lessons','Other','Other')
question4=questdlg('How long did you take lessons for?',...
'Questions',...
'<1 year','1-10 years','>10 years','>10 years')
question5=questdlg('Are you still taking lessons?',...
'Questions',...
'Yes','No','No')
%user must click yes in order to continue
while ~strcmp(question1,'No')%<SM:WHILE>
if strcmp(question1,'Yes')
if strcmp(question2,'Yes')
if strcmp(question3,'Self-Taught')
else strcmp(question3, 'Lessons')
if strcmp(question4,'<1 Year')
if strcmp(question5,'Yes')
else strcmp(question5,'No')
end
else strcmp(question4,'1-10 year')
if strcmp(question5,'Yes')
else strcmp(question5,'No')
end
else strcmp(question4, '>10 years') %error
if strcmp(question5,'Yes')
else strcmp(question5,'No')
end
else strcmp(question3, 'Other')%error
else strcmp(question2,'No')%error
end
end
end
end
question1=questdlg('Are you ready to start?',...%question box
'Questions',...
'Yes','No','No');
end
  3 comentarios
Han
Han el 1 de Mayo de 2020
It says parse error at else. But not all of the else's, only that ones that have the error comment. I'm unsure as to what this means.
Charleston Chan
Charleston Chan el 12 de Jul. de 2021
There are 2 elses:
else strcmp(question3, 'Lessons')
else strcmp(question3, 'Other')%error
You need to change them to an elseif conditional

Iniciar sesión para comentar.

Respuesta aceptada

Toder
Toder el 1 de Mayo de 2020
Editada: Toder el 1 de Mayo de 2020
For if elsif else conditionals, Matlab executes the statements in the first block who's logical expression true, then it jumps to end. Only one block of statements is executed. The else is synonymous with "otherwise". It has the statments which are executed if all the previous if and elseif conditionals fail, and only one else can be paired with one if.
Your error is because of the second else. You can have as many elseif conditionals as you like, they just need to appear between the if and the else. I think elseif is what you intend to use in your code. Hopefully this example is helpful.
if x < 5
disp('less than 5')
elseif x > 5
disp('greater than 5')
else
disp('equal to 5')
end
Suppose we set x=6 and then execute this code. Matlab first evaluates the expression x<5, which is not true, so it does not execute disp('less than 5'). Next it evaluates the expression x>5, which is true, so it executes disp('greater than 5') and jumps to end.
Now suppose we instead set x=5. Matlab first evaluates x<5, which is false, so it moves to the elseif conditional. x>5 is false, so it moves to the next block. The last block does not have any expression to evaluate (else statement are NOT allowed to have a logical expression), so disp('equal to 5') is executed, then Matlab jumps to end.
  5 comentarios
Han
Han el 1 de Mayo de 2020
Nevermind I actually got it to work, thank you so much for your help though! My ends were in the wrong place.
Toder
Toder el 1 de Mayo de 2020
Editada: Toder el 1 de Mayo de 2020
Glad I could help, and glad you got it working. For the future, if you find yourself using a lot of elseifs, you may want to consider using a switch. You can read about them in the documentation; they are just a lot easier to read than a whole bunch of elseifs.

Iniciar sesión para comentar.

Más respuestas (1)

Hussein
Hussein el 20 de Ag. de 2023
Please check one of these possible causes
Syntax errors Logical expression errors: Variable scope issues: Data type mismatches: Incorrect nesting or indentation: Typographical errors:

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by