- #1
Jay_
- 183
- 0
Hi, its been some time since I did C prog (its not my field), but I do recall somethings from what I did 4 years ago.
What would be the output of the below prog (I know its a simple one), but I think it would execute for i = 5,6,7,8,9,10 (6 times), the printf statement. Yet some of my friends say it would only execute once and others are saying it would not even execute once.
*****
main()
{
int i;
for (i = -1; i <=10; i++)
{
if (i <5)
continue;
else
break;
printf("Gets printed only once!");
}
}
****
My current logic goes like this: at i = -1 to 4, the condition i<5 is true. So it would just go back to the for loop to keep incrementing i (from i++). At i =5, the statement i<5 is false. So it would go to else. It would encounter break; and then get out of the if-else. Being still inside the for loop it would print "Gets printed only once!". After that it would make i = 6, break from if-else and do the same. Likewise for 7,8,9,10. So total 6 times.
I don't have a C compiler to verify, but I would like to know the truth.
Thanks.
Jay.
What would be the output of the below prog (I know its a simple one), but I think it would execute for i = 5,6,7,8,9,10 (6 times), the printf statement. Yet some of my friends say it would only execute once and others are saying it would not even execute once.
*****
main()
{
int i;
for (i = -1; i <=10; i++)
{
if (i <5)
continue;
else
break;
printf("Gets printed only once!");
}
}
****
My current logic goes like this: at i = -1 to 4, the condition i<5 is true. So it would just go back to the for loop to keep incrementing i (from i++). At i =5, the statement i<5 is false. So it would go to else. It would encounter break; and then get out of the if-else. Being still inside the for loop it would print "Gets printed only once!". After that it would make i = 6, break from if-else and do the same. Likewise for 7,8,9,10. So total 6 times.
I don't have a C compiler to verify, but I would like to know the truth.
Thanks.
Jay.