The program here works on the concept of
Nested Loops which means a loop within a loop.
So, here loop 1 is
For I = 1 to 4 within which is our loop 2
For j = 1 to i
The loops will be executed as follows:
- i will be assigned the value 1 as loop one begins thus, i = 1
- j also will be assigned the value 1, j = 1
- Then we enter the Response.write i
Next
Response.write "<br>"
Next
where value of i will be written. Thus the out put is:
1
- The execution goes back to check the inner loop, where the limit is j = 1 to i. Since i = 1, the inner loop ends and the first looping of the first loop ends too.
- Now, we execute the first loop again, as i = 2.
- Again the inner loop is executed, this time j = 1
- The value of i is printed as 2
- The execution goes back to inner loop again, to satisfy the condition j = 1 to 2, j takes the value 2, thus j = 2
- The inner loop is executed again, forming the output
1
22 - Same is followed of the values 3 and 4.
In nested loop, if the outer looping is say of 10 times and the inner looping is 5, the total times the outer loop is executed will be 10 times, and the total times the inner loop is executed will 5X10 = 50 times.