필터 지우기
필터 지우기

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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by