AutoIt If Then Else and ElseIf Statements

Tutorial on how to work with If, Then, Else and ElseIf in Autoit.

AutoIt If Statements are a similar to that of other scripting languages, but there are differences in its syntax.

A Single If

A single Autoit If would just be typed like below:

If $Var = 0 Then
    MsgBox(4096,"", "The variable content was.")
EndIf

If you want to compare two strings, (I.e. text), you will have to use variables, as the syntax doesn't seem to support quotes.

$string = 'This is A test'
$string2 = 'This is A test'
If $string == $string2 Then
 MsgBox(4096,"","Matched!")
EndIf

Note that we used two equals signs in the above example, this is called a operator, and the "==" operator is only used for string comparisons that you want to be case sensitive, simply use "=" if you don't want case sensitivity.

If Else

The Else statement will run code given, if the first check returned false. An example is provided below.

$string = 'This is a test'
$string2 = 'This is not a test'
If $string == $string2 Then
  MsgBox(4096,"","Matched!")
Else
  MsgBox(4096,"", "The strings did not match!")
EndIf

If ElseIf

The AutoIt ElseIf gives the same result as a nested If statement, example below.

If $var > 0 Then
    MsgBox(4096,"", "Value is positive.")
ElseIf $var < 0 Then
    MsgBox(4096,"", "Value is negative.")
EndIf

Nested If Statements

It is also possible to include If Statements inside of other statements, an example of this is shown below.

$string = 'Some string 1'
$string2 = 'Some string 2'

$string3 = 'How to do If Statements'
$string4 = 'How to do If Statements'

If $string == $string2 Then
  MsgBox(4096,"","Matched!")
Else
  If $string == $string2 Then
    MsgBox(4096,"", "The nested If was true")
  EndIf
EndIf

Post comment

Links that you insert are not nofollowed, but will be removed by admins if they are considered spam.

[url=Absolute URL for page]TITLE[/url]

You should insert code boxes around code examples, which will be automatically syntax highlighted.

[code1 html|css|javascript|php|sql]Your Code Here[/code1]

You may want to read our Privacy Policy before submitting your comment.