initialized and jumps to AfterLoopif it is nonzero. This is your break
statement—simply an elegant name for the good old gotocommand that was
so popular in “lesser” programming languages.
For this you can easily deduce the original source to be somewhat similar to
the following:
do
{
if (array[c])
break;
array[c] = c;
c++;
} while (c < 1000);
Loop Skip-Cycle Statements
A loop skip-cycle statement is implemented in C and C++ using the con-
tinuekeyword. The statement skips the current iteration of the loop and
jumps straight to the loop’s conditional statement, which decides whether to
perform another iteration or just exit the loop. Depending on the specific type
of the loop, the counter (if one is used) is usually not incremented because the
code that increments it is skipped along with the rest of the loop’s body. This
is one place where forloops differ from whileloops. In forloops, the code
that increments the counter is considered part of the loop’s logical statement,
which is why continuedoesn’t skip the counter increment in such loops.
Let’s take a look at a compiler-generated assembly language snippet for a loop
that has a skip-cycle statement in it:
mov eax, DWORD PTR [c]
mov ecx, DWORD PTR [array]
LoopStart:
cmp DWORD PTR [ecx+eax*4], 0
jne NextCycle
mov DWORD PTR [ecx+eax*4], eax
add eax, 1
NextCycle:
cmp eax, 1000
jl SHORT LoopStart
This code sample is the same loop you’ve been looking at except that the
condition now invokes the continuecommand instead of the breakcom-
mand. Notice how the condition jumps to NextCycleand skips the incre-
menting of the counter. The program then checks the counter’s value and
jumps back to the beginning of the loop if the counter is lower than 1,000.
Deciphering Code Structures 507
21_574817 appa.qxd 3/16/05 8:54 PM Page 507