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

```
I feel that you have a problem in understanding the scope of variables. I am re-writing the code for better understanding:

```
for(int i = 0; i < 10; i++){
        for(int j = 0; j < i; j++){
            System.out.println(i * j);
        } //end of scope of variable j
}// end of scope of variable i

```

You are most probably mistaken that at the end of the second loop when `i` stores the value of 1 the variable `j` should store the value of 1. But that is not the case. The variable `j` is reinitialised to the value of 0. So when the `i` attains the value of 2 in the 3rd loop the inner loop is executed 2 times for value of `j` as 0 and 1. and so on and so forth