Hello, why Matlab does not check for second condition and give an answer without checking second condition after AND short circuit operator?
10 次查看(过去 30 天)
显示 更早的评论
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))
采纳的回答
Askic V
2023-4-10
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
2 个评论
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)
plane2 = parallel_planes(1,2)
plane3 = parallel_planes(2,1)
plane4 = parallel_planes(2,2)
plane5 = parallel_planes(3,1)
plane6 = parallel_planes(3,2)
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Aerospace Applications 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!