Nested if else statements
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
German Bravo
am 2 Mai 2020
Kommentiert: Steven Lord
am 2 Mai 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;
0 Kommentare
Akzeptierte Antwort
Olawale Ikuyajolu
am 2 Mai 2020
Bearbeitet: Olawale Ikuyajolu
am 2 Mai 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.
0 Kommentare
Weitere Antworten (1)
Stephen23
am 2 Mai 2020
Bearbeitet: Stephen23
am 2 Mai 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?"
- Fix the bug in your code.
- Indent your code consistently so that you can find bugs like this yourself.
2 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!