Jump to content
Sign in to follow this  
Roman

Do i have worked too much?: If / If Not

Recommended Posts

Hello

Look at this code:

$a = 1
If $a = 1 Then
    MsgBox(0, "", "$a is 1")
EndIf
If Not $a = 2 Then
    MsgBox(0, "", "$a is NOT 2")
EndIf

Why i don't get the second message-box "$a is NOT 2"?

Feel like an absolut beginner. :">

Where i am wrong?

Regards,

Roman.

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

It seems wrong to me that you'd have to parenthesise the $a = 2 part, I would assume it's later in the order of operations, but I guess not.

To explain, what's happening is that the equation Not $a = 2 is actually made up of 2 comparisons, and the Not comparison is evaluating first because it's going left to right.

So instead of this (which you probably expected):

Not $a = 2

= Not False

= True

We're getting this:

Not $a = 2

= False = 2

= False

When you put brackets (parentheses) around the $a = 2 part, we're forcing that to evaluate first, like my first example.

... Don't know if you needed that explanation, but if not then maybe someone else will learn from it.

Share this post


Link to post
Share on other sites

It seems wrong to me that you'd have to parenthesise the $a = 2 part, I would assume it's later in the order of operations, but I guess not.

To explain, what's happening is that the equation Not $a = 2 is actually made up of 2 comparisons, and the Not comparison is evaluating first because it's going left to right.

So instead of this (which you probably expected):

Not $a = 2

= Not False

= True

We're getting this:

Not $a = 2

= False = 2

= False

When you put brackets (parentheses) around the $a = 2 part, we're forcing that to evaluate first, like my first example.

... Don't know if you needed that explanation, but if not then maybe someone else will learn from it.

It's not just left to right. There is an order of operations and NOT is the highest (first) operation. From the help file under Operators:

When more than one operator is used in an expression the order in which things happen is controlled by operator precedence. The precedence used in AutoIt is given below. Where two operators have the same precedence the expression is evaluated left to right.

From highest precedence to lowest:

NOT

^

* /

+ -

&

< > <= >= = <> ==

AND OR

So if you evaluate If Not $a = 2, it will NEVER be True, because the (Not $a) will be performed first and then compared to 2.

Any time you see "If Not ..." it should be a red flag to you. The above expression should be "If $a <> 2 Then".

:)

P.S. Is was able to find one special case where Not $a = 2. If $a = Binary("0xFFFFFFFC"):

Global $avTest[3] = [2, 3, Binary("0xFFFFFFFC")]
For $n = 0 To 2
    $a = $avTest[$n]
    If Not $a = 2 Then
        ConsoleWrite("Debug: Not " & $a & " = 2" & @LF)
    Else
        ConsoleWrite("Debug: Not " & $a & " <> 2" & @LF)
    EndIf
Next

^_^

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...