Nested If Statement HELP

1 visualizzazione (ultimi 30 giorni)
Jonathan Ortiz
Jonathan Ortiz il 15 Ott 2020
Commentato: Steven Lord il 15 Ott 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 Commento
Steven Lord
Steven Lord il 15 Ott 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)?

Accedi per commentare.

Risposte (2)

KSSV
KSSV il 15 Ott 2020
Modificato: KSSV il 15 Ott 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 il 15 Ott 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

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by