AutoHotkey Scripting – If/Decisions/Logic

Green if else statementDecision 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):

 

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 −

decision making

Let’s write a AHK script with an if conditional statements to convert the above given situation into code −

 

When the above program is executed, it shows this −

 

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 −

The above syntax can be represented in the form of a flow diagram as shown below −

if else statement

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 −

 

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

 

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 −

 

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

 

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

 

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!

jszadmin

Drafter and Webmaster at JSZapp.com
I’m Jackie Sztuk or JSZ. I have a Drafter’s degree from Copenhagen Technical College (KTS) and my passion for windows scripting is what makes me want to help as meny as i can by writing great and easy to follow How-to's.

jszadmin

I’m Jackie Sztuk or JSZ. I have a Drafter’s degree from Copenhagen Technical College (KTS) and my passion for windows scripting is what makes me want to help as meny as i can by writing great and easy to follow How-to's.

Leave a Reply