mercredi 27 décembre 2017

If-statement put in for instruction

Given the 2 cases:

1.

if (n % 2 == 0)
    for(i = 1; i <= 10; i++)
             ...
else 
    for(i = 1; i <= 9; i++)
             ...

2.

for(i = 1; i <= 9 + (n % 2 == 0); i++)
          ... 

Is there a difference in time between the 2 cases? (Case 2) Verifying every step if n is an even number (or just an additional condition) should run the program a bit slower, am I correct?

Aucun commentaire:

Enregistrer un commentaire