For Loop Issue

whats wrong w/ this code?

for (int i = 0; i < 4; i++)
{
if (i % 3 == 0) continue;
sum += i;
}
Nothing. It'll do exactly what you asked it to do. In this case, it'll add 1 and 2 to sum.
There is no the definition of variable sum and it is not clear whether it was initialized.

I would rewrite the loop the following way

1
2
3
4
5
int sum = 0;
for (int i = 0; i < 4; i++)
{
   if ( i % 3 != 0 )  sum += i;
} 


But in any case it is not clear what is the connection between the magic constant 4 and the magic constant 3.
Last edited on
Topic archived. No new replies allowed.