Loop Termination
There are many possible reasons for wanting to jump out of a loop before its normal terminating condition has been reached. The three types of loops previously described all have the ability to be terminated prematurely. Loop termination is performed through the use of an exit statement. When an exit statement is encountered, its condition is tested and, if the condition is true, the simulator skips the remaining statements in the loop and all remaining loop iterations, and continues execution at the statement immediately following the end loop statement.
The following example demonstrates how loop termination can be used to halt a sequence of test vectors that are being executed when an error is detected:
for i in 0 to VectorCount loop
ApplyVector(InputVec(i), ResultVec);
exit when CheckOutput(OutputVec(i), ResultVec) = FatalError;
end loop;
The exit condition is optional; an exit statement without an exit condition will unconditionally terminate when the exit statement is encountered. The following example shows an unconditional exit termination specified in combination with an if-then statement to achieve the same results as in the previous example:
for i in 0 to VectorCount loop
ApplyVector(InputVec(i), ResultVec);
if CheckOutput(OutputVec(i), ResultVec) = FatalError then
exit;
end loop;
When multiple loops are nested, the exit statement will terminate only the innermost loop. If you need to terminate a loop that is not the innermost loop, you can make use of loop labels to specify which loop is being terminated. The following example shows how loop labels are specified in exit statements:
LOOP1: while (StatusFlag = STATUS_OK) loop
GenerateSequence(InputVec,OutputVec,VectorCount,Seed);
LOOP2: for i in 0 to VectorCount loop
ApplyVector(InputVec(i), ResultVec);
ErrStatus := CheckOutput(OutputVec(i), ResultVec) = TestError;
if ErrStatus = ERR_COMPARE then
ReportError();
exit LOOP2;
elsif ErrStatus = ERR_FATAL then
ReportFatal();
exit LOOP1;
end if;
end loop LOOP2;
end loop LOOP1;
See also