Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the "count limit" expression of a for-loop be evaluated only once, or on each iteration?

If I invoke a method within a loop's conditional statement, will it be called with each loop iteration?

For example:

for( int i = 0; i <= expensiveComputation(); i++ ) {
    // Do something.
}

Will I be performing expensiveComputation() on each iteration? Or will the result of expensiveComputation() be stored and used in each iteration at the same time the loop variable is initialised?

Should I instead re-write it to that:

int max = expensiveComputation();
for ( int i = 0; i <= max; i++ ) {
    // Do something.
}
like image 711
BARNZ Avatar asked Sep 01 '11 18:09

BARNZ


People also ask

Does a for loop have a limit?

There is no such limit. You can loop as many number of times you want.

How do you use a for loop count?

Initialize a count variable and set it a number. Use a for loop to iterate over a sequence. On each iteration, reassign the count variable to its current value plus N.

Is a variable that Control and count the iteration of loop in a program?

A dash is a variable that control and count the iteration of a loop in a program.

What is meant by for loop?

A "For" Loop is used to repeat a specific block of code a known number of times. For example, if we want to check the grade of every student in the class, we loop from 1 to that number. When the number of times is not known before hand, we use a "While" loop.


3 Answers

It will be invoked on each iteration, unless the compiler/optimizer decides that it has no side effects and can possibly eliminate the call as an optimization.

I mean, the compiler can't just blindly store the value because a function in java, unlike a mathematical function, can have not only a return value, but also such side effects as printing something to some stream or changing some global state etc.

There is also another reason why the compiler can't omit the call each iteration. The fact that your function doesn't take any arguments does not mean that it will necessarily return the same value each time. It can, for example, input a number from a stream and return it, or it can randomly generate a number.

So the compiler needs to be extremely careful before it can safely eliminate the calls. So, if the function is expensive, you should definitely pre-store its value.

like image 97
Armen Tsirunyan Avatar answered Oct 22 '22 13:10

Armen Tsirunyan


The second option is better specially if the computation does not require to be calculated at each iteration


If you suppose your loop is n length and you computation is O(n).
With the first solution, the complexity is O(n2) while the other is O(2n)

like image 25
VirtualTroll Avatar answered Oct 22 '22 11:10

VirtualTroll


It may or may not be called on each iteration, depending on what bytecode the compiler feels like emitting. Most likely it will in fact be called each time.

If you want to ensure that it is not invoked on each iteration, then use the int max = ... version.

Why not simply test it yourself?

like image 34
jwd Avatar answered Oct 22 '22 11:10

jwd