Question: I kind of don't get the nested loops, can someone explain to me why the following code is executed 45 times please? Shouldn't the i++ and j++ increment by 1? So first loop should be (0 \* 0), (1 \* 1), (2 \* 2) and so on.

```
for(int i = 0; i < 10; i++)
    for(int j = 0; j < i; j++)
        System.out.println(i * j);

```

Answer: 
outer loop runs `i` times according to value of `i` 

1st i value is `0` last `i` value is `9` because `i<10`.

so inner loop loops `9*10/2` which is `45`

loop time=`0+1+2+.....+8+9` = `9*10/2` = `45`

note . i used this equation to calculate sum of the loop times

```
   1 + 2 + 3 + ... + N = N(N+1)/2

```
That is not how nested loops work, its like this -

1. First we enter outer loop, then we execute inner loop till the inner loop exits (that is j's value becomes equal to i) , and only after that we again increment i.

So for example , if `i` is 5 , it would enter the the outer loop, and then `j` would start from `0` till `4` (as i is 5) and the values calculated would be `(5*0) , (5*1) , (5*2) , (5*3) , (5*4)` , and after exiting the inner loop, it would again increment `i` by `1` , `i` would then become `6` and then this would repeat (that is j would start from 0 till 5).