Beefy Boxes and Bandwidth Generously Provided by pair Networks
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??


in reply to Short circuits in Logical AND (&&)

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:

while( $worker1Finished ne 'true' or $worker2Finished ne 'true' ) { #... }

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):

until( $worker1Finished eq 'true' and $worker2Finished eq 'true' ) { print "."; sleep 1; }

Read that as "Until A is true and B is true, keep looping."


Dave

Replies are listed 'Best First'.
Re^2: Short circuits in Logical AND (&&)
by vishi (Beadle) on Aug 02, 2011 at 06:36 UTC

    Oh!! I generally dont like to use the "until" block .. but as you said, this may be a situation that's ideal for its use. If in case I'd want to write this with a while block, would this logic given below work?

    while(!(($worker1Finished eq "true")&&($worker2Finished eq "true"))) { print "."; sleep(1); }

    Am just trying to learn the different cases for using the "while" and "until" constructs ... So, please give me your suggestions/advice.

      Yes, there are a lot of ways to write it correctly, and a lot of incorrect ways too. ;) The one you proposed should work fine. But I would use the lower precedence operators:

      while( not ( $worker1Finished eq 'true' and $worker2Finished eq 'true' ) ) { # ... }

      ...to at least eliminate one set of parens. In fact, if my memory of the precedence tables serves me (which it may not), you could eliminate the other set of parens like this:

      while( not $worker1Finished eq 'true' && $worker2Finished eq 'true' ) { # ... }

      Because 'eq' is higher precedence than &&, and && is higher precedence than 'not', so eq binds first, && second, and 'not' last.

      But the gods gave us 'until(){}' because it suited them to do so. While it's important to use caution when wielding negatives (and double negatives, etc.), it is useful sometimes. My preference in this case is to use it.

      until( $worker1Finished eq 'true' and $worker2Finished eq 'true' ) { # ... }

      Personal preference. But to me "until this and that keep looping" reads better than "while not both this and that keep looping."


      Dave

        Fantastic Explanation! Thanks for your patience .. that answer gave me a very good insight on using conditionals and operators. Yes, your way of eliminating paranths holds as per precedence. Wow.. wondering when I will start thinking like you when I write code... (sigh)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://917980]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-24 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found