12-11-2022, 05:39 PM
wow I googled and found the order of execution of the logical operator in QB
from first to last: NOT AND OR XOR EQV IMP
is this the same order of execution made by QB64pe? I think so, but I wait for developers response.
if so the above expression is
1. iActive AND ImouseX<500
2. result OR ImouseX>600
3. result OR ImouseY < 200
4. result OR ImouseY> 300
so in the case iActive = 0 and ImouseX = 400 and ImouseY = 100
1. 0 AND -1 --->0
2. 0 OR 0 --->0
3. 0 OR -1 --->-1
4. -1 OR 0 --->-1
Here there is a logical error if you need that iActive must be True to execute the block of code.
(here is like you are saying: if it is active and the mouse is at least at one of these positions (X<500, X>600, Y<200, Y>300) you do this...)
but using the ( ) ,surrounding the OR group of conditions, you can avoid this unwanted result... if you use AND is because you want that all the 2 parameters must be true to get back true as result.
while you use EQV when it is important to you to manage all the conditions that are both F or both T.
Here a test code that compares expression original AND + 3 OR ,with the use of () AND +(3 OR), the use of EQV (EQV + 3 OR), the use of () (EQV + (3OR))
so the expression must pay attention to the order of execution of Logical operators to return the wanted result. As for all expression we can use () to modify the order of calculation.
from first to last: NOT AND OR XOR EQV IMP
is this the same order of execution made by QB64pe? I think so, but I wait for developers response.
if so the above expression is
1. iActive AND ImouseX<500
2. result OR ImouseX>600
3. result OR ImouseY < 200
4. result OR ImouseY> 300
so in the case iActive = 0 and ImouseX = 400 and ImouseY = 100
1. 0 AND -1 --->0
2. 0 OR 0 --->0
3. 0 OR -1 --->-1
4. -1 OR 0 --->-1
Here there is a logical error if you need that iActive must be True to execute the block of code.
(here is like you are saying: if it is active and the mouse is at least at one of these positions (X<500, X>600, Y<200, Y>300) you do this...)
but using the ( ) ,surrounding the OR group of conditions, you can avoid this unwanted result... if you use AND is because you want that all the 2 parameters must be true to get back true as result.
while you use EQV when it is important to you to manage all the conditions that are both F or both T.
Here a test code that compares expression original AND + 3 OR ,with the use of () AND +(3 OR), the use of EQV (EQV + 3 OR), the use of () (EQV + (3OR))
Code: (Select All)
' we make some tests
Rem AND versus EQV
Rem wiki info https://qb64phoenix.com/qb64wiki/index.php/EQV
DefInt I
Dim iActive, ImouseX, ImouseY
iActive = 0
Locate 1, 1: Print " iActive ImouseX ImouseY"
For iActive = -1 To 0 Step 1
' the first run has iActive true while the second turn has iActive false
ImouseX = 400: ImouseY = 100 ' ImouseX < 500 and ImouseY <200 are true
Locate , 7: Print " "; iActive; " "; ImouseX; " "; ImouseY;
If iActive And ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300 Then Print " AND works "; Else Print " AND rests";
If iActive And (ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300) Then Print " AND + () work "; Else Print " AND +() rest";
If iActive Eqv ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300 Then Print " EQV works "; Else Print " EQV rests";
If iActive Eqv (ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300) Then Print " EQV +() works " Else Print " EQV +() rests "
ImouseX = 600: ImouseY = 300 'All ImouseX and ImouseY are false
Locate , 7: Print " "; iActive; " "; ImouseX; " "; ImouseY;
If iActive And ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300 Then Print " AND works "; Else Print " AND rests ";
If iActive And (ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300) Then Print " AND + () work "; Else Print " AND +() rest ";
If iActive Eqv ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300 Then Print " EQV works "; Else Print " EQV rests ";
If iActive Eqv (ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300) Then Print " EQV +() works " Else Print " EQV +() rests "
ImouseX = 700: ImouseY = 400 ' ImouseX>600 and ImouseY > 300 are true
Locate , 7: Print " "; iActive; " "; ImouseX; " "; ImouseY;
If iActive And ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300 Then Print " AND works "; Else Print " AND rests ";
If iActive And (ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300) Then Print " AND + () work "; Else Print " AND +() rest ";
If iActive Eqv ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300 Then Print " EQV works "; Else Print " EQV rests ";
If iActive Eqv (ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300) Then Print " EQV +() works " Else Print " EQV +() rests "
Next iActive
For iActive = -1 To 0 Step 1
' the first run has iActive true while the second turn has iActive false
ImouseX = 400: ImouseY = 100 ' ImouseX < 500 and ImouseY <200 are true
Locate , 7: Print " "; iActive; " "; ImouseX; " "; ImouseY;
Print iActive And ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300, ;
Print iActive And (ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300), ;
Print iActive Eqv ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300; " ";
Print iActive Eqv (ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300)
ImouseX = 600: ImouseY = 300 'All ImouseX and ImouseY are false
Locate , 7: Print " "; iActive; " "; ImouseX; " "; ImouseY;
Print iActive And ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300, ;
Print iActive And (ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300), ;
Print iActive Eqv ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300; " ";
Print iActive Eqv (ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300)
ImouseX = 700: ImouseY = 400 ' ImouseX>600 and ImouseY > 300 are true
Locate , 7: Print " "; iActive; " "; ImouseX; " "; ImouseY;
Print iActive And ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300, ;
Print iActive And (ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300), ;
Print iActive Eqv ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300; " ";
Print iActive Eqv (ImouseX < 500 Or ImouseX > 600 Or ImouseY < 200 Or ImouseY > 300)
Next iActive
so the expression must pay attention to the order of execution of Logical operators to return the wanted result. As for all expression we can use () to modify the order of calculation.