A question for fun to you guys

in #code8 years ago

Guess the answer of these questions without using compiler, with a reason :-

void main( )
{
int i;
for(i=0;i++;i<100)
printf(”%d”,i);
}
How many times does the loop executes
a) 10 b) 0 c)100 d)Infinite

void main( )
{
int i;
for(i=1;i++;i<100)
printf(”%d”,i);
}
How many times does the loop executes
a) 10 b) 0 c)100 d)Infinite

Sort:  

Problem 1, loop will run 100 times with i value running from 0 to 99.
Problem 2, loop will run 99 times with i value running from 1 to 99.
The point made by fanouropittas that value >0 are true has no bearing unless the third argument to the "for" where written as ", i )".

Wrong answer :)

I believe it's b for the first one because the check will happen when i = 0, and then the i will increment to 1, so the loop will return false from the first run.

For the second one its d because the i will always be => 1 and anything >0 in c is considered true.
Very good problem, I hope I got it right!

Wrong answer :)