"if variable <>" doesn't work

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
genaro
Posts: 45
Joined: 09 Feb 2016, 20:07

"if variable <>" doesn't work

06 Mar 2020, 01:11

For some mystical reason it doesn't skip stuff in brackets even though col = 0x2C6750, I check it with clipboard

Code: Select all

PixelGetColor, col,125,595 ,rgb
Clipboard := col
if col <> 0x2C6750 or col <> 0x244238 or col <> 0x255743
{Clipboard := col
Loop 300
{Sleep 10
PixelGetColor, col,125,595 ,rgb
}Until col = 0x244238 or col = 0x2C6750 or col = 0x255743
sleep 2300
}
sleep 200
so I have to use goto to make it work

Code: Select all

PixelGetColor, col,125,595 ,rgb
Clipboard := col
if col = 0x2C6750 goto label
if col <> 0x2C6750 or col <> 0x244238 or col <> 0x255743
{Clipboard := col
Loop 300
{Sleep 10
PixelGetColor, col,125,595 ,rgb
}Until col = 0x244238 or col = 0x2C6750 or col = 0x255743
sleep 2300
}
label:
sleep 200
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: "if variable <>" doesn't work

06 Mar 2020, 02:05

If (Expression)
{
Statements
}
...
Loop {
...
} Until Expression

Code: Select all

PixelGetColor, col,125,595 ,rgb
Clipboard := col
; if (col <> 0x2C6750) or (col <> 0x244238) or (col <> 0x255743)   ; if expression, but it is always true
if (col <> 0x2C6750) and (col <> 0x244238) and (col <> 0x255743)
{Clipboard := col
Loop 300
{Sleep 10
PixelGetColor, col,125,595 ,rgb
}Until (col = 0x244238) or (col = 0x2C6750) or (col = 0x255743)   ; until expression
sleep 2300
}
sleep 200
Hubert
genaro
Posts: 45
Joined: 09 Feb 2016, 20:07

Re: "if variable <>" doesn't work

06 Mar 2020, 04:25

hd0202 wrote:
06 Mar 2020, 02:05
If (Expression)
{
Statements
}
...
Loop {
...
} Until Expression
; if (col <> 0x2C6750) or (col <> 0x244238) or (col <> 0x255743) ; if expression, but it is always true
Thank you so much.
Why is it true when col = 0x2C6750? :shock:

Why does "=" work without brackets but not "<>"?
User avatar
boiler
Posts: 17384
Joined: 21 Dec 2014, 02:44

Re: "if variable <>" doesn't work

06 Mar 2020, 10:09

genaro wrote:
Why is it true when col = 0x2C6750? :shock:

Why does "=" work without brackets but not "<>"?
It works when you use if col = 0x2C6750 because it's using the legacy if command. To do the equivalent of <> with legacy commands, you would have to use IfNotEqual col, 0x2C6750. You cannot use other operators like <> and and using the legacy if command.

It's better to avoid all that and use if for expressions, which allows you to do if (col <> 0x2C6750) and if (col <> 0x2C6750) and (col <> 0x244238) and (col <> 0x255743). You typically indicate that you are using the version for expressions by using parentheses around the condition(s), although there are exceptions.

Yes, it can make things confusing. It would be better if the legacy if statement were no longer in the language, but it is retained for backward compatibility (allowing older scripts to still work).
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: "if variable <>" doesn't work

07 Mar 2020, 04:42

Why is it true when col = 0x2C6750? :shock:

Why does "=" work without brackets but not "<>"?
; if (col <> 0x2C6750) or (col <> 0x244238) or (col <> 0x255743) is always true

a variable "col" with a value of "0x2C6750" is false in the first condition and is true in the second and the third condition; as they are linked with "or" the complete "if" expression is true

in programming you learn very early these two important rules:
multiple conditions with the same attribute and compared as "equal" must be linked by "or"
multiple conditions with the same attribute and compared as "not equal" must be linked by "and"

Hubert

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, Descolada, fiaztv1, JKJadan, Pianist and 210 guests