mercredi 4 mars 2015

In ifs inside for loops, prefer checking for true, or for false and continue?


I'm discussing this with a work colleague. Say we want to sum the numbers from 0 to 9 skipping 5. He prefers this:



int sum = 0;
for(int i = 0; i < 10; ++i)
{
if(i == 5)
{
continue;
}

sum += i;
}


I prefer this:



int sum = 0;
for(int i = 0; i < 10; ++i)
{
if(i != 5)
{
sum += i;
}
}


Any reason to prefer one over the other? Cyclomatic complexity is the same in both cases, right?





Aucun commentaire:

Enregistrer un commentaire