Skip to main content

Control-flow idioms

  • while (condition). A for whose step assigns a variable to itself.
  • break. Set the loop variable past its limit, or clear a "keep going" flag that the condition checks.
  • continue. Wrap the rest of the body in an if.
  • n % d. n - (n / d) * d
  • cond ? a : b. if (cond) x = a; else x = b;
  • A reusable function. Put the code in a named snippet and run it with SnippetExecuteScript. It gets its own variables, so pass values through Storage.
// while: wait up to 5 seconds for a window to appear
w = WindowFindByTitle("Untitled - Notepad");
for (tries = 0; !w && tries < 50; tries = tries + 1) {
UtilityWait(100);
w = WindowFindByTitle("Untitled - Notepad");
}

// break: stop at the first match
found = -1;
n = StringSplit("alpha,beta,gamma,delta", ",");
for (i = 0; i < n; i = i + 1) {
if (StringStartsWith(StringGetSplitPartAt(i), "g")) {
found = i;
i = n; // ends the loop after this pass
}
}

// continue: skip empty parts
for (i = 0; i < n; i = i + 1) {
part = StringGetSplitPartAt(i);
if (part) {
UtilityPrint(part);
}
}