Sunday, April 25, 2010

Is these two programs same?

for (i=0;i<5;i++)
{
if(i%3==0)
continue;
}

i=0;
while(i<5)
{
if(i%3==0)
continue;
i++;
}

Answer:

Yes
No





Concept:
if you simply used only for and while ,it is same.  But you did with continue.

continue will change the control to  loop checking step without running below statement.

so in this case
for(i=0;i<5;i++)
{
if(i%3==0)
continue;
}
will run perfectly because there is no other statements below continue statement
but
i=0;
while(i<5)
{
if(i%3==0)
continue;
i++;
}
this will result in infinite loop.?! do you know how?
because 0%3=0 and 'continue' statement will  jump the control to condition checking without incrementing i .  0<5 is always true.  i always be 0 in this program

0 comments:

Post a Comment