Difference between revisions of "Visual Basic For Loops"

From Techotopia
Jump to: navigation, search
(Creating a Visual Basic For Loop)
(Continuing a For Loop)
Line 78: Line 78:
 
       MessageBox.Show("Counter is currently: " + CStr(intCount))
 
       MessageBox.Show("Counter is currently: " + CStr(intCount))
 
Next intCount
 
Next intCount
<pre>
+
</pre>
  
 
When the above example is executed, a MessageBox will appear for each iteration of the loop except for when the ''intCount'' value is equal to 3.
 
When the above example is executed, a MessageBox will appear for each iteration of the loop except for when the ''intCount'' value is equal to 3.

Revision as of 18:18, 8 April 2009

PreviousTable of ContentsNext
Visual Basic Flow ControlVisual Basic Do ... Loops


Computers are incredibly good at repeating the same task over and over very quickly. One of the key functions of a programming language such as Visual Basic is to make it easy for a developer to program a computer to perform these repetitive tasks. Visual Basic provides a number of language structures which which tell a program to perform a task repeatedly, either a specific number of times, or until certain conditions are met. In this chapter of Visual Basic essentials we will look at each of these in detail.


Contents


Creating a Visual Basic For Loop

<google>ADSDAQBOX_FLOW</google> The Visual Basic For loop is ideal for situations where a task needs to be performed a specific number of times. For example, perhaps a value needs to be added to a variable 100 times. A Visual Basic For loop consists of a header, a code block and a next statement. The header contains information about how many times the loop is to be performed, the code block contains the statements to be executed on each iteration and the next statement sends the loop back to the header to repeat. The syntax of a For loop is as follows:

For counterVariable = fromValue To toValue
...VB Statements...
Next counterVariable

The easiest way to gain an understanding of For loops is to see an example. The following code excerpt declares a variable to act as the loop counter and the performs a loop 5 times, displaying the value of the counter on each loop iteration:

Dim intCount As Integer

For intCount = 0 To 5
       MessageBox.Show("Counter is currently: " + CStr(intCount))
Next intCount

For loops can also be nested. In a nested For Loop, the inner loop is executed for each iteration of the outer loop. For example:

Dim intCount1, intCount2 As Integer

For intCount1 = 0 To 5
       For intCount2 = 0 To 10
              'VB code here
       Next intCount2
Next intCount1

Incrementing a For Loop by a Value Greater Than 1

By default Visual Basic increments the For counter variable by a value of 1. This number can be increased using the Step keyword at the end of the For header statement. For example, to increment the counter by 10:

Dim intCount As Integer

For intCount = 0 To 100 Step 10
       MessageBox.Show("Counter is currently: " + CStr(intCount))
Next intCount

In the above example, the increment value applied to the counter variable is increased to 10.


Early Exit of a For Loop

it is often necessary to exit a For loop before it has completed the specified number of loop iterations. This is achieved using the Visual Basic Exit For statement. This is typically used in conjunction with a conditional If statement. The Following code example causes the loop to exit if the counter is equal to 3:

Dim intCount As Integer

For intCount = 0 To 5
       If intCount = 3 Then Exit For
       MessageBox.Show("Counter is currently: " + CStr(intCount))
Next intCount

Continuing a For Loop

The Exit For command causes the For loop to exit entirely. Another option is to skip the remaining statements in a loop under certain circumstances, let continue to loop. This is achieved using the Continue For statement. The following example skips the code in the body of the For statement if counter is 3, and continues the loop at the next iteration:

Dim intCount As Integer

For intCount = 0 To 5
       If intCount = 3 Then Continue For
       MessageBox.Show("Counter is currently: " + CStr(intCount))
Next intCount

When the above example is executed, a MessageBox will appear for each iteration of the loop except for when the intCount value is equal to 3.