# AutoHotkey Scripting: Operators

Operators 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.

## 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 −

```2 + 3

P(x) = x4 + 7x3 - 5x + 9.
```

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 −

 Operator Description Example + Adds two operands A + B will give 30 – Subtracts second operand from the first A – B will give -10 * Multiplies both operands A * B will give 200 / Divides numerator by de-numerator B / A will give 2

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

```a := 10
b := 20

c := a + b
msgbox Value of c = %c%

c := a - b
msgbox Value of c = %c%

c := a * b
msgbox Value of c = %c%

c := b / a
msgbox Value of c = %c%
```

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

```Value of c = 30

Value of c = -10

Value of c = 200

Value of c = 2
```

## Relational Operators

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

```A := 20

B := 10
```

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 −

```(A > B)
```

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 −

 Operator Description Example = 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.

```a := 10
b := 20

; Check whether a is equal to 10
if( a = 10 )
{
; if a is equal to 10 then this body will be executed
MsgBox, % "a is equal to 10"
}

; Check whether b is equal to 10
if( b = 10 )
{
; if b is equal to 10 then this body will be executed
MsgBox, % "b is equal to 10"
}

; Check if a is less than b
if( a < b )
{
; if a is less than b then this body will be executed
MsgBox, % "a is less than b"
}

; Check whether a and b are not equal
if( a != b )
{
; if a is not equal to b then this body will be executed
MsgBox, % "a is not equal to b"
}
```

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

```a is equal to 10

a is less than b

a is not equal to b
```

## 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 –

 Operator Description Example && 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 −

```a := 1

b := 0

if ( a && b )
{
MsgBox, This will never show because condition is false
}

if ( a || b )
{
MsgBox, This will be shown because condition is true
}

if ( !(a && b) )
{
MsgBox, This will be shown because condition is true
}
```

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

```This will be shown because condition is true
This will be shown because condition is true```

I find this next bit especially useful:

## Assignment Operators

To store a string or number in a variable

There are two methods:

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

For example:

```MyNumber = 123
MyString = This is a literal string.
CopyOfVar = %Var%  ; With the = operator, percent signs are required to retrieve a variable's contents.
```

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:

```MyNumber := 123
MyString := "This is a literal string."
CopyOfVar := Var  ; Unlike its counterpart in the previous section, percent signs are not used with the := operator.
```

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?