After the if statement is ran why is the answer 10?

조회 수: 1 (최근 30일)
Allison Sims
Allison Sims 2022년 7월 30일
댓글: Voss 2022년 7월 31일
A=1;
B=2;
C=10;
if A<0
C=5;
if B==2
C=3; %I thought the answer would be 3 why is it 10?
end
end
disp(C)
10

채택된 답변

Voss
Voss 2022년 7월 30일
A=1;
B=2;
C=10;
if A<0 % false (1 is not less than 0)
% ... nothing in here executes
end
disp(C) % C remains 10
10
  댓글 수: 2
Allison Sims
Allison Sims 2022년 7월 30일
Ohh okay that makes sense thank you very much
Voss
Voss 2022년 7월 31일
You're welcome!

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

추가 답변 (1개)

James Tursa
James Tursa 2022년 7월 30일
A=1;
:
if A<0
A is not negative, so the body of the if-test never runs.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by