c - I wonder whats happens when if(i%2) what does it checks to come to continue. It misses == but it prints out the sum as 20? Why? -
#import <stdio.h> int main(void) { int sum,i; sum = 0; for(i=0;i<10;i++) { if(i%2) continue; sum+=i; } printf("\n%d",sum); return 0; }
how if(i%2)
works in above code?
in computing, modulo operation finds remainder after division of 1 number (sometimes called modulus).% modulus operator.
so i%2 returns remainder left after divided 2.
therefore if odd i%2=1(true)
else 0(false).(even condition)
therefore if added sum else loop continues.
Comments
Post a Comment