A loop is the execution of a set of statements repeatedly in a specified case. Most commonly used (for ... next, do ... loop),
1, repeated execution of the statement called the circular statement.
Loop statements can be grouped into three types:
(1), when the condition is not false before the repeated execution of the statement.
(2) Repeat the execution of the statement before the condition becomes true.
(3) Repeat the execution of the statement at the specified number of times.
2, for ... Next Loop If the number of repeated operations is fixed, use for ... Next loop is a good choice, and it will also introduce a very similar syntax for each ... Next loop, which applies to repeated loops in an array or set, (a) for ... Next in for ... The Next Loop's syntax uses the initial, final, step, and loop variables to do the work of repeating counting. When the loop is first executed, the loop variable begins to accumulate a step from the initial value, until it is equal to or exceeds the final.
For example:
The initial value is 1, the final is 10, and the step is 2,
The loop will execute 5 times,
The values of the loop variables are 1, 3, 5, 7, 9, respectively.
The sixth time because the cyclic variable has been added to 11, has exceeded 10, then the loop is no longer performed.
3, for ... The syntax for the Next loop is as follows:
For loop variable = Initial to final value step
Program statement ...
[Exit for]
Program statement ...
Next
Description
(1), loop variable: This variable is usually an integer, but it can be other numeric types if necessary, he is a numeric variable.
(2), initial values: This is the initial value of the loop variable, which is the numeric type. Its value is not necessarily an integer, or a decimal number, VB will automatically give him an integer
(3), end value: The end value of the loop variable, which is the numeric type. Its value is not necessarily an integer, or a decimal number, VB will automatically give him an integer
(4), step size: The increment of the cycle variable each time, the value type of the step should be the same as the loop variable, its values can be positive (ascending loop) or negative (descending cycle), when not 0, if not specifically specified, the step by default is 1.
(5), loop body: A statement between a for statement and a Next statement, which can be one or more statements.
(6), Next: is a loop terminal statement, after Next the "loop variable" and the For statement in the "loop variable" must be the same.
such as: For I=0 to ten step 1
......
Next
Execution process:
(1) and assign the initial value 1 to the cyclic variable i
(2), the value of I compared with the final value of 10, if i>10, then jump out of the loop, execute (5), otherwise execute the loop body.
(3), I add a step value, namely i=i+1
(4), return (2) continue to execute
(5), executing the code following the next statement
The function of this loop is to determine the number of loops and repeatedly execute the statements in the loop, based on the initial, final, and step values of the For statement. For ..... Next Loop 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 execute in the following cases,
(1), when the step is positive, the initial value is greater than the final
(2), when the step length of this negative number, the initial value is less than the final
When the original value equals the final value, a loop is performed regardless of whether the step is positive or negative.
The For statement and Next statement must appear in pairs, cannot be used alone, and the for statement must precede the next statement.
The general formula for the number of cycles N is:
N=int ((final-initial)/step +1
For ..... When the initial value in the next loop is less than the final, the step must be positive, indicating a count from small to large, and if the initial value is greater than the final, the step must be set to a negative number, representing a large to small count.
For example: When the initial value is less than the final
Copy code code as follows:
<%
For i=0 to 1 ' Default step size is 1 to not write
Response.Write (i& "
")
Next
%>
For example: When the initial value is greater than the final
Copy code code as follows:
<%
For i=10 to 1 Step-1
Response.Write (i& "
")
Next
%>
When the step size is greater than 0 o'clock: When the original value <= end value, for ... The statement between next will be executed once, then the loop variable will be added step, if it is less than the final value, then perform a for ... The statement between next, until the loop variable accumulates to greater than the final value, will jump off for ... Next loop.
Copy code code as follows:
<%
For I=1 to 1 step 0.5
Response.Write (i& "
")
Next
%>
When the step size is less than 0 o'clock: The original value >= end value, will execute the statement within the loop once, then the loop variable minus the step (negative), if the loop variable also >= the final value, then execute the statement within the loop, until the loop variable accumulation to less than the final value, will jump away for ... Next loop.
The execution of a loop statement:
Copy code code as follows:
<%
For i=2 to 1 Step-1
Response.Write (i& "
")
Next
%>
4. The execution process of the circular statement:
(1) Enter the loop through the For statement. If the initial, final, and step of the cyclic variable is an arithmetic expression, the system first calculates the value of the arithmetic expression, assigns the initial values to the cyclic variable, and stores the value of the final value and the step size into memory.
(2) The value of the loop variable is compared with the final value, and when the value of the loop variable does not exceed the range of the final value, the individual statements in the loop are executed sequentially. If the value of the loop variable exceeds the range of the end value, the loop is exited and the next subsequent statement is executed.
(3) After executing the statement of the loop body, the next statement is encountered. The loop variable is added to the value of a step, then the final value, compared, if still not more than the start of the Weizhou, then again execute the loop body of the statements, repeat execution, until the value of the loop variable exceeds the range of the final value, the end of the loop, execution next subsequent statement.
Class Examples:
"Example 1"
Simple 1 plus 10, with a cyclic feature plus 1 each time
Copy code code as follows:
<%
For I=0 to 10
Sum=sum+i
Next
Response.Write (SUM)
%>
Final display results: 55
"Example 2"
Copy code code as follows:
<%
For I=0 to ten step 2
Response.Write (i& "
")
Next
%>
Final Display Result: 0,2.4,6,8,10
"Example 3"
Copy code code as follows:
<%
For I=0 to 10
Response.Write (i& "
")
If I=5 Then
Exit for ' Force end loop
End If
Next
%>
In the above procedure, originally I will accumulate from 1 to 10, executes 10 times, but in I accumulates to 5 o'clock, conforms to the i=5 judgment type, knot
The result executes the Exit for statement, jumps off the loop, so the last page displays the "0,1,2,3,4,5,"
"Example 4"
Copy code code as follows:
<%
For i=10 to 0 step-1
Sum=sum+i
Next
Response.Write (SUM)
%>
Final display results: 55
5, we take the example to explain for ... Different usages of Next loop:
"Example 1"
Copy code code as follows:
<% for I=1 to 10
Response.Write i& ","
Next
%>
The above procedure will add up I, and the loop I will be by 1,2,3, ... Add 1 each time, up to 10, a total of 10 times, the last page will show "1,2,3,4,5,6,7,8,9,10,"
"Example 2"
Copy code code as follows:
<%
For A=1 to ten step 0.5
Response.Write a& ","
Next
%>
In the above procedure a will accumulate 0.5 each time, the total execution 20 times, finally in the webpage, displays "1,1.5,2,2.5,3,3.5 ..."
9.5,
"Example 3"
Copy code code as follows:
<%
For j=10 to 1 Step-1
Response.Write j& ","
Next
%>
The above program J will be from 10 to 1, each tired minus 1, finally in the Web page, showing the "10,9,8,7,6,5,4,3,2,1,"
"Example 4"
The initial, final, and step steps in the loop are read only once at the start of the program, and then do not affect the loop execution even if the value is changed within the loop.
Copy code code 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 procedure, intentionally in the loop to reduce the step by 0.1 each time, the final value is added 1 each time, with the intention that I will never reach the end value, but because the step and end values are read only once, it will not change, so the output of this program is still "1".
"Example 5"
The loop variable in the loop is changed in the loop, so:
Copy code code as follows:
<%
For K=1 to 10
Response.Write k& ","
Next
%>
The above program will add K, and the loop of K will be 1,2,3, ... Add 1 each time to 10, a total of 10 times, the last page will show
Shows the 1,2,3,4,5,6,7,8,9,10,