Nested If Statement HELP

1 visualización (últimos 30 días)
Jonathan Ortiz
Jonathan Ortiz el 15 de Oct. de 2020
Comentada: Steven Lord el 15 de Oct. de 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 comentario
Steven Lord
Steven Lord el 15 de Oct. de 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)?

Iniciar sesión para comentar.

Respuestas (2)

KSSV
KSSV el 15 de Oct. de 2020
Editada: KSSV el 15 de Oct. de 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 el 15 de Oct. de 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

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by