go to previous page   go to home page   go to next page
/* Compute the sum of the integers 0 through 9 */
int count=0, sum=0;
while ( 1 )
{
  sum = sum + count ;
  . . .
  count= count + 1;
}
printf("sum = %d\n", sum );

Answer:

You would like to say "no," the code is not correct, because count is never tested and the loop will never end.


Broken Code

But what if the complete fragment were this:

/* Compute the sum of the integers 0 through 9 */
int count=0, sum=0;

while ( 1 )
{
  sum = sum + count ;
  if ( count == 9 ) break;
  count= count + 1;
}
printf("sum = %d\n", sum );

It turns out, the code is correct. (At least it computes what it should, although in an ugly fashion.)


QUESTION 4:

Does there seem to be a problem when control jumps out of code blocks?