In a loop, it can be divided into 3 parts:
for(initialization ; terminating condition ; increment){ }
In for-loop, initialization will only be performed once. But the termination condition will be checked at each iteration. Therefore, if you write a for loop like:
for (int i = 0; i<count(); i++){ }
Method
count () will run every iteration.
By writing it as shown below, count() will be called only once, and the reason for this, of course, will reduce the unnecessary call to count ().
for (int i = 0, z = count(); i < z; i++)
The above is also equivalent to writing it as (except that z now beyond the scope of the loop):
int z = count(); for (int i = 0; i < z; i++)
source share