# Hello, why Matlab does not check for second condition and give an answer without checking second condition after AND short circuit operator?

조회 수: 2 (최근 30일)
Aknur . 2023년 4월 10일
댓글: Walter Roberson . 2023년 4월 11일
Hello everyone!
Kindly ask help in if statement. If I want to write..
if plane00 ==plane1 OR plane00==plane3 AND plane01==plane1 OR plane01==plane3, then print ('Planes are parallel1')
when I test the code below it only check for one condition isequal(plane00, plane1) || isequal(plane00, plane3)) and then supposed is true if it is true, but did not check second condition after && operator
(isequal(plane01,plane1))||isequal(plane01,plane3)
Thnak you in advance for your time and consideration. Apreciate any help
if (isequal(plane00, plane1) || isequal(plane00, plane3)) && ((isequal(plane01,plane1))||isequal(plane01,plane3))
fprintf('Planes are parallel1:');
ThetaBar = -(Theta0);
PhiBar = -(Phi0);
fprintf('ThetaBar: %f\n',ThetaBar);
fprintf('PhiBar: %f\n',PhiBar);
% I also tried to do smth like this
%(plane01 == 1 || 3 && plane00 == 1 || 3)
##### 댓글 수: 1표시 없음숨기기 없음
Walter Roberson 2023년 4월 11일
Despite your "supposed is true", (isequal(plane00, plane1) || isequal(plane00, plane3)) is probably false.
For debugging, break it into two pieces:
part1 = isequal(plane00, plane1) || isequal(plane00, plane3);
disp(part1)
if part1 && ((isequal(plane01,plane1))||isequal(plane01,plane3))

댓글을 달려면 로그인하십시오.

### 채택된 답변

Hi Aknur,
in the example below, the part after && is evaluated.
BTW, operators || and && are so called short-circuit operators.
https://www.mathworks.com/help/matlab/ref/shortcircuitand.html
plane00 = 1;
plane1 = 1;
plane01 = 3;
plane3 = 3;
if (isequal(plane00, plane1) || isequal(plane00, plane3)) &&...
(isequal(plane01,plane1) || isequal(plane01, plane3))
fprintf('True');
end
True
##### 댓글 수: 2표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
Stephen23 2023년 4월 11일
This is your code run here on the forum. So far it seems that && and || are working as expected.
plane00 = 4;
plane01 = 3;
parallel_planes = [1,3;2,4;5,6];
plane1 = parallel_planes(1,1)
plane1 = 1
plane2 = parallel_planes(1,2)
plane2 = 3
plane3 = parallel_planes(2,1)
plane3 = 2
plane4 = parallel_planes(2,2)
plane4 = 4
plane5 = parallel_planes(3,1)
plane5 = 5
plane6 = parallel_planes(3,2)
plane6 = 6
if (isequal(plane01,plane1)||isequal(plane01,plane3)) && (isequal(plane00, plane1) || isequal(plane00, plane3))
fprintf('Planes are parallel5:');
% ThetaBar = -(Theta0);
% PhiBar = -(Phi0);
% fprintf('ThetaBar: %f\n',ThetaBar);
% fprintf('PhiBar: %f\n',PhiBar);
elseif (isequal(plane00,plane2)||isequal(plane00,plane4)) && (isequal(plane01, plane2) || isequal(plane01, plane4))
fprintf('Planes are parallel2:');
% ThetaBar = -(Theta0);
% PhiBar = -(Phi0);
% fprintf('ThetaBar: %f\n',ThetaBar);
% fprintf('PhiBar: %f\n',PhiBar);
elseif (isequal(plane00,plane5)||isequal(plane00,plane6)) && (isequal(plane01, plane5) || isequal(plane01, plane6))
fprintf('Planes are parallel3:');
% ThetaBar = -(Theta0);
% PhiBar = -(Phi0);
% fprintf('ThetaBar: %f\n',ThetaBar);
% fprintf('PhiBar: %f\n',PhiBar);
else
% ThetaBar = -(Theta0);
% PhiBar = (180 - Phi0);
fprintf('Planes are not parallel:');
% fprintf('ThetaBar: %f\n',ThetaBar);
% fprintf('PhiBar: %f\n',PhiBar);
end
Planes are parallel2:

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

R2021b

### Community Treasure Hunt

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

Start Hunting!