Question #29922

hi i am looking for help on this i think the answer is: 1, 3, 5. Any help would be great.

for(int i = 1; i < 10; i+=2)
{
cout<< i <<endl;
if( i % 5 == 0)
break;
}

Expert's answer

Yes, you are right, answer is &quot;1 3 5&quot;. Theinitial value of the i variable is 1. and each step it is incremented by 2, so it will
be (2n+1) on the nth step. So loop will just print odd numbers: 1, 3, 5, 7, 9...But there is condition &quot;if (i % 5 = 0)&quot; that holds when i = 0, 5,
10, 15.... So the first value at which the condition is met will be 5, and an
indication &quot;break&quot; will stop the loop.

LATEST TUTORIALS
APPROVED BY CLIENTS