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.