6. asp script statement Loop

Source: Internet
Author: User
Tags html form input
In the previous two articles, we learned the variables, functions, procedures, and condition statements of the script language VBScript. This article will continue to introduce the circular statements of vbscipt, summarize the application of scripting language in ASP. 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 a programming language, then I am wrong. The characteristic of ASP Dynamic Server Page environment is that it is written in one or more scripting languages. The scripting language can be seen as a simplified version of the programming language, which is easy to learn and understand, this provides great convenience for the designers of dynamic websites. It can be said that the proper use of the script language is directly related to the advantages and disadvantages of ASP applications. After learning the functions and condition statements of the script language VBScript in the previous article, let's continue to look at the cyclic statements in VBScript.

Loop statements are used to execute program code repeatedly. loops can be divided into three types: one is to execute statements repeatedly before the condition changes to false, and the other is to execute statements repeatedly before the condition changes to true, the other type repeats the statement according to the specified number of times. The following statements can be used in VBScript:
Do... loop: loop when the (or until) condition is "true.
While... Wend: loop when the condition is "true.
For... Next: specify the number of cycles and use the counter to run the statement repeatedly.
For each... Next: executes a set of statements repeatedly for each element in the set or array. Let's take a look at do... loop, which is a block of statements that can be run multiple times. When the condition is "true" or before the condition changes to "true", the statement block is repeatedly executed. See the following example: <HTML> <Title> doloop. asp </title> <body bgcolor = "# ffffff"> <P> 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>"
If counter> 13 then
Exit do
End if
Counter = counter + 1
Loop
%>
<HR> </body>

This ASP program uses the cyclic statement to create a sales settlement record table, and cut and paste the above Code into the workbook and save it as doloop. ASP, and browse through HTTP in the browser. According to the current month, you will see the result as shown in.

  

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 Count value is less than the value of the current month and 1 is added, that is, the month value and a horizontal line are displayed, and the count value is added to 1. The loop statement is executed repeatedly until the preceding conditions are false and the loop is exited. If count is greater than 13, use exit do to exit the loop immediately.
The do loop statement can also use the following syntax:

Do

[Statements] [exit do]

[Statements] loop [{While | until} condition]

The while... Wend statement is provided for users who are familiar with its usage. However, because while... Wend lacks flexibility, we recommend that you use the do... loop statement. Let's take a look at the for next statement. The for... next statement is used to run the specified number of times in the statement block and use the counter variable in the loop. The value of this variable increases or decreases with each loop.

The following example repeats the process myproc 50 times. The for statement specifies the counter variable X, its start value, and end value. The next statement adds 1 to the counter variable 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 each increase or decrease of the counter variable. In the following example, the counter variable J is added with 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 10 Step 2
Total = total + J
Next
The sum of msgbox "is" & total &". "
End sub

To reduce the number of counter variables, set step to a negative value. The end value of the counter variable must be smaller than the start value. 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 = 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 in some special circumstances (for example, when an error occurs), you can exit the loop... then... use the exit for statement 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. The loop for each... Next is similar to the loop for... next. For each... next does not mean to run the specified number of times, but repeats a group of statements for each element or object set in the array. 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 the group contains at least one element, it will be executed in the for each block. Once a loop is entered, all statements in the loop are executed for the first element in the group. As long as there are other elements in the group, the statement in the loop is executed for each element. When there are no other elements in the group, exit the loop and continue the execution from the statement after the next statement.

At this point, we have completed learning all the basic knowledge of the script language VBScript, but you cannot use VBscript skillfully by reading the existing articles, you must improve your level through continuous practice. Of course, if you are familiar with C, you can also use JavaScript as the scripting language for ASP applications. I wonder if you have found it difficult to debug ASP programs because there are no ready-made tools. Here I will give you a brief introduction to Microsoft Script debugger, we can use it for a certain amount of program debugging.

The Microsoft Script debugger (script error detection tool) included in iis4.0 provides the error detection function of the script program. You can use the Microsoft Script debugging tool to debug scripts written using VBScript and JScript, and Java applets, beans, and ActiveX components.

Some script programs are executed in the client browser, and some script programs (<%... %>. Microsoft Script Debugger: detects the script programs executed by the client and the script programs executed by the server. The script program executed in the client browser is executed in the client browser, including the VBScript and JScript parts in the standard HTML code. When the browser loads the HTML code or triggers an event by pressing a button, the HTML code including the script program is executed. The script program executed by the browser on the user end is mainly used for basic checks on HTML form input and other functions.
The script program executed on the server is executed on the IIS server, including in the. ASP program. Execute the command on the IIS server first, execute the result to generate standard HTML code, and then transmit it to the client browser. Script programs executed on the server are mainly used for links between multiple web pages, processing of HTML form input, and accessing database data on the server.

Microsoft Script debugger provides the following debugging functions:

1. Set a breakpoint

2. Track the script program step by step.

3. Set bookmarks.

4. View call stacks.

5. View and change the value.

6. execute script commands.

From the next article, we will start to learn ASP built-in objects. Stay tuned.

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.