multiple logical operators in if statement
26 次查看(过去 30 天)
显示 更早的评论
Hi, I have a quick question. Is it possible to write multiple logical conditions in if statement like this;
if
(s < (3 * a + b) * 0.25) || (s > b) ||...
(mflag) && (abs(s-b) >= (abs(b-c) * 0.5)) ||...
(~mflag) && (abs(s-b) >= (c-d) * 0.5) ||...
(mflag) && (abs(b-c) < tol) ||...
(~mflag) && (abs(c-d) < tol)
% bisection method
s = (a+b)*0.5;
mflag = true;
else
mflag = false;
end
0 个评论
采纳的回答
Star Strider
2017-1-17
It’s definitely possible to write it because you just demonstrated that by writing it.
You have a series of logical tests that appear to be constructed correctly, providing that all the arguments are scalars.
The question is if it makes sense logically and does what you want it to do. Only you can determine that.
0 个评论
更多回答(1 个)
Walter Roberson
2017-1-17
Certainly.
Note: && has higher priority than || so your sections THIS && THAT || SOMETHING && SOMETHINGELSE would be evaluated as (THIS && THAT) || (SOMETHING && SOMETHINGELSE)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!