TSolo315 MturkCrowd
Six years of turking experience

Loops

Repeat commands

1

What is a Loop?


At this point you have learned enough to be able to tackle most problems you will run across when trying to create a script to make HITs more efficient. This tutorial and future tutorials will cover topics that either make things easier or provide extra versatility. Loops can make your life easier by allowing you to repeat a group of commands a specified number of times. Loops follow the format Loop count {code}. Here is an example:


f1::
Loop 4 {
  sendinput this will be sent four times.
}
return

The count provided can be a variable, allowing you to loop a dynamic number of times.


^1::
Loop %numberOfEntries% {
  sendinput this will be sent the number of times the variable numberOfEntries is equal to.
}
return

Every time you go through the code of a loop statement you "iterate" over the loop code -- so going through the loop's code is know as an "iteration." In the first example above with a count of 4, there would be four iterations (you loop through the code four times.)

2

Break and Continue


There are two special commands that can be used within loops.

  • When AHK encounters the break command, it will immediately exit the loop -- regardless of how many iterations it had gone through so far.
  • When AHK encounters the continue command, it will skip the rest of the code in the current loop iteration and jump back to the top of the loop statement, starting the next iteration.

Here is an example using break and continue:


^1::
count := 0
Loop 30 {
  count += 1
  if (count < 10) 
  {
    continue ; skip the rest of the loop's code and jump to the next iteration.
  }
  if (count >= 10 and count < 25)
  {
    sendinput %count% ; send count variable
    sendinput `r ; line break
    continue ; skip the rest of the loop's code and jump to the next iteration.
  }
  if (count >= 25)
  {
    sendinput count has reached 25
    sendinput `r ; line break
    break ; break out of the loop completely.
  }
}
sendinput Loop Terminated.
return

3

A_Index


A_Index is a special variable that keeps track of what iteration you are on in the current loop. It starts at one and counts up every time you go through the code in a loop statement. Here is the same example using A_Index:


    ^1::
    Loop 30 {
      if (A_index < 10) 
      {
        continue ; skip the rest of the loop's code and jump to the next iteration.
      }
      if (A_index >= 10 and A_index < 25)
      {
        sendinput %A_index% ; send the number of the current iteration.
        sendinput `r ; line break.
        continue ; skip the rest of the loop's code and jump to the next iteration.
      }
      if (A_index >= 25)
      {
        sendinput count has reached 25
        sendinput `r ; line break.
        break ; break out of the loop completely.
      }
    }
    sendinput Loop Terminated.
    return

4

While Loop


A while loop follows the format While (expression) {code}. Code in a while loop will continue to run until the provided expression evaluates to false. You can make an infinite while loop by using While True and then by creating a condition to exit the loop with a break command. Examples:


    ^1::
    While (A_Index < 20) { ; Loop will stop when A_Index reaches 20.
      sendinput %A_Index% `r
    }
    return

    ^2::
    While True { ; Loop will run until a break command is reached.
      sendinput this loop has ran %A_Index% times `r
      if (A_Index > 22) 
      {
        sendinput Iteration Limit Reached
        break
      }
    }
    return

Next Tutorial: Coming Soon