AutoHotkey Scripting: Operators

OperatorsOperators in programming

are symbols that tells the

compiler or interpreter to

perform specific mathematical,

relational or logical operations

and produce final results.

 

This post will explain the concept of

operators and I will take you

tough the important arithmetic and

relational operators available in AutoHotkey.

 

Multiplication

Arithmetic Operators

 

Computer programs are widely used for mathematical calculations.

We can write a script which can do simple calculation like

Adding two numbers (2 + 3) and we can also write a script

Which can solve a complex equation like P(x) = x4 + 7x3 – 5x + 9.

If you have been a student

You are most likely aware that in the first

Expression 2 and 3 are operands and + is an operator.

Similar concepts exist in Computer Programming.

Take a look at the following two examples −

 

These two statements are called arithmetic expressions in a programming language and plus, minus used in these expressions are called arithmetic operators and the values used in these expressions like 2, 3 and x, etc., are called operands.

In their simplest form, such expressions produce numerical results.

Similarly, AHK provides various arithmetic operators.

The following table lists down a few of the important arithmetic operators available in AutoHotkey.

Assume variable A holds 10 and variable B holds 20, then −

OperatorDescriptionExample
+Adds two operandsA + B will give 30
Subtracts second operand from the firstA – B will give -10
*Multiplies both operandsA * B will give 200
/Divides numerator by de-numeratorB / A will give 2

 

Simple example of an AutoHotkey script to help understand the above mathematical operators −

 

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

Not equal

Relational Operators

 

Consider a situation where we create two variables and assign them some values −

 

Here, it is obvious that variable A is greater than B in values.

With the help of some symbols you can write so called relational expressions.

Using the AHK scripting language, it can be written like this −

 

Here, we used a symbol > and it is called a relational operator and in their simplest form, they produce Boolean results which means the result will be either true or false (1 or 0).

The following table lists down a few of the important relational operators available in AutoHotkey.

Assume variable A holds 10 and variable B holds 20 −

OperatorDescriptionExample
=Checks if the values of two operands are equal, if yes then condition becomes true.(A = B) is not true.
!=Checks if the values of two operands are not equal, if values are not equal then condition becomes true.(A != B) is true.
>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(A > B) is not true.
<Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(A < B) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(A >= B) is not true.
<=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.(A <= B) is true.

 

Here, I’ll show you an example of AHK Programming which makes use of an if conditional statement.

This statement will be discussed in more depth later in a separate post

But in short:

You use an if statement to check a condition and if the condition is true

the body of the if statement is executed

otherwise the body of the if statement is skipped.

 

When the above script is executed, it produces three msgboxes with this content −

 

 

bitwise-inclusive-OR

Logical Operators

 

Logical operators are very important in programming and they help us make decisions based on certain conditions.

Suppose we want to combine the result of two conditions

Then logical AND as well as OR logical operators help us in producing the final result.

 

The following table shows the logical operators supported by AutoHotkey.

Please assume variable A holds 1 and variable B holds 0 –

OperatorDescriptionExample
&&Called Logical AND operator. If both the operands are non-zero, then condition becomes true.(A && B) is false.
||Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true.(A || B) is true.
!Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.!(A && B) is true.

 

Try this example:

To better understand the logical operators available in the AHK language −

 

When you execute the above script, it produces this result −

 

I find this next bit especially useful:

 

expression assignment

Assignment Operators

To store a string or number in a variable

There are two methods:

Traditional and expression.

 

The traditional method uses the equal sign operator (=) to assign unquoted literal strings or variables enclosed in percent signs.

For example:

 

On the other hand:

The expression method uses the colon-equal operator (:=) to store numbers, quoted strings, and other types of expressions.

The following examples are functionally identical to the previous ones:

 

The latter method is to be preferred due to its greater clarity, and because it supports expression syntax.

Always using expressions is best practice because:

They are required to do a lot of things like math and it is also the syntax used in function parameters and objects.

What do you feel about the operators in AutoHotkey?

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