Q:

Can someone help me to understand this for-loop in Java?

I went through some computer science tests and don't understand the outcome of this for-loop.
    int a, b, counter;

    for (a = 1; a <= 10 ; a = a + 1)
    {
        for (b = 0; b < a; b = b + 1) 
        {
                    counter++;
        }
    }
    System.out.println("Counter: " + counter);

The first for-loop iterates 10 times but what about the second one? And why is the output 55?

A:

The first loop iterates 10 times. For each of first loop iteration second loop iterates: 1 time, 2 times, 3 times .... 10 times.
Explanation: in the second loop b iterates from 0 to less than a. When a is 1, it iterates 1 time. When a is 2 the second loop iterates 2 times, when a is 3 the second loop iterates 3 times and so on. 
So the counter value would be: 1 + 2 + 3 + 4 .... + 10 = (10 * (10 + 1))/2 = 55

