Go and RTFM on "continue" and you will see why the programs behave differently.

Rajesh M. Kanojia

13Dec2012 00:11

Re: Simple c programming

continue keyword is used to send the control back to the loop without executing remaing code.
now comes to ur problem:-
when u enter any -ve number then only continue statement get execute.
in ur program increment of variable i is also play a important role how:-
in ur first program i value get incremented as contol come back to the loop that's why u get answer 13
but in second program the value of i will incremented only when if condition get false.

try one more thing enter all number -ve u find that every time u get a message on console
Enter the value:-
the reason is i value doesn't change.

smp

18Dec2012 16:13

Re: Simple c programming

The only problem here is 'continue' statement. In your code, calling the 'continue' statement will result the execution to go back to the for loop for the next iteration. In the first case, calling the continue statement returns to the 'for' loop in which the value of 'i' is incremented. In the latter case the value of i is incremented after the continue statement. So if the continue statement is called in the second program, then the pointer returns to for loop without incrementing the value of i which makes the programs differ.

In first case, even if continue statement is called, value of i increases.
In second case, if continue statement is called, value of i remains the same.

iranjit

5Jan2013 18:29

Re: Simple c programming

When continue statement is executed, i+=1; is not executed in the second program. That's why you are getting different result.