Working with Visual Basic's Looping Control StructuresLooping control structures allow for a set of instructions to be executed a repeated number of times. The number of times the code is repeated can be a fixed number of times, such as with For ... Next loops, or repeated until some condition is met, such as with Do ... Loop loops. In the next three sections we'll be looking at two of Visual Basic's looping constructs: For ... Next loops and Do ... Loop loops. These two constructs are, at a high level, equivalent, though they have differing syntax. That is, they accomplish essentially the same taskrepeating a set of instructions a certain number of times. In fact, either one of the two looping structures we'll be examining can be implemented with the other. Using For ... Next LoopsNext looping control structure is likely the looping structure best suited for your needs. The For ... Next loop has the following syntax: For integerVariable = start to stop Instruction1 Instruction2 ... InstructionN Next integerVariable
The semantics of the For loop are as follows: The variable integerVariable, which should be an integer variable type (Short, Integer, or Long), is assigned the value start. This variable is often referred to as the looping variable.
After the looping variable is initially assigned to the start value, instructions Instruction1 through InstructionN are executed. After these instructions are executed, the value of the looping variable is incremented by one. If, at this point, the value of the looping variable is less than or equal to stop, instructions Instruction1 through InstructionN are executed again, the value of the looping variable is incremented by one, and again, the looping variable's value is checked against stop. The loop continues to execute, with the value of the looping variable being incremented at each loop iteration, until the value of the looping variable is greater than stop.
So, to display the message "Hello, World!" in the user's browser three times, you could put the following For ... Next loop in the ASP.NET web page's Page_Load event handler: Dim i as Integer For i = 1 to 3 LabelWebControl.Text &= "Hello, World!" Next i The first thing we do is create an integer variable named i. When the For loop executes, i is initially assigned the value 1. Then the body of the loop is executed, which concatenates the string "Hello, World!" to the current value of some Label Web control's Text property. (Recall that &= is an operator that takes the value of the variable on the left side and concatenates to it the value on the right side, saving the resulting concatenation back to the variable on the left side. After the body finishes executing, the value of i is incremented by one. A check is then made to see whether i's current value is less than or equal to 3. The reason is that, at this point, i equals 2. Therefore, the loop body is executed again, which will again concatenate "Hello, World!" to the Label's Text property. As before, i is incremented and compared to see whether it is less than or equal to 3. Being equal to 3, i is indeed less than or equal to 3, so the loop body executes again. After concatenating "Hello, World!" to the Label's Text property, i is (again) incremented, equaling 4. At this point, i is not less than or equal to 3, so the For loop body does not execute. Instead, the line of code immediately following the Next i executes. Incrementing the Looping Variable by More Than One Each Loop IterationThe standard For loop syntax increments the looping variable by one with each iteration of the loop body. But what if you want to increment the looping variable by more than one, or what if you want to decrement the looping variable? To accommodate for different looping variable increments per loop iteration, Visual Basic's For loop can have an optional Step portion that indicates specifically the amount that the looping variable should be incremented per loop. The syntax of the For loop with a Step clause is as follows: For integerVariable = start to stop Step stepAmount Instruction1 Instruction2 ... InstructionN Next integerVariable Imagine that you wanted to perform a loop that iterated through the even numbers between 0 and 10. You could accomplish this using a For ... Next loop that started at 0 and went to 10, incrementing by 2 at each iteration. The syntax for such a For loop would be Dim evens as Integer For evens = 0 to 10 Step 2 LabelWebControl.Text &= evens & " is an even number.<br />" Next evens This For loop, if added to an ASP.NET web page's Page_Load event handler, will display in the specified Label: 0 is an even number. 2 is an even number. 4 is an even number. ... 10 is an even number.
Do ... Loop LoopsThe Do ... Loop loop, often referred to simply as a Do loop, executes the loop body either while a condition holds or until a condition is met, depending on the syntax. First, let's consider the Do loop that iterates while a condition holds. The syntax for such a loop is as follows: Do While condition Instruction1 Instruction2 ... InstructionN Loop condition is an expression that evaluates to a Boolean value. When the Do loop is encountered, the condition is checked. If it evaluates to true, the loop bodyinstructions Instruction1 through InstructionNis executed. After the loop body has executed, the condition is checked again. If it is still TRue, the loop body is executed again. This process repeats until condition evaluates to False after the execution of the loop body. A Do loop can also be constructed so that its loop body is executed repeatedly until a condition is met. The syntax for this form of the Do loop is Do Until condition Instruction1 Instruction2 ... InstructionN Loop Here, the semantics of the Do loop are as follows: When the Do loop is encountered, the condition is checked. If it evaluates to False, the loop bodyinstructions Instruction1 through InstructionNis executed. After the loop body has executed, the condition is checked again. If it is still False, the loop body is executed again. This process repeats until condition evaluates to true after the execution of the loop body. Like with the For loop, the Do loop can be used to display the even numbers between 0 and 10. To accomplish this, we could use the following syntax: Dim number as Integer = 0 Do While number <= 10 LabelWebControl.Text &= number & " is an even number.<br />" number += 2 Loop Here, an Integer number is created and assigned the value 0. The Do loop then iterates while the value of number is less than or equal to 10. Because 0 (number's initial value) is less than or equal to 10, the loop body executes. In the loop body a message ("0 is an even number") is appended to a Label Web control's Text property and then the value of number is incremented by 2. This loop will continue to be executed until the end of the sixth iteration, after which number will have the value 12.
|