Break / Continue
The keywords break
and continue
are used for manual control of loops:
-
break
ends the enclosing loop. Execution is continued after the end of the loop.
-
continue
ends the current loop execution and continues with the next loop item from the beginning of the loop.
Example:
for(var i = 0; i < 10; i++)
{
Log("Zahl: %d", i);
if(i > 6) break;
if(i > 2) continue;
Log("Zahl: %d (2. Ausgabe)", i);
}
Log("Endwert: %d",i);
Output:
Zahl: 0
Zahl: 0 (2.Ausgabe)
Zahl: 1
Zahl: 1 (2.Ausgabe)
Zahl: 2
Zahl: 2 (2.Ausgabe)
Zahl: 3
Zahl: 4
Zahl: 5
Zahl: 6
Zahl: 7
Endwert: 7
This loops counts the variable i
from 0 to 10.
If the first three loop executions (i from 0 to 2) the value is displayed twice.
From value 3 on continue
is called after the first output. This will skip the current loop execution. The output is made only once.
If value 7 is reached, break
is called. break
will, as opposed to continue
, not only skip current loop execution but will break out of the whole loop. You can notice by seeing that the value of i
is 7 at the end, not 11.
Peter, Juli 2001