{...} (block)
A pair of braces denotes a block. Blocks are typically used with functions, Else, Loop, While-loop, and IF-commands.
{ zero or more commands }
Remarks
A block is used to bind two or more commands together. It can also be used to change which IF an ELSE belongs to, as in this example where the block forces the ELSE to belong to the first IF rather than the second:
if var1 = 1 { if var2 = abc sleep, 1 } else return
Although blocks can be used anywhere, currently they are only meaningful when used with functions, Else, Loop, or an IF-type command such as IfEqual or IfWinExist.
If an IF, ELSE, Loop, While-loop, or For-loop has only a single command, that command need not be enclosed in a block. However, there may be cases where doing so enhances the readability or maintainability of the script.
A block may be empty (contain zero commands), which may be useful in cases where you want to comment out the contents of the block without removing the block itself.
One True Brace (OTB, K&R style): The OTB style may optionally be used in the following places: expression if-statements, the else keyword, while-loops, For-loops, normal loops, function definitions, Try, Catch, and Finally. This style puts the block's opening brace on the same line as the block's controlling statement rather than underneath on a line by itself. For example:
if (x < y) { ... } else { ... } While x < y { ... } For k,v in obj { ... } Loop %RepeatCount% { ... } MyFunction(x, y) { ... } Try { ... } Catch e { ... } Finally { .... }
Similarly, a command or other action may exist to the right of a brace (except the open-brace of the One True Brace style). For example:
if x = 1 { MsgBox This line appears to the right of an opening brace. It executes whenever the IF-statement is true. MsgBox This is the next line. } MsgBox This line appears to the right of a closing brace. It executes unconditionally.
Related
Functions, While-loop, Loop, Else, If, If(Expression)
Example
if x = 1 { MsgBox, test1 Sleep, 5 } else MsgBox, test2