IF Statement with Multiple Conditions and priority
    조회 수: 7 (최근 30일)
  
       이전 댓글 표시
    
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
  Walter Roberson
      
      
 2021년 12월 16일
				"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.
답변 (1개)
  Walter Roberson
      
      
 2021년 12월 16일
        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
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

