vendredi 20 novembre 2015

If Else vs && ||

Is there a difference between coding with if else or && and || operators.

For example in if-else style I can write this code

for( var i = 0; i < 1000000000; i ++ ) {
    if( i % 2 == 0 ) {
        f1();
    } else {
        f2();
    }
}

And in && and || style I can get same result with this code

(( i % 2 == 0 ) && (test1() || true)) || test2();

I tested them in JS, they are working approx on same time, but I didnt test them on C++. Maybe it depends compiler or language.

Is there a speed difference? Or any difference at all?

Thank you

Aucun commentaire:

Enregistrer un commentaire