- 1
Q. on while loop
why is the answer 2. ?? please explain
2 Answers
0
can anyone dicuss the part related to %
- 1
That is because.
int x = 1;
while (x++ < 5) // if x is less than 5, do the loop. Now add 1 to x. So the first time would be while 1 < 5 do this. Now add one to 1.
// Second time. while 4 < 5 do this. Now add 1 to 4. if 5 % 2 == 0 do this. FALSE.
// Third time won't execute because 5 is not less than 5.
{
if(x % 2 == 0) // if 2 % 2 == 0 do this.
{
x +=2; // Add 2 to 2.
}
}