I'm wondering which is more efficient.
Let's say I have this code:
while (i<10000){
if (cond1)
doSomething1();
else if (cond2)
doSomething2();
else
doSomething3();
}
Now cond1 and cond2 are "constant" conditions, which means that if it happens once for i
it will happen to all of them, and just one of them can be true.
Most of the time, the last doSomething3()
is executed.
Now what happens if I write something like this:
if (cond1){
while (i<10000)
doSomething1();
}
else if (cond2){
while (i<10000)
doSomething2();
}
else{
while (i<10000)
doSomething3();
}
Is it more efficient because I'm checking cond1 and cond2 just once?