フィルターのクリア

Nested if else statements

13 ビュー (過去 30 日間)
German Bravo
German Bravo 2020 年 5 月 2 日
コメント済み: Steven Lord 2020 年 5 月 2 日
When I try to run the code shown below I get the following error: Error: File: iftest.m Line: 16 Column: 1
Illegal use of reserved keyword "else". This is a file that I saved yesturday without the else statement and the file was able to run, but this morning when I added the else statement I got the error I mention above. I was able to make the code run only after re-typing it on a different script, I typed the exact same lines. Why is matlab not able to connect the else and if statements on the original script? and how can I fix this problem without having to re-type the entire thing to a new file?
%Get the user input
y = input('Enter a number between 1 and 10:');
%If the user entered a value outside the range change it appropiately.
if y > 10 || y < 1
fprintf('The number you entered is outsie the range. It will be changed.\n')
if y > 10
y = 10;
fprintf('The number has been changed to 10.\n');
end;
if y < 1
y = 1;
fprintf('The number has been changed to .\n')
end;
end;
else
fprintf('The number is in the range\n')
end;

採用された回答

Olawale Ikuyajolu
Olawale Ikuyajolu 2020 年 5 月 2 日
編集済み: Olawale Ikuyajolu 2020 年 5 月 2 日
you have three if statements and three end statements to close the conditions before introducing another else. So just delete the end above the else statement.

その他の回答 (1 件)

Stephen23
Stephen23 2020 年 5 月 2 日
編集済み: Stephen23 2020 年 5 月 2 日
Badly indented code hides bugs.
Your code is badly indented.
Your badly indented code hides the bug that causes that error message.
Indenting the code consistently makes the cause of the bug easy to see:
%Get the user input
y = input('Enter a number between 1 and 10:');
%If the user entered a value outside the range change it appropiately.
if y > 10 || y < 1
fprintf('Seriously? The number you entered is outsie the range man!!!. It will be changed.\n')
if y > 10
y = 10;
fprintf('The number has been changed to 10.\n');
end;
if y < 1
y = 1;
fprintf('The number has been changed to .\n')
end;
end; % <- matches the first IF statement. Probably you should delete this.
else % <- does not match any IF statement !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
fprintf('The number is in the range\n')
end;
Exactly as the error message tells you, your else does not match anything.
"I was able to make the code run only after re-typing it on a different script, I typed the exact same lines."
I doubt that.
"Why is matlab not able to connect the else and if statements on the original script?"
Because unmatched else statements cannot be matched to anything. Basically your code is like this:
if ...
end
else
What do you expect MATLAB to match that else to?
"and how can I fix this problem without having to re-type the entire thing to a new file?"
  1. Fix the bug in your code.
  2. Indent your code consistently so that you can find bugs like this yourself.
  2 件のコメント
German Bravo
German Bravo 2020 年 5 月 2 日
編集済み: German Bravo 2020 年 5 月 2 日
I missed that end statement, I thought I had writen the exact same code but obviously I did not.
I appreciate the comment about the indentation as well, I will be more careful about that from now on.
thank you for your help.
Steven Lord
Steven Lord 2020 年 5 月 2 日
FYI you can smart indenting to detect these types of issues.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by