In this code example, a nested loop is used to check how often the loops are run through.
class NestedLoop {
public static void main(String[] args) {
int a = 5, b = 7, c1 = 0, c2 = 0;
for (int i = 20; i > a; i--) {
c1++;
for (int j = 0; j < b; j++) {
c2++;
if (i == j) {
System.out.println("How many times the loops are run?");
System.out.print("first loop: " + c1 + "\nsecond loop: "
+ c2 + " (" + c1 + " * " + b + ")");
}
}
}
}
}
How many times the loops are run?
first loop: 15
second loop: 105 (15 * 7)