int i;
for(i=10;i<=20;i=i+2)
if(i%10 <= 5)
printf("hello\n");
Why is "hello" printed four times? I expected it to be three times. Is it because of precedence?
int i;
for(i=10;i<=20;i=i+2)
if(i%10 <= 5)
printf("hello\n");
Why is "hello" printed four times? I expected it to be three times. Is it because of precedence?
Pay attention to the loop exit condition, which is <=
rather than the more common <
. The printout is triggered for i
values of 10
, 12
, 14
, and also 20
.