For+Loops

Loops are used when you want some code to be executed a number of times or until a condition is satisfied. There are three types of loops we will go through below.

// “For … Next” Loops  //

The “For … Next” loop is probably the most popular loop statement in Visual Basic. When you code it, you are basically saying “I want to do some statements, a certain number of times, using this variable”. This is the syntax of the “For … Next” loop:

** // For  // ****  index = start // To // end [// Step // stepnum]  ** ** [ Statements ] // Next // index **


 * “For” is the ** keyword ** you use to start the loop
 * “index” is a user defined // numeric // variable that the loop uses as a counter
 * “start” is the // number // from which the loop starts from
 * “To” is the ** keyword ** which separates the start and end numbers
 * “end” is the // number // at which the loop stops
 * “Step” is an ** optional keyword ** indicating how much the loop should step by each time through
 * “stepnum” is a // number // that indicates the size of increment / decrement the step should have (this can be a negative number)
 * “Next” is the ** keyword ** that completes the loop
 * “index” the same as above but here we are simply identifying the variable that is updated by the Next keyword (this is optional, but recommended for readability)

Notice that with a “for” loop, you are doing something a certain number of times (that number is known before you enter the loop).