Loop (normal)

Performs one or more statements repeatedly: either the specified number of times or until Break is encountered.

Loop Count

パラメータ

Count

型:整数

If omitted, the loop continues indefinitely until a Break or Return is encountered. Otherwise, specify how many times (iterations) to perform the loop. ただし、明示的な空白値や1未満の数値は、ループが完全にスキップされることになります。

Countは、ループが始まる直前に一度だけ評価されます。例えば、Countが関数呼び出しや代入などの副作用を持つ式の場合、副作用は一度だけ発生します。

Countが括弧で囲まれている場合、スペースやタブは必要ありません。事例:Loop(2)

備考

ループ文の後には、通常、ループの本体を形成する文の集合体であるブロックが続きます。ただし、1つの文しかないループにはブロックは必要ありません("if"とその "else"は、この目的のために1つの文としてカウントされます)。

A common use of this statement is an infinite loop that uses the Break statement somewhere in the loop's body to determine when to stop the loop.

The use of Break and Continue inside a loop are encouraged as alternatives to Goto, since they generally make a script more understandable and maintainable. One can also create a "While" or "Do...While/Until" loop by making the first or last statement of the loop's body an IF statement that conditionally issues the Break statement, but the use of While or Loop...Until is usually preferred.

組込変数A_Indexには、現在のループの繰り返し回数が格納されます。ループ本体が最初に実行されるときに1を含む。2回目には2が入り、以下同様です。内側ループが外側ループで囲まれている場合、内側ループが優先されます。A_Index works inside all types of loops, including file loops and registry loops; but A_Index contains 0 outside of a loop.

A_Indexには、スクリプトによって任意の整数値を割り当てることができる。Countが指定されている場合、A_Indexを変更すると、実行される反復処理の回数に影響します。例えば、A_Index := 3とすると、ループ文は3回目の反復にあるかのように動作し(次の反復ではA_Indexは4になります)、A_Index--は現在の反復が合計にカウントされないようにします。

ループの後には、Countがゼロの場合に実行されるElse文がオプションで付いていることがある。

OTB(One True Brace)スタイルはオプションで使用することができます。事例:

Loop {
    ...
}
Loop RepeatCount {
    ...
}

Specialized loops:ループを使用して、ファイル、フォルダー、レジストリ項目を(1つずつ)自動的に取得することができます。詳しくはfile loopと registry loopを参照のこと。また、ファイル読み込みループは、ファイルの内容全体を1行ずつ操作することができます。最後に、解析ループは、区切られた文字列の中に含まれる個々のフィールドを操作することができます。

Until, While-loop, For-loop, Files-and-folders loop, Registry loop, File-reading loop, Parsing loop, Break, Continue, Blocks, Else

3回繰り返されるループを作成します。

Loop 3
{
    MsgBox "反復番号は " A_Index  ; A_Index は 1, 2, そして 3の順になります。
    Sleep 100
}

無限ループを作成しますが、25回目の反復で終了します。

Loop
{
    if (A_Index > 25)
        break  ;ループを終了させます
    if (A_Index < 20)
        continue  ; 以下をスキップして、新しい反復を開始します。
    MsgBox "A_Index = " A_Index  ; 20から25までの数字だけを表示します。
}