Errors Within an If Statement

22 ビュー (過去 30 日間)
Han
Han 2020 年 5 月 1 日
回答済み: Hussein 2023 年 8 月 20 日
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 件のコメント
Han
Han 2020 年 5 月 1 日
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 2021 年 7 月 12 日
There are 2 elses:
else strcmp(question3, 'Lessons')
else strcmp(question3, 'Other')%error
You need to change them to an elseif conditional

サインインしてコメントする。

採用された回答

Toder
Toder 2020 年 5 月 1 日
編集済み: Toder 2020 年 5 月 1 日
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 件のコメント
Han
Han 2020 年 5 月 1 日
Nevermind I actually got it to work, thank you so much for your help though! My ends were in the wrong place.
Toder
Toder 2020 年 5 月 1 日
編集済み: Toder 2020 年 5 月 1 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Hussein
Hussein 2023 年 8 月 20 日
Please check one of these possible causes
Syntax errors Logical expression errors: Variable scope issues: Data type mismatches: Incorrect nesting or indentation: Typographical errors:

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by