Follow Me on Twitter

Monday, October 26, 2009

Solutions to C Puzzles 3

What's the expected output for the following program and why?
enum {false,true};

int main()
{
int i=1;
do
{
printf("%d\n",i);
i++;
if(i < 15)
continue;
}while(false);
return 0;
}

Solution:

1

It prints 1 and quits.
Reason is continue statement brings/takes the execution to the beginning of the loop.
do while is nothing but while loop apart from the first iteration in which the
condition is not checked. Hence when continue is executed, execution to while
statement which returns false and hence quits the while loop.

1 comment:

  1. Whatever be the situation do-while loop always checks the 'while(condition)' in each iteration.

    ReplyDelete