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

1 visualización (últimos 30 días)
Allison Sims
Allison Sims el 30 de Jul. de 2022
Comentada: Voss el 31 de Jul. de 2022
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

Respuesta aceptada

Voss
Voss el 30 de Jul. de 2022
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 comentarios
Allison Sims
Allison Sims el 30 de Jul. de 2022
Ohh okay that makes sense thank you very much
Voss
Voss el 31 de Jul. de 2022
You're welcome!

Iniciar sesión para comentar.

Más respuestas (1)

James Tursa
James Tursa el 30 de Jul. de 2022
A=1;
:
if A<0
A is not negative, so the body of the if-test never runs.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by