When learning Asp, if someone tells you that you don’t need programming knowledge, then you’re a big mistake. In fact, the characteristic of the ASP dynamic server page environment is that it is written through one or several scripting languages. The script provides us designers with considerable convenience. Let’s take a look at the introduction of ASP script loop statements together.
The proper use of scripting language is directly related to the advantages and disadvantages of ASP applications. After learning the functions and conditional statements of the scripting language VBScript in the previous article, today we will continue to take a 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 statement according to the specified number of times. 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.
ForEach...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:
The above is an introduction to the ASP script loop statement. If you have any questions, you can leave a message to communicate. Thank you for your support from the new technology channel right or wrong!
Please fill in the sales settlement records for each month from this year to this month on this page.
<%
counter=1
thismonth=month(now())
Dowhilecounterresponse.write""&counter&"month:"
response.write"__________________________________"&"
"
Ifcounter>13then
exitdo
endif
counter=counter+1
Loop
%>
This ASP program uses loop statements to create a sales settlement record table, clip the above code to the notepad and save it as DoLoop.asp, and browse it in the browser in HTTP, depending on the current month.
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 month, and finally establish a loop. 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 the count is greater than 13, exitdo will be used to exit the loop immediately.
DoLoop statements can also use the following syntax:
Do
[statements][ExitDo]
[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 ForNext statement. The For...Next statement is used to run a statement block as specified number of 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 starting and ending values. The Next statement adds the counter variable by 1 each time.
SubDoMyProc50Times()
Dimx
Forx=1To50
MyProc
Next
EndSub
The keyword Step is used to specify the value of the counter variable each time it increases or decreases. In the following example, the counter variable j is incremented by 2 each time. After the loop is finished, the total value is the sum of 2, 4, 6, 8 and 10. SubTwosTotal()
Dimj,total
Forj=2To10Step2
Total=total+j
Next
MsgBox "sum of "&total&"."
EndSub
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. SubNewTotal()
DimmyNum,total
FormyNum=16To2Step-2
total=total+myNum
Next
MsgBox "sum of "&total&"."
EndSub
The ExitFor statement is used to exit the For...Next statement before the counter reaches its termination value. Because usually only exit the loop in certain special cases (such as when an error occurs), you can use the ExitFor 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 ForEach...Next statement, the ForEach...Next loop is similar to the For...Next loop. ForEach...Next is not to run the statement as specified, but to repeat 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: ForEachelementIngroup
[statements]
[ExitFor]
[statements]Next[element]
If there is at least one element in the group, it will enter the ForEach 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 alone. 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 ScriptDebugger (Script error detection tool) included in IIS4.0 provides error detection functions for script programs. You can use Microsoft Script error detection tools to detect scripts written in VBScript, JScript, and Javaapplets, 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 ScriptDebugger 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 will be 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 ScriptDebugger provides the following debugging functions:
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.
That’s all for the introduction of ASP script loop statements. Do you understand after reading it? If you don’t understand, you can leave a message to the editor of Foxin. Foxin is very happy to answer your questions.