Is this code correct?
/* Compute the sum of the integers 0 through 9 */ int count=0, sum=0; while ( count < 10 ) { sum = sum + count ; ... count= count + 1; } printf("sum = %d\n", sum );
Ordinarily, you would say "yes".
However, you might worry about what is happening at "...
" .
You hope that
there is nothing in the code at "...
" that affects
sum
or count
.
Also, you expect the code
inside the braces {
and }
to execute from beginning to end.
But what if the fragment were this:
/* Compute the sum of the integers 0 through 9 */
int count=0, sum=0;
while ( count < 10 )
{
sum = sum + count ;
continue;
count= count + 1;
}
printf("sum = %d\n", sum );
Now things have changed.
What happens with the new version of the code fragment?