Nested If Statement HELP

1 Ansicht (letzte 30 Tage)
Jonathan Ortiz
Jonathan Ortiz am 15 Okt. 2020
Kommentiert: Steven Lord am 15 Okt. 2020
prompt = ' enter a value between 0 and 100 ' ;
x = input(prompt) ;
if x >= 90 && x <= 100
disp (' Grade is A ');
if x <= 89 && x >= 80
disp (' Grade is B ');
end
end
Wondering why it is ignorning my second if statment if I input 85 it wont display " grade is B"
  1 Kommentar
Steven Lord
Steven Lord am 15 Okt. 2020
Others have given you alternatives. For an explanation, as you've written the code the condition of the first if statement must be satisfied for MATLAB to even evaluate the condition of the second if statement. If the first if statement's condition is not satisifed, MATLAB continues execution after the end statement associated with that if statement.
Is there any number that is simultaneously greater than or equal to 90 (from the first part of the first if condition) and less than or equal to 89 (the first part of the second if condition)?

Melden Sie sich an, um zu kommentieren.

Antworten (2)

KSSV
KSSV am 15 Okt. 2020
Bearbeitet: KSSV am 15 Okt. 2020
prompt = ' enter a value between 0 and 100 ' ;
x = input(prompt) ;
if x >= 90 && x <= 100
disp (' Grade is A ');
elseif x <= 89 && x >= 80
disp (' Grade is B ');
end
OR
prompt = ' enter a value between 0 and 100 ' ;
x = input(prompt) ;
if x >= 90 && x <= 100
disp (' Grade is A ');
end
if x <= 89 && x >= 80
disp (' Grade is B ');
end

Sudhakar Shinde
Sudhakar Shinde am 15 Okt. 2020
Use elseif:
prompt = ' enter a value between 0 and 100 ' ;
x = input(prompt) ;
if x >= 90 && x <= 100
disp (' Grade is A ');
elseif x <= 89 && x >= 80
disp (' Grade is B ');
end

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by