Basic Introduction to ASP fifth (ASP Script Loop statement) _ Application Tips

Source: Internet
Author: User
Tags html form html form input

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 "> 
 

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]

If there is at least one element in the group, it goes into the for each block execution. Once you enter the loop, all the statements in the loop are executed first on the first element in the group. As long as there are other elements in the group, the statements in the loop are executed on each element. Exits the loop when there are no more elements in the group, and then continues execution from the statement following the next statement.

So far, we've done all the basics of scripting language VBScript, but you can't skillfully use VBScript by reading the existing articles, you have to improve your level by practicing. Of course, if you are familiar with C, you can also choose JavaScript as the scripting language for ASP applications. I do not know if you have found that the ASP program debugging more difficult, because there is no ready-made tools, here I give you a brief introduction of Microsoft Script Debugger, we can use it to carry out a certain amount of program debugging work.

The Microsoft script Debugger (script debugging Tool) included with IIS4.0 provides scripting debugging capabilities. You can use the Microsoft Script debugging tool for debugging work with VBScript, JScript, and Java applets, beans, and ActiveX components.

Some scripts are executed in the client browser, and some scripts (<% ...%>) are executed on the server side.    Microsoft script Debugger, you can debug script programs executed by the client and server-side execution scripts. Scripts executed in the client browser are executed in the browser of the client, including VBScript and Jscript portions of standard HTML code. The HTML code that includes the script is executed when the browser loads this HTML code or, for example, presses a button to trigger an event. A script program executed by a client browser, used primarily for basic checks of HTML form input.
The scripting programs executed on the server side are executed on the IIS server side, including in the. asp program. Executed first on the IIS server, execution results generate standard HTML code, and then transfer to the client browser. Server-Side execution of the script, mainly for the links between multiple Web pages, HTML form input processing, as well as access to the server database data and so on.

Microsoft Script Debugger provides the following debugging features:

1, set the point of interruption

2, step-by-Step tracking script program.

3, set the bookmark.

4, view call stacking.

5, view and change the value.

6, execute the script instruction.

From the beginning of the next article, we will begin to learn the ASP's built-in objects, we do not go away.

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.