필터 지우기
필터 지우기

Nested If Statement HELP

조회 수: 2 (최근 30일)
Jonathan Ortiz
Jonathan Ortiz 2020년 10월 15일
댓글: Steven Lord 2020년 10월 15일
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
Steven Lord
Steven Lord 2020년 10월 15일
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)?

댓글을 달려면 로그인하십시오.

답변 (2개)

KSSV
KSSV 2020년 10월 15일
편집: KSSV 2020년 10월 15일
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 2020년 10월 15일
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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by