Inside while/until and for loops, if a continue or break statement is encountered in the body, the rest of the body will cease to execute. If the statement is a continue statement, the current iteration will end and the next iteration will proceed. If it’s a break statement, the current iteration and any subsequent iterations will end — the loop will terminate altogether.
If a non-negative integer follows the keyword continue, as in continue 3;, and the loop is nested in other loops, then that number, minus 1, of innermost ancestor loops will be terminated, and then the surrounding loop will ‘continue’ (defined above). The statement continue; is a synonym of continue 0;.
Example of nested continuing:
while «expressionA» do {
while «expressionB» do {
while «expressionC» do {
while «expressionD» do {
continue 2;
% this loop will break
};
% this loop will break
};
% this iteration will stop but this loop will continue
};
% this loop will not be affected
};
If a non-negative integer follows the keyword break, as in break 3;, and the loop is nested in other loops, that number of innermost ancestor loops will also be terminated. The statement break; is a synonym of break 0;.
Example of nested breaking:
while «expression» do {
while «expression» do {
while «expression» do {
while «expression» do {
break 2;
% this loop will break
};
% this loop will break
};
% this loop will break
};
% this loop will not be affected
};
break
andcontinue
statements offer control over loop iterations.Syntax:
Semantics:
Decorate:
Inside
while
/until
andfor
loops, if acontinue
orbreak
statement is encountered in the body, the rest of the body will cease to execute. If the statement is acontinue
statement, the current iteration will end and the next iteration will proceed. If it’s abreak
statement, the current iteration and any subsequent iterations will end — the loop will terminate altogether.If a non-negative integer follows the keyword
continue
, as incontinue 3;
, and the loop is nested in other loops, then that number, minus 1, of innermost ancestor loops will be terminated, and then the surrounding loop will ‘continue’ (defined above). The statementcontinue;
is a synonym ofcontinue 0;
.Example of nested continuing:
If a non-negative integer follows the keyword
break
, as inbreak 3;
, and the loop is nested in other loops, that number of innermost ancestor loops will also be terminated. The statementbreak;
is a synonym ofbreak 0;
.Example of nested breaking:
Static Semantics: Semantic Error
It is a semantic error if
TokenWorth(INTEGER)
is negative.