Perl: the Markov chain saw | |
PerlMonks |
Re: Short circuits in Logical AND (&&)by davido (Cardinal) |
on Aug 02, 2011 at 06:06 UTC ( [id://917980]=note: print w/replies, xml ) | Need Help?? |
There's a logic error. You want to idle until both A and B are true. But the way you have it constructed, if A is true, then it will stop executing. Or if B is true, it will stop. The only way it can keep going is if both are not true. It's difficult to spot because you're testing for the Boolean truth of a negative. What you're saying is "While A is untrue and B is untrue they both are untrue, so keep looping." But that overlooks if A is true and B is not. If A is true, then it realizes the first term is equal to true which gives you a boolean false in the first term, which collapses your 'and'. If either one of the terms evaluates to Boolean false, the 'and' cannot be true, so the while loop terminates. You probably want:
In other words: "While A is untrue or B is untrue then both A and B cannot be true, so keep looping." In that case, as long as either one of them is not 'true', you continue. The only way to stop is if both of them are not true. It could be much easier to wrap your mind around the logic (at least it is for me) if you simplify it a little by using an until() block. I rarely use them in the until(){} form, but this seems like an ideal situation; especially since you said right at the top of your post "I want to do something until..." (paraphrasing):
Read that as "Until A is true and B is true, keep looping." Dave
In Section
Seekers of Perl Wisdom
|
|