Recommended: How to implement asp component-free thumbnails There are many ASP components that generate thumbnails on the Internet. If your virtual space does not support registering new components, you may feel that your website is losing its color. Xinqing is not talented, and combined with online resources, she wrote a component-free thumbnail program for reference only. Let's take a look at the basics first. First of all, we know that the following code is displayed on the page: img src=pic.gif border=0 width
This article teaches you the ASP script loop statement:
The characteristic of 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 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.
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>< body bgcolor=#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())
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>< /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, you will see the results in the figure below.
Let's analyze this program. Our purpose is to print a table based on the current month. First, we set up 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 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]
Share: ASP saves remote files to local % Function SaveRemoteFile(LocalFileName,RemoteFileUrl) SaveRemoteFile=True dim Ads,Retrieval,GetRemoteData Set Retrieval = Server.CreateObject(Microsoft.XMLHTTP) With Retrieval .Open Get, RemoteFileUrl, False, , .Send If .Readystate4 then SaveRemote