JScript 10.0 for Statement Executes a block of statements for as long as a specified condition is true.
for (initialization; test; increment)
...statement

Arguments
- initialization
Required. An expression. This expression is executed only once, before the loop is executed. - test
Required. A Boolean expression. If test is true, statement is executed. If test if false, the loop is terminated. - increment
Required. An expression. The increment expression is executed at the end of every pass through the loop. - statement
Optional. Statement to be executed if test is true. Can be a compound statement.

Remarks
You usually use a for loop when the loop is to be executed a known number of times. A for loop is useful for iterating over arrays and for performing sequential processing. The test of a conditional expression occurs before the execution of the loop. Therefore, a for statement executes zero or more times. On any line in a for loop statement block, you can use the break statement to exit the loop, or you can use the continue statement to transfer control to the next iteration of the loop.

Example
In the following example, the for statement executes the enclosed statements as follows: First, the initial value of the variable i is evaluated. Then, as long as the value of i is less than or equal to 9, the document.write statements are executed and i is reevaluated. When i is greater than 9, the condition becomes false and control is transferred outside the loop.
// i is set to 0 at the start and is incremented by 1 at the
// end of each iteration.
// The loop terminates when i is not less than or equal to
// 9 before a loop iteration.
var s = "";
for (var i = 0; i <= 9; i++)
{
s += i + " ";
}
print (s);
// Output: 0 1 2 3 4 5 6 7 8 9
All of the expressions of the for statement are optional. In the following example, the for statements implement an infinite loop, and a break statement is used to exit the loop.
var s = "";
var j = 0;
for (;;)
{
if (j >= 5)
{
break;
}
j++;
s += j + " ";
}
print (s);
// Output: 1 2 3 4 5

Requirements

See Also
|
JScript 10.0 for-Anweisung Führt einen Anweisungsblock aus, solange eine angegebene Bedingung true ist.
for (initialization; test; increment)
...statement

Argumente
- initialization
Erforderlich. Ein Ausdruck. Dieser Ausdruck wird nur einmal vor Ausführung der Schleife ausgeführt. - test
Erforderlich. Ein boolescher Ausdruck. Wenn test true ist, wird statement ausgeführt. Hat test den Wert false, wird die Schleife beendet. - increment
Erforderlich. Ein Ausdruck. Nach jedem Schleifendurchlauf wird der Inkrementausdruck ausgeführt. - statement
Optional. Die Anweisung, die auszuführen ist, wenn test true ist. Hierbei kann es sich um eine zusammengesetzte Anweisung handeln.

Hinweise
for-Schleifen werden in der Regel verwendet, wenn die Anzahl der Schleifeniterationen feststeht. Die for-Schleife eignet sich zum Durchlaufen von Arrays und für die sequenzielle Verarbeitung. Bedingte Ausdrücke werden vor Ausführung der Schleife getestet. Daher werden for-Anweisungen nicht, einmal oder mehrmals ausgeführt. Auf jeder Zeile im Anweisungsblock einer for-Schleife können Sie diese mithilfe der break-Anweisung beenden, Sie können aber auch die continue-Anweisung verwenden, um die Steuerung an die nächste Iteration der Schleife zu übertragen.

Beispiel
Im folgenden Beispiel werden mit der for-Anweisung die eingeschlossenen Anweisungen wie folgt ausgeführt: Zuerst wird der Anfangswert der Variablen i ausgewertet. Sofern der Wert von i kleiner oder gleich 9 ist, werden die document.write-Anweisungen ausgeführt, und i wird erneut ausgewertet. Wenn i größer als 9 ist, ergibt die Auswertung der Bedingung False, und die Steuerung wird von der Schleife abgegeben.
// i is set to 0 at the start and is incremented by 1 at the
// end of each iteration.
// The loop terminates when i is not less than or equal to
// 9 before a loop iteration.
var s = "";
for (var i = 0; i <= 9; i++)
{
s += i + " ";
}
print (s);
// Output: 0 1 2 3 4 5 6 7 8 9
Alle Ausdrücke der for-Anweisung sind optional. Im folgenden Beispiel implementieren die for-Anweisungen eine Endlosschleife, und die Schleife wird mit einer break-Anweisung beendet.
var s = "";
var j = 0;
for (;;)
{
if (j >= 5)
{
break;
}
j++;
s += j + " ";
}
print (s);
// Output: 1 2 3 4 5

Anforderungen

Siehe auch
|