ASP Script Loop statement

Source: Internet
Author: User
Tags exit

This article teaches you the ASP script Loop statement:

ASP Dynamic Server page environment is characterized by the fact that it is written in one or more scripting languages, scripting language can be seen as a simplified version of the programming language, it is easy to learn and master, which to the vast number of dynamic Web site designers to provide a considerable convenience. It can be said that the use of scripting language is directly related to the advantages and disadvantages of ASP applications. After we learned about the functions and conditional statements in the scripting language VBScript, today we continue to look at the looping statements in VBScript.

The function of a circular statement is to repeat the execution of the program code, which can be divided into three categories: a class repeats the statement before the condition becomes "false", a class repeats the statement before the condition becomes "true," and another class repeats the statement at a 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 loops and repeat the statement using the counter.

For Each ... Next: Repeats a set of statements for each item in the collection or for each element in the array.

Let's take a look at do ... first. Loop, which is a block of statements that can be run multiple times (with indeterminate number of times). Repeats the statement block when the condition is true or the condition changes to true. Take a look at the following example: < html>< head>

< title>doloop.asp</title>< body bgcolor= "#FFFFFF" >

< p> please fill in this page with the sales settlement records for each month up to this month. < 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 circular statement to make a sales settlement record table, the above code clips to the memo book to Save as the doloop.asp, and in the browser HTTP way browsing, according to the current month's difference, you will see the following figure the result.

Let's analyze this program, our goal 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 function month () and now () to get the current month, and finally create a loop, when count Value is less than the current month's value plus 1, the month value and a horizontal line are displayed and the value of Count is added to 1, and the loop statement repeats until the above condition is false to exit the loop. Where count is greater than 13, use Exit Do to exit the loop immediately.

The Do Loop statement can also use the following syntax:

Todo

[Statements] [Exit do]

[Statements] Loop [{while | Until} condition]

While ... Wend statements are provided to users who are familiar with their usage. But because while ... Wend lacks flexibility, so it is advisable to use do ... The Loop statement. Let's look at the for Next statement below. For ..... The Next statement is used to run the statement block for a specified number of times, using a counter variable in the loop, with the value increasing or decreasing 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 causes the counter variable to add 1 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 in the counter variable. In the following example, the counter variable J increases by 2 each time. After the loop ends, 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

Total = Total + j

Next

MsgBox "sum is & Total &". "

End Sub

To decrement the counter variable, you can set the 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 is reduced by 2 each time. After the loop ends, 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 for ... before the counter reaches its termination value. Next statement. Because it is usually only possible to exit the loop under certain special circumstances (for example, when an error occurs), 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 take a 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 the 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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.