AutoIt (Conditional Statements)

From The Ultimate Programming Reference

You will often want to change the flow of your script based on a condition or series of conditions. Is one number bigger than another? Or, does a string contain a certain value?

Conditions are evaluated as true (non-zero) or false (zero). Conditions generally use comparison operators like ==, <>, >=.


The following conditional statements are available in AutoIt:

Both statements are similar and decide which code to execute depending on the condition given. Here is an example of an If statement that pops up a message box if a variable is greater than 10.

$var = 20

If $var > 10 Then
    MsgBox(0, "Example", "$var was greater than 10!")
Else
    MsgBox(0, "Example", "$var was less than 10")
EndIf


In the example above the expression $var > 10 evaluated to true because the variable was indeed greater than 10. This caused the If statement to execute the first MsgBox line and display "$var was greater than 10!".



A Select statement is very similar, but is generally used for situations where you want to test a large number of conditions as it is generally easier to read than than large If/ElseIf type block. e.g.

$var = 30

Select
     Case $var > 1 AND $var <= 10
         MsgBox(0, "Example", "$var was greater than 1")

     Case $var > 10 AND $var <= 20
         MsgBox(0, "Example", "$var was greater than 10")

     Case $var > 20 AND $var <= 30
         MsgBox(0, "Example", "$var was greater than 20")

     Case $var > 30 AND $var <= 40
         MsgBox(0, "Example", "$var was greater than 30")

     Case $var > 40
         MsgBox(0, "Example", "$var was greater than 40")
EndSelect 

Personal tools