Loops+(VB)

// “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 ** Notice that with a “for” loop, you are doing something a certain number of times (that number is known before you enter the loop). __ Mr. Swaine Destroys His Own Rule!!! Is All Hope Lost? __ We have always said that you should use the “int” prefix when we have an integer. Well, when you go through a loop, your index is going to be an integer value. If you want to call it intLoopIndex, that’s great. However… You will understand why this is when we get into arrays. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 * “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)
 * IN LOOPS YOU ARE ALLOWED TO NAME YOUR INDEX “x” or “y” or “z” TO MAKE IT EASIER TO CODE. THIS IS THE ONLY TIME THAT THIS WILL BE ALLOWED. ||

// A) //// Do … While Loop // This loop works such that it executes the code within the loop as long as a condition is true. This is different from the “For” loop where we executed the loop a certain number of times.  The syntax for the do while loop is a bit simpler, but you have two options:  || **// Do //**** //While// ** ** [ Statements ] **  ** [ Statements ] **  “Do”, “While”, and “Loop” are all keywords that Visual Basic will recognize as loop keywords. This may seem easier than a “For … Then” loop, but, let’s look at this example:  Do While x <=100   txtOutput.text= txtOutput.text & vbNewLine & x    x =x + 1   Loop || In this example, the loop will continue while “x” is below 101. “x” starts at 1, but each time through the loop, it increases by 1.  A big difference between “For … Then” loops and “Do … While” loops is that when you are using a “Do … While” loop, you must change something within the loop so the condition will eventually not be satisfied and the loop will exit. With “For … Then” loops, the loop will eventually exit after so many iterations (a fancy word for number of loops through).
 * // Loop //**
 * **__ OR __** || **// Do //**
 * // Loop While //**** ** ||
 * x = 1
 * Endless Loops: An endless loop occurs when a condition doesn’t change OR you accidentally change it in a way such that the condition is always satisfied. In the above example if we took out the “x = x + 1” line we would have an endless loop. ||

// B) Do … Until Loop // The //Until// keyword does the reverse of the //While// keyword. In other words, the loop continues until an expression evaluates to //true//. As long as the expression returns false, the loop continues. As with the //While// loop the //Until// expression may be placed at the start or end of the loop:  The syntax is as follows:  || ** Do Until ** ** [ Statements ] **  ** [ Statements ] **  So if we were to take the example from the “Do … While” section and rewrite it using a “Do … Until” instead, it would look like this:  Do Until x > 100   txtOutput.text= txtOutput.text & vbNewLine & x    x =x + 1   Loop || Notice that the only difference is that we have changed the “<=” operator to a “>”. In other words, the “Do … While” loop and the “Do … Until” are very similar – one looks for a condition to be true and the other looks for a condition to be false. 
 * Loop **
 * __ OR __** || ** Do **
 * __ OR __** || ** Do **
 * Loop Until ** ||
 * x = 1