# AutoIt Tutorial [If,ElseIf,else]

Discussion in 'Programming & Scripting' started by Zisly, Dec 11, 2008.

1. ### ZislyWell-Known Member

Joined:
Jun 9, 2008
Messages:
7,349
In this tutorial you will learn how to use: If,else,elseif.
Let's begin with If

Code:
\$value = 6
If \$value > 6 Then
MsgBox(0,"Tutorial","Value is bigger than 6")
EndIf

Ok, as you can see I assinged 6 to the variable \$value and then you can see that it says "If \$value > 6 then", which means (in a human language)
if the value is bigger than 6 then: *statement*
So if the value would bigger than 6 the statement after Then would be executed, but in this case 6 is not bigger than six, so the statement won't
be executed.
Now, if the statement is not on the same line as on the "If \$value > 6 Then" you will need an EndIf, to end the If check.
So if you would like the code to be a little shorter, you could write
Code:
If \$value > 5 Then MsgBox(0,"Tutorial","Value is bigger than 6")
But the code might not look so "clean" if you would do that, but thats your choice to make, how you want to code.

While talking about the code being "clean", in Scite there is a function which came with it called Tidy, this will Tidy your code
so it's easier to read, to use it either press ctrl+t or click on 'tools>Tidy AutoIt Source'.

Ok, so our code didn't execute any statement as the expression was not true.
So now we will make it execute.
Code:
\$value = 6   ;; assign 6 to the variable \$value
If \$value > 6 Then  ;; if the  \$value is bigger than 6 then execute *expression*
MsgBox(0, "Tutorial", "Value is bigger than 6")
ElseIf \$value = 6 Then ;; if the \$value is 6  then execute *expression*
MsgBox(0, "Tutorial", "Value matched!")
EndIf

So what this does is: if the first expressions is not true it will test the other one, which is: if the value would be the same as six it will execute the statement.
As this is true it will show the messagebox

You can see a list of other operators in the help file.

So now I will explain 'Else'
It kinda talks for itself, but might still need an explanation.
So what Else does is: if the first statements were not true the else expression will be executed.
Ex:
Code:
\$value = 6  ;; assign 6 to the variable \$value
If \$value > 6 Then   ;; if the  \$value is bigger than 6 then execute *expression*
MsgBox(0, "Tutorial", "Value is bigger than 6")
Else   ;; else, if the first statement was not true execute *expression*
MsgBox(0,"Tutorial","The first statement was not true")
EndIf

This was a rather short tutorial, so I will explain on how you can make a longer comment without needing the ';' for every line. (AzLaN proposed this?)
There are two ways you could do that, first one is by doing like this:
Code:
This is a comment
This is another comment
MsgBox(0,"Tutorial","aaa")

As the message box is in a comment it will of course not execute.

Here is the other way:
Code:
#cs
This is a comment
This is another comment
MsgBox(0,"Tutorial","aaa")
#ce

You might already have figured out was #cs and #ce stands for, if not: #cs = comment start , #ce = comment end.

Ok, now we are done with this tutorial.
Hope you did understand it, if not just ask

PS: Tutorial was written long time ago.

Add rep if you found this useful!

2. ### ZislyWell-Known Member

Joined:
Jun 9, 2008
Messages:
7,349
Bump my tutorial.

3. ### aasemNew Member

Joined:
Jun 11, 2009
Messages:
1
What about if there is more then 1 else?

4. ### ZislyWell-Known Member

Joined:
Jun 9, 2008
Messages:
7,349

Use elseif.
But show an example on what you mean.