If someone tells you that learning ASP does not require any programming knowledge, then he is wrong; if I tell you that learning ASP requires mastering a programming language, then I am wrong. The characteristic of the ASP dynamic server page environment is that it is written through one or several scripting languages. The scripting language can be regarded as a simplified version of the programming language. It is easy to learn and master, which provides considerable convenience to the designers of dynamic websites. It can be said that 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:
<html>
<head>
<title>DoLoop.asp</title>
<bodybgcolor=#FFFFF></head>
<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 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.