HTML Applications Tutorial #4This particular tutorial may need a little more concentration as we are to embark on the tortuous subject of loops. It took me many months to get my head around loops and mulitiple loops still give me a headache. Take a look at this.
FOR Loops
Option Explicit
Dim i
for i = 1 to 5
MsgBox ("Variable i is now set to " & i)
next
Remember to add this to your template file (see previous tutorials) before attempting to run it. The variable i is an index which is added to each time the loop goes round. Here's how it works. i is set to be the value 1 and a msgbox is displayed showing that. The next tells the computer to go back to the for and add 1 to the value (the STEP command has been omitted for clarity) then go through the loop again except that it displays the number 2 in the message box and so on until the value of 5 is reached and the loop ends which in this case means the end of the program.
The loop can be any set to run using any valid numbers although the bigger the second value, the longer the loop will run which may temprarily hang the browser. It could even be set to run from 5 to 1 if required and can skip values using the form
for i = 1 to 6 STEP 2
so that in this case only every other value is used. Take a while to digest this if you are new to programming. Those of you familiar with other dialects of BASIC may be wondering why the next is not followed by the index as in
next i
This is surely much better programming practice and so it is. Unfortunately, VBScript does not recognise it and will generate an error. There is a great deal that is not present in VBScript and we will encounter some more in due course. We will of course continue to use good programming practice whenever possible. When you are sure you know what's happening with for loops then move on the the DO loop.
DO Loops
Option Explicit
Dim i
i = 1
DO
i = i + 1
MsgBox ("Variable i is now set to " & i)
LOOP until i = 5
The DO LOOP differs in that some of the work involved must be done by the programmer, particularly when it comes to incrementing the index variable. Generally, this kind of loop would not be used for this kind of application as a FOR loop would obviously be better but I've used it so that you can compare the two pieces of code which give precisely the same results. So the variable i is set to 1 (VBScript would normally set it to zero but it is good practice to give it a value even if it is going to be zero). The loop starts and we add 1 to the index value (increment it). This means that it is actually now set to 2 thus the msgbox displays the value as being 2. Where you put the incrementing code (i = i + 1) is important. Remember, the computer will not do it for you as in the above example. The message box is displayed then the value of i is tetsed to see if it has reached 5. The loop will continue until this condition is met. Note that LOOP is a keyword and cannot be used as a variable name which some BASIC's use to simulate this kind of structure. The computer goes back to the DO and goes through the code again, adding 1 (could be more if required), displays the value of i and tests in case it has reached 5. Eventually it will and the loop will end. Be aware that if a mistake is made and the loop never ends then the program will lock up the PC and you will need to shut it down from Task Manager. Don't be afraid to experiment and make mistakes - you'll learn much more from your mistakes than from your successes.
I think that's probably enough brain bending code for this session. We'll continue next time with more loops.