Nested If Statement HELP

1 回表示 (過去 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 Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by