break and continue
break and continue
The keywords break
and continue
are used for finer 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("Number: %d", i);
if(i > 6) break;
if(i > 2) continue;
Log("Number: %d (2. Ausgabe)", i);
}
Log("Final Number: %d",i);
Output:
Number: 0
Number: 0 (2.Ausgabe)
Number: 1
Number: 1 (2.Ausgabe)
Number: 2
Number: 2 (2.Ausgabe)
Number: 3
Number: 4
Number: 5
Number: 6
Number: 7
Final Number: 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.
Peter2001-07