I am new to programming and computer science. HTML is all I know and I have been facing problems with vbscript. This program (my first in vbscript) was given by my teacher. But I really do not understand anything. I referred to my book but in vain. I am not even sure if this is the right SE to post the question. Please help.
-
1It's "traditional". You could use `banana` and `nuclear_missile` as your variables if you wanted to, but most decent programmers would recognize `i` and `j` as loop counters. `i, j, k`, `p, q, r`, `foo, bar, baz`, etc... – Marc B Feb 26 '15 at 19:35
-
1possible duplicate of [Why are variables "i" and "j" used for counters?](http://stackoverflow.com/questions/4137785/why-are-variables-i-and-j-used-for-counters) – LittleBobbyTables - Au Revoir Feb 26 '15 at 19:38
-
What is step 1? And why do I use * after declaring j variable? Why not after the I one? – Karan Singh Feb 26 '15 at 19:43
-
In imperative languages, single letter variable names are often used to represent counts/index values in loops. – Carcigenicate Feb 26 '15 at 19:45
-
That is not the question – Karan Singh Feb 26 '15 at 19:46
1 Answers
What you have there is a loop with another nested loop, both of which print some text to the screen (document.write("...")).
The outer loop
For i = 1 To 5 Step 1
...
Next
iterates from 1 to 5 in steps of 1 (which is redundant, since 1 is the default step size, so you could just omit the Step 1). If you printed the value of i inside the loop
For i = 1 To 5 Step 1
document.Write(i & "<br>")
Next
You'd get the following output:
1
2
3
4
5
In your code sample you just print <br>, though, so each cycle of the outer loop just prints a line break.
In addition to printing line breaks in the outer loop you also have a nested loop, which for each cycle of the outer loop iterates from 1 to the current value of i, again in steps of 1.
For j = 1 To i Step 1
...
Next
So in the first cycle of the outer loop (i=1) the inner loop iterates from 1 to 1, in the second cycle of the outer loop (i=2) it iterates from 1 to 2, and so on.
For i = 1 To 5 Step 1
document.Write(i & "<br>")
For j = 1 To i Step 1
document.Write("*")
Next
Next
Since the inner loop prints an asterisk with each cycle you get i asterisks per line before the inner loop ends, the outer loop then goes into the next cycle and prints a line break, thus ending the current output line.
A good (although somewhat tedious) way to get an understanding of how the loops work is to note the current value of each variable as well as the current output line in a table on a sheet of paper, e.g. like this:
code line | instruction | i | j | output line
----------+------------------------+-------+-------+------------
1 | For i = 1 To 5 Step 1 | 1 | Empty |
2 | document.Write("<br>") | 1 | Empty | <br>
3 | For j = 1 To i Step 1 | 1 | 1 |
4 | document.Write("*") | 1 | 1 | *
5 | Next | 1 | 1 | *
6 | Next | 1 | 1 | *
1 | For i = 1 To 5 Step 1 | 2 | 1 | *
2 | document.Write("<br>") | 2 | 1 | *<br>
3 | For j = 1 To i Step 1 | 2 | 1 |
4 | document.Write("*") | 2 | 1 | *
5 | Next | 2 | 1 | *
3 | For j = 1 To i Step 1 | 2 | 2 | *
4 | document.Write("*") | 2 | 2 | **
5 | Next | 2 | 2 | **
6 | Next | 2 | 2 | **
1 | For i = 1 To 5 Step 1 | 3 | 2 | **
2 | document.Write("<br>") | 3 | 2 | **<br>
3 | For j = 1 To i Step 1 | 3 | 1 |
4 | document.Write("*") | 3 | 1 | *
... | ... | ... | ... | ...
- 184,186
- 23
- 230
- 299
-
-
Well, it's certainly not as elegant as stepping through the code with a debugger, but that method did help me understand how code worked when I was at the OP's level. – Ansgar Wiechers Feb 27 '15 at 10:15
-
Thanks a tonne! :) exactly what I wanted to understand! But why is next used? And that too twice? – Karan Singh Feb 27 '15 at 14:44
-
@KaranSingh Because the syntax of [`For..Next`](https://msdn.microsoft.com/en-us/library/sa3hh43e%28v=vs.84%29) loops requires it. The `Next` keyword marks the end of the group of statements the loop is supposed to execute. – Ansgar Wiechers Feb 27 '15 at 20:48