/en/_posts/2011-11-12-Repeating.markdown

https://github.com/maul-esel/ahkbook · Markdown · 77 lines · 67 code · 10 blank · 0 comment · 0 complexity · 656215a8d6a6efccddb283387e0bc5c1 MD5 · raw file

  1. ---
  2. title: Repeating things
  3. layout: post
  4. permalink: /en/Repeating.html
  5. ---
  6. # Repeating things
  7. ## a simple loop
  8. Sometimes you want to do something "forever" or as long as the script runs. Or you want to do it several times, but you don't know exactly how long. For those cases, enclose it in a `Loop`-block:
  9. {% highlight ahk linenos %}; any AutoHotkey version
  10. Loop
  11. {
  12. MsgBox Loop iteration %A_Index%
  13. }
  14. {% endhighlight %}
  15. As you see above, the built-in variable `A_Index` contains the current iteration count. If you just want to repeat one line, you can leave out the braces.
  16. As the above may consume a lot of memory, you may want it to wait some time before the next iteration:
  17. {% highlight ahk linenos %}; any AutoHotkey version
  18. Loop
  19. {
  20. MsgBox Loop iteration %A_Index%
  21. sleep 1000 ; waits 1000 ms = 1 s before going on
  22. }
  23. {% endhighlight %}
  24. ### `SetTimer`
  25. For less memory consumption and to be able to do other things during the "wait-period", you can also use [SetTimer]() instead of a loop.
  26. ### repeating x times
  27. Sometimes you also want to repeat something but only a certain number of iterations. That's very easy:
  28. {% highlight ahk linenos %}; any AutoHotkey version
  29. Loop 10 ; does 10 iterations
  30. {
  31. MsgBox Loop iteration %A_Index%
  32. }
  33. {% endhighlight %}
  34. You can also use a variable here. There's only a slight difference in AutoHotkey v2 and AutoHotkey\_H v2:
  35. In all other versions, you must enclose the variable in percent signs. In AutoHotkey v2 and AutoHotkey\_H v2, leave the percent signs out (`Loop` accepts an expression parameter).
  36. ### `break` and `continue`
  37. During loop execution, you may sometimes discover something which should stop the loop or the current iteration.
  38. In all kinds of loops presented here (except for `SetTimer`, which isn't a real loop), you can use `break` to stop the loop, and `continue` to skip the rest of the current iteration:
  39. {% highlight ahk linenos %}; any AutoHotkey version
  40. Loop
  41. {
  42. if (breakNow)
  43. break
  44. if (skipIteration)
  45. continue
  46. ; do something here that may change "breakNow" or "skipIteration"
  47. }
  48. {% endhighlight %}
  49. ## `while` & `until`
  50. If there's only one condition that may cause a `break`, you way also use a `while`-loop or a loop-`until`.
  51. {% highlight ahk linenos %}; any AutoHotkey version
  52. While (!breakNow)
  53. {
  54. ; do something that may change "breakNow"
  55. }
  56. {% endhighlight %}
  57. {% highlight ahk linenos %}; AutoHotkey_L, AutoHotkey_H, AutoHotkey v2 and AutoHotkey_H v2
  58. Loop
  59. {
  60. ; do something that may change "breakNow"
  61. } until breakNow
  62. {% endhighlight %}
  63. The difference is that `while` checks the condition **before** the iteration, whereas `until` checks it **after** the iteration (so there's always at least one iteration).
  64. ## `for` (each)
  65. AutoHotkey has also a `for` loop (which corresponds to `foreach` in many other languages). We will cover this when dealing with objects later.
  66. ## Loop parse, reg, file, ...
  67. ...