IF Statement with Multiple Conditions and priority

14 views (last 30 days)
IDN
IDN on 16 Dec 2021
Answered: Walter Roberson on 16 Dec 2021
I have a set of conditions but one has priority over the other
example:
Data = zeros(size(V));
for t = 1:numel(Data)
if (V>=MAV&XRDiff>0&PXR<=5&XR>=5) %//// Statement1:If this conditions are met entirely then output 1
Data = 1;
if (XR>=10) %/// Statement2:if Satement 1 is False then evalute condition
Data = -1; %//// Statement2:If this conditions are met entirely then output -1
Data = 0; %//// If Statement 2 is FALSE, it means also Statement 1 was FALSE and then i want it to be zero
end
end
end
  1 Comment
Walter Roberson
Walter Roberson on 16 Dec 2021
"Statement2:if Satement 1 is False then evalute condition"
You would have had to use an else to reach statement 2 when statement 1 was false.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 16 Dec 2021
In the C programming language, the scope of an if body is a statement that might be a simple statement or might be a compound block inside {} . So in C if you had
if (A)
this();
that();
then the scope of the if would have ended after the this(); call, and no else is involved, so that() would be called no matter whether the condition is true or false. Those C statements would be equivalent, in C, to
if (A)
{this();}
else
{};
that();
But even in C, an else is not implied by being the next statement after an if body.
In MATLAB, though, the scope of an if body is until the first matching else or elseif or end statement. So in MATLAB,
if (A)
this();
that();
end
would call both this() and that() if the condition (A) is true, and no else would be called. To get an else in MATLAB you need to specify it:
if (A)
this();
else
that();
end

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!