In this article two, we learned the scripting language VBScript variables, functions, procedures and conditional statements, this article will continue to introduce Vbscipt loop statements, and the scripting language in the ASP application to summarize.
If someone tells you that learning ASP does not require any programming knowledge, then he is wrong; If I told you that learning ASP had to master a programming language, then I was wrong. ASP Dynamic Server Page environment is characterized in that it is written through one or several scripting languages, scripting language can be regarded as a simplified version of the programming language, it is easy to learn and grasp, which gives the vast number of dynamic Web site designers to provide considerable convenience. It can be said that the proper use of scripting language is directly related to the advantages and disadvantages of ASP applications. After we learned the function and conditional statements of scripting language VBScript, today we continue to look at the looping statements in VBScript.
The function of the loop statement is to repeat the program code, the loop can be divided into three categories: a class to repeat the statement before the condition becomes "false", a class of repeated execution of the statement before the condition becomes "true", and another class to repeat the statement in the specified number of times. The following looping statements can be used in VBScript:
Do ... Loop: loops when (or until) the condition is true.
While ... Wend: Loops When the condition is true.
For ... Next: Specify the number of cycles and use the counter to run the statement repeatedly.
For Each ... Next: For each item in the collection or for each element in the array, a set of statements is executed repeatedly.
Let's take a look at do ... Loop, which is a block of statements that can run multiple times (in a variable number of times). Executes the statement block repeatedly before the condition is true or the condition becomes true. Take a look at the following example: < html>< head>
< title>doloop.asp</title>< body bgcolor= "#FFFFFF" >
< p> please fill in the sales settlement records for each month until this month, on this page. < p>
<%
Counter = 1
Thismonth = month (now ())
Do While counter < Thismonth + 1
Response.Write "" & Counter & "Month:"
Response.Write "______________________________" & "< br>< br>"
If counter >13 Then
Exit Do
End If
Counter = counter+1
Loop
%>
< hr></body>
This ASP program uses the loop statement to make a sales settlement record table, paste the above code into the notebook to save as doloop.asp, and in the browser in the HTTP mode of browsing, depending on the current month, you will see the results.
Let's analyze this procedure, our goal 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 function month () and now () to get the current month, and finally build the loop, when the count is less than the value of the current month plus 1, that is, display the month value and a horizontal line and add the value of Count to 1, the Loop statement repeats until the above conditions are false to exit the loop. If Count is greater than 13, exit the loop immediately with Exit do.
The Do Loop statement 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. But because while ... Wend lacks flexibility, so it is advisable to use do ... Loop statement. Let's take a look at the for Next statement. For ... The Next statement is used to run a statement block for a specified number of times, using a counter variable in the loop, and the value of the variable increases or decreases with each loop.
The following example repeats a procedure MyProc 50 times. The For statement specifies the counter variable x and its starting and ending values. The Next statement adds 1 to the counter variable at a time. Sub Domyproc50times ()
Dim x
For x = 1 to 50
MyProc
Next
End Sub
The keyword Step is used to specify a value for each increment or decrease of the counter variable. In the following example, the counter variable j is incremented by 2 each time. At the end of the loop, the total value is the sum of 2, 4, 6, 8, and 10.
Sub Twostotal ()
Dim J, Total
For j = 2 to ten Step 2
To decrement the counter variable, you can set Step to a negative value. The end value of the counter variable must be less than the starting value at this time. In the following example, the counter variable MyNum minus 2 at a time. At the end of the loop, the total value is the sum of 16, 14, 12, 10, 8, 6, 4, and 2. Sub Newtotal ()
Dim MyNum, Total
For myNum = 2 Step-2
Total = Total + MyNum
Next
MsgBox "Sum is" & Total & ". "
End Sub
The exit For statement is used to exit the for ... before the counter reaches its terminating value. Next statement. Because it is usually only in certain special cases (such as when an error occurs) to exit the loop, you can do so in the If ... Then ... The Exit for statement is used in the TRUE statement block of the Else statement. If the condition is False, the loop will run as usual.
Finally, let's look at the for each ... Next statement, for Each ... Next Loop with for ... Next loop is similar. For Each ... Next is not to run the statement for a specified number of times, but to repeat a set of statements for each element in the array or for each item in the collection of objects. This is useful when you do not 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 goes into the for each block execution. Once in the loop, all the statements in the loop are executed first for the first element in the group. As long as there are other elements in the group, the statements in the loop are executed on each element. Exits the loop when there are no more elements in the group, and then resumes execution from the statement following the Next statement.
At this point, we have completed all the basic knowledge of scripting language VBScript, but only by reading the existing articles you are unable to skillfully use VBScript, you have to improve their level through continuous practice. Of course, if you're familiar with C, you can also choose JavaScript as the scripting language for your ASP application. I do not know if you have found that the ASP program debugging is difficult, because there is no ready-made tools, here I give you a brief introduction of Microsoft Script Debugger, we can use it for a certain amount of program debugging work.
IIS4.0 includes the Microsoft script Debugger (Script Debugger), which provides debugging capabilities for the Scripting program. You can use the Microsoft Script Debugger to work with scripts written in VBScript, JScript, and debugging Java applets, beans, and ActiveX components.
Some scripts are executed on the client side of the browser, and some scripts (<%.%>) are executed on the server side. Microsoft script Debugger, which can debug the script executed by the client and the server-side execution of the script program. The script executed in the client browser is executed in the client's browser, including VBScript, the Jscript section in standard HTML code. This includes the script's HTML code when the browser loads this HTML code or, for example, presses a button to trigger an event. A script that is executed by the client browser, primarily for functions such as basic checking of HTML form input.
Scripts executed on the server side are executed on the IIS server side, including in the. asp program. Executes on the IIS server first, and the execution results produce standard HTML code, which is then transferred to the client browser. Server-Side execution of the script, mainly for the link between multiple pages, HTML form input processing, and access to the database on the server data, and so on.
Microsoft Script Debugger provides the following debugging features:
1, set the break point
2. Follow up the script procedure gradually.
3, set bookmarks.
4, view the call stack.
5. View and change values.
6. Execute script instructions.
From the next section, we'll start learning about ASP's built-in objects, so stay tuned.
Total = Total + j
Next
MsgBox "Sum is" & Total & ". "
End Sub
ASP tutorial: ASP Script Looping statements