Determining Number of Iterations in a loop (Java)

Viewed 638

I was wondering if there was a single formula anybody knew of to calculate the number of iterations in a loop beforehand.

I played around with it and got to the equation:

the number of iterations is equal to ((the absolute value of the difference of the final value and initial value) plus (the absolute value of the increment/decrement)) divided by the absolute value of the increment/ decrement

or

expected = (int)((Math.abs(final-initial) + Math.abs(iteration)) / iteration),

for example:

for(int i = 43; i > 9; i-=8) , will run 5 times, because 43-10 (you add one to the initial if it is doesn't include) = 33+8 = 41/8 ~ 5

If it is equal to or greater than- for(int i = 43; I >= 9; i-=8), then you'd do (43-9+8) / 8 (5) according to my formula

I've tested it a few times but not sure yet.

Thanks.

2 Answers

Baring any special disruption of the loop (breaks, altering counter, etc), you can test the formula a follows. If the termination test is < and not <= you should subtract 1 from start.

total iterations = (end - start + incr)/incr; // for <=
total iterations = (end - start  + incr - 1)/incr; // for <

Random r = new Random();
for (int k = 0; k < 10000; k++) {
    int count = 0;
    int start = r.nextInt(20)+1;
    int end = r.nextInt(1000) + start+1;
    int incr = r.nextInt(50)+1;
    
    for (int i = start; i <= end; i += incr) {
        count++;
    }
    
    int calc = (end - start + incr - 1)/incr;
    if (calc != count) {
        System.out.println("Oops");
    }
}

Baring any special disruption of the loop (breaks, altering counter, etc), the formula is as follows:

iterations = (end - start+incr)/incr, where relational operator is >= or <=

if relational operator is < or >, then iterations = (end - start+incr-1)/incr

This can be tested as follows

/*
 * Baring any special disruption of the loop (breaks, altering counter, etc), the formula is as follows:

iterations = (|start-end|+|incr|)/|incr|, where relational operator is >= or <=

- if relational operator is < or >, then iterations = ((|start-end|+|incr|-1)/|incr|

 * @author rahul.arora, WJS
 */

import java.util.Random;

public class LoopIterationCalcTester {

    public static void main(String[] args) {
        
        
    for (int x = 0; x < 100; x++) {
        Random r = new Random();
        for (int k = 0; k < 10000; k++) {
            int count = 0;
            int start = r.nextInt(20)+1;
            int end = r.nextInt(1000) + 20;
            int incr = r.nextInt(50)+1;
            
            for (int i = start; i < end; i += incr) {
                count++;
            }
            
            int calc = (end - start-1+incr)/incr;
            
            if (calc != count) {
                System.out.println("Oops");
               
            }
        }
        
        
        for (int k = 0; k < 10000; k++) {
            int count = 0;
            int start = r.nextInt(20)+1;
            int end = r.nextInt(1000) + 20;
            int incr = r.nextInt(50)+1;
            
            for (int i = start; i <= end; i += incr) {
                count++;
            }
            
            int calc = (end - start+incr)/incr;
            
            if (calc != count) {
                System.out.println("Oops");
             
            }
        }
        for (int k = 0; k < 10000; k++) {
            int count = 0;
            int start = r.nextInt(20)+1;
            int end = r.nextInt(1000) + 20;
            int incr = r.nextInt(50)+1;
            
            for (int i = end; i >= start; i -= incr) {
                count++;
            }
            
            int calc = (end - start+incr)/incr;
            
            if (calc != count) {
                System.out.println("Oops");
            
            }
        }
        for (int k = 0; k < 10000; k++) {
            int count = 0;
            int start = r.nextInt(20)+1;
            int end = r.nextInt(1000) + 20;
            int incr = r.nextInt(50)+1;
            
            for (int i = end; i > start; i -= incr) {
                count++;
            }
            
            int calc = (end - start-1+incr)/incr;
            
            if (calc != count) {
                System.out.println("Oops");
            
            }
        }
        }
    }

}

Related