Aller au contenu

Photo

I don't fully understand the logic in this instance...


  • Veuillez vous connecter pour répondre
6 réponses à ce sujet

#1
M. Rieder

M. Rieder
  • Members
  • 2 530 messages
I had an event that should not happen in area A or in area B, so I made the conditional:


if ((is not in area A)|| (is not in area B))
      {
      do stuff.
       }


But it didn't work properly.  So I switched the code to this:


if ((is not in area A)&&(is not in area B))
{
do stuff.
}

and it worked. 

To me this is confusing because the first one seems to say if it isn't area A or B then do stuff, and the second seems to say if it isnt area A and B (which seems impossible) then do stuff.

Can anyone explain this to me?

#2
Dann-J

Dann-J
  • Members
  • 3 161 messages
The first one would always fire, since one of the OR alternatives is always correct. If you are in Area A, then the 'not in Area B' is true. If you are in Area B, then the 'not in Area A' is true. If you are in neither Area A nor B, then both are true. The OR separates the two checks, so any one of them being true allows the function to fire.

The second one only fires if you are in neither Area A nor Area B. If you are in one or the other, then the AND clause fails (since it connects the two checks).

In essence:
OR separates the conditions so they are independant. Any one of them being true fires the function.
AND connects the conditions so they depend on each other. They must all be true to fire the function.


It would have worked if you'd used:

if ((is in area A)|| (is in area B))
  return;
[otherwise do something]

Modifié par DannJ, 19 août 2011 - 12:59 .


#3
Morbane

Morbane
  • Members
  • 1 883 messages
They refer to this phenomenon as a "shortcircuit" in programmers lingo

#4
Kaldor Silverwand

Kaldor Silverwand
  • Members
  • 1 603 messages
Formal logic:
p AND q = True if and only if both p and q are True
p OR q = True if either p or q are True

NOT (p AND q) is equivalent to NOT p OR NOT q
NOT (p OR q) is equivalent to NOT p AND NOT q

So the phrase "not in either area A or B" is equivalent to NOT in area A AND NOT in area B. It can be written in two equivalent forms:
!(isArea(A) || isArea(B))
(!(isArea(A)) && !(isArea(B)))

Interpreters can be clever enough to only check the necessary parts of a multi-element if statement to determine whether the statement is true or false. So as soon as the determination can be made the interpreter will skip any further checks because they are needless. If there is a multi-element OR statement then as soon as one element is determined to be true then the statement as a whole is true, and for a multi-element AND statement as soon as one element is determined to be false then the statement as a whole is false. This is short-circuiting.

Regards

#5
M. Rieder

M. Rieder
  • Members
  • 2 530 messages
It makes sense now. Thanks. I just wasn't thinking it the whole way through.

#6
kevL

kevL
  • Members
  • 4 078 messages
* dvvvvzzzt-t-t-t- *

Your system has shut down. It will need to be rebooted.

#7
The Fred

The Fred
  • Members
  • 2 516 messages
You could write:
if( ! (is in area 1 OR is in area 2) )

The key is the brackets.
Look at your second attempt:
if ((is not in area A)&&(is not in area B))
Just read it:
"if is not in area A and is not in area B". That's the same as saying "if is not in area A or B".
In turn, we could convert that back into what I wrote above.

Your first attempt, by comparison, was saying "if we're not in area A or we're not in area B". The problem is that if you're in area B, you're not in area A, so it returns true.

EDIT: Formal logic like Kaldor's there is pretty good stuff, but if you find it a bit confusing, simply converting what you've written into words can make it make a lot more sense.

Modifié par The Fred, 21 août 2011 - 05:54 .