 # AutoHotkey Scripting – If/Decisions/Logic Decision making is critical to Scripting/programming also in Autohotkey.

There will be many situations when you will be given two or more options and

You will have to select one option based on the given conditions.

If for example:

You want to show a remark about John (a student) based on his scores.

Take this situation −

Assume given scores are x for a student (John):

```If given scores are more than 95, then

Student is brilliant

If given scores are less than 30, then

Student is sub-par

If given scores are less than 95 and more than 30, then

Student is average
```

Now, the question is how to write AutoHotkey code to handle such situations.

All programming languages provide conditional statements that work based on the idea in this flow diagram − Let’s write a AHK script with an if conditional statements to convert the above given situation into code −

```x := 45

if ( x > 95)
{
MsgBox % "Student is brilliant"
}

If ( x < 30)
{
MsgBox % "Student is sub-par"
}

If ( x < 95 && x > 30 )
{
MsgBox % "Student is average"
}
```

When the above program is executed, it shows this −

```Student is average
```

The above program uses if conditional statements.

The first if statement checks whether the given condition i.e. variable x is greater than

If the condition is true, then the conditional body is entered to execute the given statements.

Here, a single MsgBox command is used to show a remark about the student.

The next if statement also checks its condition.

Not showing anything this time.

Finally, the third if statement is executed, here we have the following two conditions −

• First condition is x > 95
• Second condition is x < 30

AHK evaluates both the given conditions and then, the overall result is combined with the help of the logical operator && (meaning “and”).

If the final result is true, then the conditional statement will be executed

Otherwise no statement will be executed.

This post will hopefully give you a basic idea on various forms of if statements available in AutoHotkey.

Different programming languages provide different types of decision-making statements

But the basic concept remains the same as explained.

## If…else statement

If statements can be followed by an optional else statement,

which executes when the Boolean expression is false.

The syntax of an if…else statement in AutoHotkey can look like this −

```If (boolean_expression)

{

; Statement(s) will execute if the Boolean expression is true

}

else

{

; Statement(s) will execute if the Boolean expression is false

}
```

The above syntax can be represented in the form of a flow diagram as shown below − If…else statements are useful when you need to make a decision between two options.

For example, if a student secures higher scores than 95,

Then the student is brilliant,

Otherwise they are not brilliant

Such situation can be coded, as follows −

```x := 45

if ( x > 95)
{
MsgBox % "Student is brilliant"
}
Else
{
MsgBox % "Student is not brilliant"
}
```

When the above program is executed, it shows the following −

```Student is not brilliant
```

## If… else if… else statement

An if statement can be followed by an optional else if… else statement, which is very useful to test various conditions.

While using if, else if, else statements, there are a few points to keep in mind −

• An if can have zero or one else’s.
• An if can have zero to many else… if’s and they must come before the last else.
• Once an else… if succeeds, none of the remaining else… if’s or else will be checked. This is an important point to understand as it has large impliations on efficiency of code execution

The syntax of an if… else if… else statement in AutoHotkey is −

```If (boolean_expression 1)
{
; Executes when the boolean expression 1 is true and exits logic evaluation
}
else if ( boolean_expression 2)
{
; Executes when the boolean expression 2 is true and exits logic evaluation
}
else if ( boolean_expression 3)
{
; Executes when the boolean expression 3 is true and exits logic evaluation
}
else
{
; Executes when none of the above conditions are true
}
```

Now with the help of if… else if… else statement, the first situation can be coded as follows −

```x := 45

if ( x >= 95)
{
MsgBox % "Student is brilliant"
}
else if ( x <= 30)
{
MsgBox % "Student is sub-par"
}
else if ( x < 95 && x > 30 )
{
MsgBox % "Student is average"
}
```

When the above program is executed, it produces the following result −

```Student is average
```

The if’s in the examples use the if (expression) syntax, AutoHotkey also has what is called a traditional if statement but I will not go in to that in this post as you will most likely never need to use it…

Let me know by using the comments if I’m missing something, I also wish to know if you’d like me to cover some of this in better detail!