Hello, why Matlab does not check for second condition and give an answer without checking second condition after AND short circuit operator?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
Walter Roberson
il 11 Apr 2023
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))
Risposta accettata
Askic V
il 10 Apr 2023
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 Commenti
Stephen23
il 11 Apr 2023
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
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!