/* 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 );
You would like to say "no," the code is not correct, because
count
is never tested and the loop will never end.
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.)
Does there seem to be a problem when control jumps out of code blocks?