Do you know how to use the for loop in asp? Next, the editor of Foxin will explain to you how to use the for loop in Asp based on his accumulated experience, for reference by friends in need.
A loop refers to the repeated execution of a set of statements multiple times in a specified case. Most commonly used (for …next, do …loop),
1. Repeated execution statements are called loop statements.
Loop statements can be divided into three types:
(1) Repeat the statement when the condition is not false.
(2) Repeat the statement before the condition becomes true.
(3) Repeat the statement according to the specified number of times.
2. For…Next Loop If the number of repeated operations is fixed, using For…Next Loop is a good choice. In addition, the For Each…Next Loop with a very similar syntax will also be introduced. It is suitable for repeated loops in arrays or sets. (I) For…Next Use initial value, final value, step size and loop variables in the syntax of For…Next Loop to complete the work of repeated counting. When the loop is first executed, the loop variable begins to accumulate step size from the initial value until it is equal to or exceeds the final value.
For example:
The initial value is 1, the final value is 10, and the step size is 2.
The loop will be executed 5 times.
The values of the loop variable are 1, 3, 5, 7, and 9, respectively.
The sixth time is that the loop variable is accumulated to 11 and has exceeded 10, so the loop is no longer executed.
3. The syntax of the For…Next loop is as follows:
For loop variable = initial value To final value Step step
Program statements…
[ Exit For]
Program statements…
Next
illustrate:
(1) Loop variable: This variable usually uses integers, but it can also be of other numerical types when necessary, and it is a numerical variable.
(2) Initial value: This is the initial value of the loop variable, which is a numerical type. Its value is not necessarily an integer or a decimal. VB will automatically get an integer for it.
(3) Final value: The final value of the loop variable, which is a numerical type. Its value is not necessarily an integer or a decimal. VB will automatically get an integer for it.
(4) Step size: The number of increments of the loop variables each time, the numerical type of the step size should be the same as the loop variable. The value can be a positive number (incremental loop) or a negative number (increasing loop). When it cannot be 0, if not specified, the step size defaults to 1.
(5) Loop body: A statement between a For statement and a Next statement can be one or more statements.
(6), Next: is a loop terminal statement. The "loop variable" after Next must be the same as the "loop variable" in the For statement.
For example: for i=0 to 10 Step 1
......
Next
Execution process:
(1) Assign the initial value 1 to the loop variable i
(2) Compare the value of i with the final value 10. If i>10, the loop will jump out and execute (5), otherwise the loop body will be executed.
(3) Add i to a step size value, that is, i=i+1
(4), return (2) Continue to execute
(5) Execute the code after the Next statement
The function of this cycle is to determine the number of cycles based on the initial value, final value and step size in the FOR statement, and repeatedly execute each statement in the loop body. For…Next loops follow the principle of "check first, then execute", that is, check whether the loop variable exceeds the final value,
Then decide whether to execute the loop body. Therefore, the loop body will not be executed in the following cases,
(1) When the step size is positive, the initial value is greater than the final value
(2) When the step size is negative, the initial value is less than the final value
When the initial value is equal to the final value, a loop is performed regardless of whether the step size is positive or negative.
The For statement and the Next statement must appear in pairs and cannot be used alone, and the For statement must be preceded by the Next statement.
The general formula for the number of cycles N is:
N=Int((final value-initial value)/step length) + 1
When the initial value in the For…Next loop is less than the final value, the step size must be a positive value, indicating the count from small to large; if the initial value is greater than the final value, the step size must be set to negative value, indicating the count from large to small.
For example: When the initial value is less than the final value
The code copy is as follows:<%
for i=0 to 10 Step 1 'Default step size is 1 and you can not write it
Response.Write(i&"<br>")
Next
%>
For example: When the initial value is greater than the final value
The code copy is as follows:<%
for i=10 to 1 Step -1
Response.Write(i&"<br>")
Next
%>
When the step size is greater than 0: When the original value is <= final value, the statement between For…Next will be executed once, and then the loop variable will be added with the step size. If it is still less than the final value, the statement between For…Next will be executed again. Until the loop variable is accumulated to be greater than the final value, it will jump out of the For…Next loop.
The code copy is as follows:<%
for i=1 to 1 Step 0.5
Response.Write(i&"<br>")
Next
%>
When the step size is less than 0: When the original value >= final value, the statement in the loop will be executed once, and then the loop variable will be subtracted from the step size (negative). If the loop variable is still >= final value, the statement in the loop will be executed again, and the loop variable will not be separated from the For…Next loop until the loop variable is accumulated to less than the final value.
The execution process of loop statements:
The code copy is as follows:<%
for i=2 to 1 Step -1
Response.Write(i&"<br>")
Next
%>
4. The execution process of loop statements:
(1) Enter the loop through the For statement. If the initial value, final value and step size of the loop variable are arithmetic expressions, the system first calculates the value of the arithmetic expression, then assigns the initial value to the loop variable, and stores the value of the final value and step size into memory
(2) Compare the value of the loop variable with the final value. When the value of the loop variable does not exceed the range of the final value, each statement in the loop body will be executed in sequence. If the value of the loop variable exceeds the range of the final value, the loop is exited and the subsequent statement of NEXT is executed.
(3) After executing each statement in the loop body, encounter the NEXT statement, add the loop variable by one step value, then go to the final value, compare, if it still does not exceed the starting point, execute the statements in the loop body again, repeat the execution until the value of the loop variable exceeds the range of the final value, end the loop, and execute Next subsequent statements.
Classroom examples:
【Example 1】
Simple 1 to 10, add 1 each time using the cycle characteristics
The code copy is as follows:<%
for i=0 to 10
sum=sum+i
Next
Response.Write(sum)
%>
Final display result: 55
【Example 2】
The code copy is as follows:<%
for i=0 to 10 step 2
Response.Write(i&"<br>")
Next
%>
Final display results: 0, 2.4, 6, 8, 10
【Example 3】
The code copy is as follows:<%
for i=0 to 10
Response.Write(i&"<br>")
if i=5 then
exit for' Forced end loop
end if
Next
%>
In the above program, i will be accumulated from 1 to 10 and executed 10 times, but when i is accumulated from 5, it conforms to the judgment formula of i=5.
If the Exit For statement is executed, the loop is jumped away, so the final web page shows the result "0,1,2,3,4,5,"
【Example 4】
The code copy is as follows:<%
for i=10 to 0 step -1
sum=sum+i
Next
Response.Write(sum)
%>
Final display result: 55
5. Let’s use examples to illustrate the different usages of For…Next loops:
【Example 1】
The code copy is as follows:<% For i=1 To 10
Response.write i&","
Next
%>
The above program will add I, and the i in the loop will add 1 from 1, 2, 3,...each time, 1, until 10, and a total of 10 executions. Finally, the web page will display "1, 2, 3, 4, 5, 6, 7, 8, 9, 10,"
【Example 2】
The code copy is as follows:<%
For a=1 To 10 Step 0.5
Response.write a&","
Next
%>
In the above program a, each time will be accumulated by 0.5, and a total of 20 executions will be performed. Finally, in the web page, the "1, 1.5, 2, 2.5, 3, 3.5...
9.5,
【Example 3】
The code copy is as follows:<%
For j=10 To 1 Step -1
Response.write j&","
Next
%>
In the above program, j will be reduced from 10 to 1, each time by 1, and finally, on the web page, "10, 9, 8, 7, 6, 5, 4, 3, 2, 1,"
【Example 4】
The initial value, final value and step size in the loop are read only once when the program is executed at the beginning, and then even if its value is changed within the loop, it will not affect the loop execution.
The code copy is as follows:<%
StepNum=1
EndNum=1
For i=1 To endNum Step stepNum
Response.write i&","
StepNum=StepNum-0.1
EndNum=EndNum+1
Next
%>
In the above program, the step size is deliberately reduced by 0.1 each time in the loop and the final value is increased by 1 each time, in order to make i never reach the final value. However, because the step size and final value are only read once, they will not change again, so the output result of this program is still "1".
【Example 5】
The loop variables in the loop will change in the loop, so:
The code copy is as follows:<%
For k=1 To 10
Response.Write k&","
Next
%>
The above program will add k, and the k in the loop will add 1, 2, 3,...1 each time will be accumulated until 10, and a total of 10 executions will be performed 10 times, and the web page will be displayed.
Shows "1,2,3,4,5,6,7,8,9,10,
After reading this article, do you know how to use the for loop in asp? If you don't understand, please leave a message to the editor, and the editor will give you a detailed answer~