Clear Filters
Clear Filters

Nested if else statements

13 views (last 30 days)
German Bravo
German Bravo on 2 May 2020
Commented: Steven Lord on 2 May 2020
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;

Accepted Answer

Olawale Ikuyajolu
Olawale Ikuyajolu on 2 May 2020
Edited: Olawale Ikuyajolu on 2 May 2020
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.

More Answers (1)

Stephen23
Stephen23 on 2 May 2020
Edited: Stephen23 on 2 May 2020
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 Comments
German Bravo
German Bravo on 2 May 2020
Edited: German Bravo on 2 May 2020
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 on 2 May 2020
FYI you can smart indenting to detect these types of issues.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!