How to use two logical operation inside a if condition?
Show older comments
I have three statements in a if condition and they are executed when the logical operations are true. For example I have two variables a and b, and their values are:
a=7;
b=5;
if a=7 && b=a || b<a
do something
end
Can someone please tell me what is the correct way to perform second operation (b=a | | b<a) first, and then do && operation between the result and the first statement (a=7)?
Accepted Answer
More Answers (1)
Image Analyst
on 19 Aug 2017
Like this:
a=7;
b=5;
if a==7 && (b==a || b<a)
% do something
end
Make sure you use double equals for comparison, rather than single equals which does assignment.
Since a has to equal 7, the if won't be true unless b is equal to or less than 7. So you could do this
if a == 7 && b <= 7
which is the same thing.
1 Comment
Asim Ismail
on 19 Aug 2017
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!