In the previous two articles of this article, we have learned the variables, functions, procedures and conditional statements of the scripting language VBScript. This article will continue to introduce the loop statements of VBScript to you and summarize the application of the scripting language in ASP.
After learning the functions and conditional statements of the scripting language VBScript in the previous article, today we will continue to look at loop statements in VBScript.
The function of loop statements is to repeatedly execute program code. Loops can be divided into three categories: one type repeats the statement before the condition becomes false, one type repeats the statement before the condition becomes true, and the other type repeats the execution according to the specified number of times. Statement. The following loop statements can be used in VBScript:
Do...Loop: Loop when (or until) the condition is true.
While...Wend: Loop when the condition is true.
For...Next: Specify the number of loops and repeat the statement using a counter.
For Each...Next: For each item in the set or each element in the array, repeat a set of statements. Let's first look at Do...Loop, which can run statement blocks multiple times (various times). Repeat the statement block when the condition is true or before the condition becomes true. Please see the following example:
- <html>
- <head>
- <title>DoLoop.asp</title><bodybgcolor=#FFFFF></head><p></p>
- <p>Please fill in the sales settlement records for each month from this year to this month on this page. <P>
- <%
- counter=1
- thismonth=month(now())
- Dowhilecounter<thismonth+1
- response.write&counter&month:
- response.write______________________________________&<BR><br>
- Ifcounter>13then
- exitdo
- endif
- counter=counter+1
- Loop
- %>
- <hr></body></html>
This ASP program uses loop statements to create a sales settlement record table, clip the above code into the notepad and save it as DoLoop.asp, and browse it in the browser in HTTP. Depending on the current month, you will see To the results shown in the figure below.
- <imgsrc='http://arch.pconline.com.cn/ppedu/empolder/wz/asp/10111/pic/2001124_asp_11.gif'>
Let's analyze this program. Our purpose is to print a table based on the current month. First, we create a counter count and set its value to 1. Then we use the functions month() and now() to get the current In the month, a loop is finally established. When the value of count is less than the value of the current month, the month value and a horizontal line are displayed and the value of count is increased by 1. The loop statement is repeated until the above conditions are false. If count is greater than 13, exit do will be used to exit the loop immediately.
Do Loop statements can also use the following syntax:
Do
[statements][Exit Do]
[statements]Loop [{While | Until} condition]
While...Wend statements are provided for users who are familiar with their usage. However, since While...Wend lacks flexibility, it is recommended to use the Do...Loop statement. Let's take a look at the For Next statement. The For...Next statement is used to run a statement block as specified times, using a counter variable in a loop, whose value increases or decreases with each loop.
The following example repeats the procedure MyProc 50 times. The For statement specifies the counter variable x and its start and end values. The Next statement adds the counter variable by 1 each time.
Sub DoMyProc50Times()
Dim x
For x = 1 To 50
MyProc
Next
End Sub
The keyword Step is used to specify the value of the counter variable each increase or decrease. In the following example, the counter variable j is incremented by 2 each time. After the loop is over, the total value is the sum of 2, 4, 6, 8, and 10.
Sub TwosTotal()
Dim j, total
For j = 2 To 10 Step 2
total = total + j
Next
The sum of MsgBox is & total & .
End Sub
To decrement the counter variable, set Step to a negative value. At this time, the termination value of the counter variable must be less than the starting value. In the following example, the counter variable myNum is decremented by 2 each time. After the loop is finished, the total value is the sum of 16, 14, 12, 10, 8, 6, 4 and 2.
Sub NewTotal()
Dim myNum, total
For myNum = 16 To 2 Step -2
total = total + myNum
Next
The sum of MsgBox is & total & .
End Sub
The Exit For statement is used to exit the For...Next statement before the counter reaches its termination value. Because the loop is usually just to exit in some special cases (such as when an error occurs), you can use the Exit For statement in the True statement block of the If...Then...Else statement. If the condition is False, the loop will run as usual.
Finally, let's take a look at the For Each...Next statement, the For Each...Next loop is similar to the For...Next loop. For Each...Next Instead of running a statement as specified, it repeats a set of statements for each element in the array or for each item in the object collection. This is very useful when you don't know the number of elements in the collection. Its syntax is as follows:
For Each element In group
[statements]
[Exit For]
[statements]Next [element]
If there is at least one element in the group, it will enter the For Each block to execute. Once you enter the loop, all statements in the loop are first executed on the first element in the group. As long as there are other elements in the group, statements in the loop will be executed on each element. Exit the loop when there are no other elements in the group and then continue execution from the statement after the Next statement.
At this point, we have completed the learning of all the basic knowledge of the scripting language VBScript, but you cannot be proficient in using VBScript by reading these existing articles. You must improve your level through continuous practice. Of course, if you are familiar with C, you can also choose JavaScript as the scripting language for ASP applications. I wonder if you have found that debugging ASP programs is difficult, because there are no ready-made tools. Here I will briefly introduce Microsoft Script Debugger to you, which we can use to perform a certain amount of program debugging.
Microsoft Script Debugger (Script error detection tool) included in IIS4.0 provides error detection functions for scripting programs. You can use the Microsoft Script error-detection tool to perform error-detection on scripts written in VBScript, JScript, and Java applets, beans, and ActiveX components.
Some script programs are executed in the user-side browser, while some script programs (parts in < % … %>) are executed on the server side. Microsoft Script Debugger can detect script programs executed by the user and script programs executed by the server. The script program executed in the user-side browser is executed in the user-side browser, including the VBScript and Jscript parts in the standard HTML code. This HTML code including the script program is executed when the browser loads this HTML code or when the event is triggered by a button press. A script program executed by a user-side browser is mainly used for basic checking of HTML form input and other functions.
The script program executed on the server side is executed on the IIS server side, including in the .asp program. First execute on the IIS server, the execution result generates standard HTML code, and then transmits it to the user browser. The script program executed by the server is mainly used for linking multiple web pages, processing of HTML form input, and accessing database information on the server.
Microsoft Script Debugger provides the following debugging features:
1. Set the interruption point
2. Gradually track the script program.
3. Set bookmarks.
4. Check the call stack.
5. Review and change the value.
6. Execute script instructions.
Starting from the next article, we will start learning the built-in objects of ASP, don’t go away.