Visual Basic For Loops

Revision as of 17:57, 6 August 2007 by Neil (Talk | contribs) (Creating a Visual Basic For Loop)

Revision as of 17:57, 6 August 2007 by Neil (Talk | contribs) (Creating a Visual Basic For Loop)

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 repeatedlyt, 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.

Creating a Visual Basic For Loop

The Visual Basic For loop is ideal for situations wheer 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 copntains information about how many time 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