A loop is a technique used to repeat an action. The Visual Basic language presents many variations of loops. They combine the Do and the Loop keywords. The formula of the Do... Loop While loop is: Do Statement(s) Loop While Condition
As you may guess already, the Condition must provide a way for it to be true or to be false. Otherwise, the looping would be executed over and over again. An alternative to the Do... Loop While uses the following formula: Do Statement(s) Loop Until Condition Once again, the Statement(s) section executes first. Then, the Condition is checked. If the Condition is true, the compiler returns to the Statement(s) section to execute it. This will continue until the Condition becomes false. Once the Condition becomes false, the compiler gets out of this loop and continues with the section under the Loop Until line.
As mentioned above, the Do While... Loop expression executes a statement first before checking a condition that would allow it to repeat. If you want to check a condition first before executing a statement, you can use another version as Do While... Loop. Its formula is: Do While Condition Statement(s) Loop In this case, the compiler checks the Condition first. If the Condition is true, the compiler then executes the Statement(s) and checks the Condition again. If the Condition is false, or when the Condition becomes false, the compiler skips the Statement(s) section and continues with the code below the Loop keyword.
An alternative to the Do While... Loop loop uses the following formula: Do Until Condition Statement(s) Loop This loop works like the Do While... Loop expression. The compiler examines the Condition first. If the condition is true, then it executes the Statement(s) section.
The looping statements we reviewed above are used when you don't know or can't anticipate the number of times a condition needs to be checked in order to execute a statement. If you know with certainty how many times you want to execute a statement, you can use another form of loops that use the For...Next expression. One of the loop counters you can use is For...To...Next. Its formula is: For Counter = Start To End Statement(s) Next Used for counting, the expression begins counting at the Start point. Then it examines whether the current value (after starting to count) is lower than End. If that's the case, it then executes the Statement(s). Next, it increments the value of Counter by 1 and examines the condition again. This process goes on until the value of Counter becomes equal to the End value. Once this condition is reached, the looping stops. Here is an example: <html> <head> <title>Exercise</title> </head> <body> <% Dim Number As Short For Number = 5 To 16 Response.Write(Number) Response.Write("<br />") Next Response.Write("Counting Stopped at: " & Number) %> </body> </html> This would produce: The formula above will increment the counting by 1 at the end of each statement. If you want to control how the incrementing processes, you can set your own, using the Step option. Here is the formula: For Counter = Start To End Step Increment Statement(s) Next You can set the incrementing value to your choice. If the value of Increment is positive, the Counter will be added its value. Here is an example: <html> <head> <title>Exercise</title> </head> <body> <% Dim Number As Short For Number = 5 To 42 Step 4 Response.Write(Number) Response.Write("<br />") Next Response.Write("Counting Stopped at: " & Number) %> </body> </html> This would produce:
You can also set a negative value to the Increment factor, in which case the Counter will be subtracted the set value. |
|
|||||||||||||||||||||
|