U
U
UID_B Nintendo2018-10-01 06:38:28
Java
UID_B Nintendo, 2018-10-01 06:38:28

How to count the number of iterations in a loop?

I'm wondering how many iterations will be executed in the outer and inner loop.
for(int i=0; i < N-1; i++) {
for(int j=0; j < Ni-1; j++) {
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-10-01
@kostyamega8

Define an integer variable before the outer loop, make it ++ in the inner one:

int iteration_count = 0;

for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < N - i - 1; j++) {
        iteration_count++;
    }
}

System.out.println(iteration_count);

If you need the total number of iterations of both loops - then in the outer one, too, ++.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question