# range

## Recommended Posts

If i want to make this:

if \$x1 <> \$x2 and \$y1 <> \$y2

within a limit like 10 or so, how do i write it? Is there a keyword? Something like:

if \$x1 <> range(\$x2, 10) and \$y1 <> range(\$y2, 10)

Thanks!

##### Share on other sites

Try:

\$x1 = 170
\$x2 = 180
\$y1 = 2000
\$y2 = 2010
\$range = 10

If \$x1 < \$x2 + \$range Or \$x1 > \$x2 - \$range And \$y1 < \$y2 + \$range Or \$y1 > \$y2 - \$range Then
MsgBox(0, '', 'Yes')
Else
MsgBox(0, '', 'No!')
EndIf

##### Share on other sites

Try this.

Local \$x1 = 0, \$y1 = 0
Local \$x2 = 9, \$y2 = 11
Local \$iRange = 10

If ((\$x1 > \$x2 - \$iRange) And (\$x1 < \$x2 + \$iRange)) And _
((\$y1 > \$y2 - \$iRange) And (\$y1 < \$y2 + \$iRange)) Then
MsgBox(0, "Test 1", "\$x1, \$y1 is within the range of \$x2, \$y2", 2)
Else
MsgBox(0, "Test 1", "Out of range.", 2)
EndIf

;== OR using  WithinRange() function =======================

If WithinRange(\$x1, \$x2, \$iRange) And WithinRange(\$y1, \$y2, \$iRange) Then
MsgBox(0, "Test 2", "\$x1, \$y1 is within the range of \$x2, \$y2", 2)
Else
MsgBox(0, "Test 2", "Out of range.", 2)
EndIf

Func  WithinRange(\$a, \$b, \$range)
If ((\$a > \$b - \$range) And (\$a < \$b + \$range)) Then
return True
Else
return False
EndIf
EndFunc

@JoHanatCent

If \$x1 < \$x2 + \$range Or \$x1 > \$x2 - \$range And \$y1 < \$y2 + \$range Or \$y1 > \$y2 - \$range Then

will always be true for \$range values > zero.

Edited by Malkey

##### Share on other sites

i could be mistaken, but doesn't this work?

if \$min <= \$var <= \$max then

endif

##### Share on other sites

CodyBarrett,

but doesn't this work?

No - it is just luck that it seems to work in this case.

Explanation:

- 1. Remember AutoIt evaluates expressions of the same precedence in left to right order.

- 2. Remember some operators (like >= and <=) return True/False.

- 3. True is interpreted as 1 and False as 0.

So let us look at the operations AutoIt is carrying out:

\$min = 3
\$var = 3
\$max = 3

If \$min <= \$var <= \$max Then
ConsoleWrite("Yes 1" & @CRLF)
Else
ConsoleWrite("No 1" & @CRLF)
EndIf

; Which is actually processed like this
If (\$min <= \$var) <= \$max Then
ConsoleWrite("Yes 2" & @CRLF)
Else
ConsoleWrite("No 2" & @CRLF)
EndIf
;   (3 <= 3) <= 3
;   True <= 3
;   1 <= 3
;   True

; But does not work if processed like this
If \$min <= (\$var <= \$max) Then
ConsoleWrite("Yes 3" & @CRLF)
Else
ConsoleWrite("No 3" & @CRLF)
EndIf
;   3 <= (3 <= 3)
;   3 <= True
;   3 <= 1
;   False

; And your original fails if you reverse the order of the comparisons!
If \$max >= \$var >= \$min Then
ConsoleWrite("Yes 4" & @CRLF)
Else
ConsoleWrite("No 4" & @CRLF)
EndIf
;   (3 >= 3) >= 3
;   True >= 3
;   1 >= 3
;   False

Remember the warning in the Help file:

"False / True: These keywords should not be used in expressions as AutoIt will not detect this 'misuse' and the results will be unpredictable."

I hope that is all clear.

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

##### Share on other sites

You are terrible complicating !!!

\$var1 = 5;
\$var2 = 10;

\$range = 7;

If Abs(\$var1 - \$var2) < \$range Then MsgBox(0,'','Its in a range!')

Use absolute value of the difference!

Edited by 4ggr35510n

##### Share on other sites

CodyBarrett,

No - it is just luck that it seems to work in this case.

Explanation:

- 1. Remember AutoIt evaluates expressions of the same precedence in left to right order.

- 2. Remember some operators (like >= and <=) return True/False.

- 3. True is interpreted as 1 and False as 0.

So let us look at the operations AutoIt is carrying out:

\$min = 3
\$var = 3
\$max = 3

If \$min <= \$var <= \$max Then
ConsoleWrite("Yes 1" & @CRLF)
Else
ConsoleWrite("No 1" & @CRLF)
EndIf

; Which is actually processed like this
If (\$min <= \$var) <= \$max Then
ConsoleWrite("Yes 2" & @CRLF)
Else
ConsoleWrite("No 2" & @CRLF)
EndIf
;   (3 <= 3) <= 3
;   True <= 3
;   1 <= 3
;   True

; But does not work if processed like this
If \$min <= (\$var <= \$max) Then
ConsoleWrite("Yes 3" & @CRLF)
Else
ConsoleWrite("No 3" & @CRLF)
EndIf
;   3 <= (3 <= 3)
;   3 <= True
;   3 <= 1
;   False

; And your original fails if you reverse the order of the comparisons!
If \$max >= \$var >= \$min Then
ConsoleWrite("Yes 4" & @CRLF)
Else
ConsoleWrite("No 4" & @CRLF)
EndIf
;   (3 >= 3) >= 3
;   True >= 3
;   1 >= 3
;   False

Remember the warning in the Help file:

"False / True: These keywords should not be used in expressions as AutoIt will not detect this 'misuse' and the results will be unpredictable."

I hope that is all clear.

M23

oh!

okay... i thought it would work because (by luck now you explained it) it has worked in several functions of mine. thanks for explaining why it wouldn't.

##### Share on other sites

CodyBarrett,

i thought it would work

Well, of course, it does work in some cases where the values are suitable - but it is best not to rely on it in more general coding for the reasons I explained above.

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

## Create an account

Register a new account