Help: if condition with evaluating multiple statements?
1 次查看(过去 30 天)
显示 更早的评论
Hi everybody,
My sample if condition have 3 expression. As the following code, how can I evaluate statement 2 and statement 3 when the expressions are true?
a=1
b=1
c=3
if c==2
disp('C=2')
elseif a~=c
disp('a~=c') % Statement 2
elseif a~=c && b~=c
disp('a&b~=c') % Statement 3
end
When I run the code, it just showed 'a~=c', eventhough the expression 'a~=c && b~=c' is true.
Thanks.
0 个评论
采纳的回答
Mischa Kim
2014-10-28
编辑:Mischa Kim
2014-10-28
Khanh, only one of the if ( elseif ) statements is executed. In your case it is the second one, because it is the first condition that evaluates true.
Use instead:
if c==2
disp('C=2')
elseif a~=c
if b~=c
disp('a&b~=c') % Statement 3
else
disp('a~=c') % Statement 2
end
end
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!