AHK loop

AutoHotkey Scripting – Loops

AHK loopLet’s discuss the loop and how to use it in AHK

In this post I try to present looping,

one of AutoHotkey’s basic concepts in a non-programmers way

So I’ll discuss two of the loops available in AutoHotkey.

Once you are clear about these two loop commands,

you can find more in the AutoHotkey documentation /other resources

So remember to check out other loops like:

for-loop and file-reading loop that are also available in AHK to learn how they work.

But now let’s consider a situation where you want to show Hello, World! in a dialog on screen.

More than once, let’s  just say five times.

Here is a simple AHK script to do that −

Msgbox % "Hello, World!"

Msgbox % "Hello, World!"

Msgbox % "Hello, World!"

Msgbox % "Hello, World!"

Msgbox % "Hello, World!"

return

 

When the above program is executed, it produces a msgbox 5 times with this content −

Hello, World!

Hello, World!

Hello, World!

Hello, World!

Hello, World!

 

That was simple but:

Let’s considerer another situation

Now you want to write Hello, World! one thousand times.

We can certainly write MsgBox commands a thousand times

However there are many reasons why this would not be optimal.

All programming languages provide a concept called loop,

which helps in executing one or more commands a desired number of times.

All high-level programming languages even provide various forms of loops,

which can be used to execute one or more statements repeatedly.

 

Let’s write the above AHK script with the help of a loop and later, we will discuss how other loops work

Loop 5
{
    MsgBox, % "Hello, World!"
}

 

When the above program is executed, it produces the same result as seen before −

Hello, World!

Hello, World!

Hello, World!

Hello, World!

Hello, World!

 

The above script makes use of a simple loop, which is being used to execute a set of programming statements enclosed within {…}.

A loop statement allows us to execute a command or group of statements multiple times.

All loops in AutoHotkey also set the value of the build in variable “A_index” with the current loop number i.e. if a loop is currently running for the 3th time then A_index will hold the number 3 and on the fourth loop it will hold 4 etc…

 

The while Loop

 

The while loop available in AutoHotkey has the following syntax −

while ( condition )
{
;.... while loop the loop body ....
}

 

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

while loop

 

The following are important points to be noted about a while loop −

  • A while loop starts with a keyword while followed by a condition enclosed in ( ).
  • Following the while command and condition, you will have the body of the loop enclosed in curly braces {…}.
  • A while loop body can have one or more lines of code to be executed repeatedly.
  • If the body of a while loop has just one line, then its optional to use curly braces {…}.
  • A while loop keeps executing its body until a given condition holds true. As soon as the condition becomes false, the while loop breaks and continues executing from the immediate next statement after the while loop body.
  • Let’s write the above AHK script with the help of a while loop and then we will discuss how this loop works
  • A condition is usually a relational statement, which is evaluated to either true or false. A value equal to zero is treated as false and any non-zero value works like true.
while ( A_index <= 5 )
{
    MsgBox, % "Hello, World!"
}

 

When the above program is executed, it produces the same result as seen before −

Hello, World!

Hello, World!

Hello, World!

Hello, World!

Hello, World!

 

Here, the computer first checks whether the given condition is true, i.e., if variable “A_index” is less than or equal to 5 or not, if the condition is true, the loop body is entered to execute the given statements.

In our current example we only have a single command in the loop body −

  • The Msgbox command, which shows the string Hello World!

After executing all the lines/commands given in the loop body, the computer goes back to while ( A_index = 5) and the given condition, (A_index = 5), is checked again, and the loop is executed again if the condition holds true.

This process repeats as long as the given condition remains true which means variable “A_index” has a value less than 6.

 

The Loop…Until

 

A while loop checks a given condition before it executes any statements given in the body part.

AutoHotkey provides other ways to use loop, like Loop…Until that allows executing a loop body first, then checking a given condition. It has the following syntax −

This process repeats as long as the given condition remains true which means variable “A_index” has a value less than 6.

Loop
{
;....loop in body....until....
} until ( Expression )

 

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

Loop until

 

If you write the above example using Loop…Until, then it looks like this −

Loop
{
    MsgBox, % "Hello, World!"
} until ( A_index = 5 )

 

When the above script is executed, it produces the same 5 msgbox’s −

Hello, World!

Hello, World!

Hello, World!

Hello, World!

Hello, World!

 

 

The break statement

 

When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.

The syntax for a break statement in AHK is −

break

 

A break statement can be represented in the form of a flow diagram as shown below −

break statement

 

Here is a variant of the above script, but it will break out of the loop after showing Hello World!

Three times −

Loop
{
    MsgBox % "Hello, World!"
    If ( A_index = 3 )
    {
        break;
    }
} until ( A_index = 5 )

 

When the above script is executed, it produces the following MsgBox’s −

Hello, World!

Hello, World!

Hello, World!

 

 

The continue statement

 

The continue statement in AutoHotkey works somewhat like the break statement.

Instead of forcing termination, continue forces the next iteration of the loop to take place, skipping any code in between the continue and the closing brace without going through the loop.  (this can be thought of as “restarting” the loop and skipping anything that was after the continues location in the body)

The syntax for a continue statement in AHK is as follows −

continue

 

A continue statement can be represented in the form of a flow diagram as shown below −

continue statement

 

A variant of the script above with continue looks like:

This will skip showing a MsgBox when A_index has a value equal to 3 −

Loop
{
    If ( A_index = 3 )
    {
        continue
    }
    MsgBox, % "Hello, World! Number: " A_index
} until ( A_index = 5 )

 

If you execute the above AutoHotkey script, it shows the following result −

Hello, World! Number: 1

Hello, World! Number: 2

Hello, World! Number: 4

Hello, World! Number: 5

 

If you’d like to know more about the loop online, I’m happy to help

Let me know what things you’ve used the loop command for…

Leave a Reply