Repetition (Looping Control) Structures Selection control structures are fine for executing a block of code once for a given condition. Looping control structures are used to execute a block of code multiple times until a condition is met. It is extremely important that the condition is met at some point in the loop, or you get what is known as an endless loop - one that never stops without killing the program. Four basic loop structures are: For-Next : a pre-test loop see page VB340 for the syntax of this loop While-Wend : a pre-test loop Syntax While condition [loop instructions] Wend Do-While : a post-test loop Do-Until : a post-test loop see page VB345 for the syntax of these loops The first three loops will execute the code as long as, or while, the condition is true. The last loop will execute the code until the condition is true. All loops have three basic parts: A looping variable A condition against which to test the value of the looping variable An increment/decrement assignment for the looping variable A pre-test loop is one in which the condition is tested before the code block is executed. This means that the code block may never be executed. A post-test loop is one in which the condition is tested after the code block is executed. This means that the code block will always execute at least once. The steps that take place in a pre-test loop are: 1. Initialize the looping variable 2. Test the looping variable 3. If the test is true 4. execute the code 5. increment/decrement the looping variable 6. return to step 2 7. If the test is false, exit the loop The steps that take place in a post-test loop are: 1. Initialize the looping variable 2. Execute the code 3. Test the looping variable 4. If the test is true 5. increment/decrement the looping variable 6. return to step 2 7. If the test is false, exit the loop The For-Next Loop The For-Next loop is used when you have a good idea of how many times the loop will execute. In the first example below, we know that it will loop exactly 10 times. In the rest of the examples, we know the loop will execute as many times as the value of the variable. In a For-Next loop, the looping variable, by default, always increments by 1 This will execute code as long as the condition is true. Identify the three basic parts in the loops below initialize, test, increment/decrement Dim intLoop as Integer Dim intTriangle as Integer Dim intFactorial as Integer Dim intSumOfOdds as Integer For intLoop = 1 to 10 Next For intLoop = 1 to intNumber 'this loop will calculate the triangle value of a number intTriangle = intTriangle + intLoop Next For intLoop = 1 to intNumber 'this loop will calculate the triangle value of a number ' and the factorial value of a number intTriangle = intTriangle + intLoop intFactorial =intFactorial * intLoop Next For intLoop = 1 to intNumber Step 2 'this loop will calculate the sum of the odd numbers intSumOfOdds = intSumOfOdds + intLoop Next For intLoop = intNumber to 1 Step -2 'this loop will calculate the sum of the odd numbers 'in reverse intSumOfOdds = intSumOfOdds + intLoop Next The While-Wend Loop This will execute code while the condition is true. Identify the three basic parts in the loops below initialize, test, increment/decrement Dim intLoop as Integer Dim intTriangle as Integer Dim intFactorial as Integer intLoop = 1 While intLoop <= 10 intLoop = intLoop + 1 Wend intLoop = 1 While intLoop <= intNumber 'this loop will calculate the triangle value of a number ' and the factorial value of a number intTriangle = intTriangle + intLoop intFactorial =intFactorial * intLoop intLoop = intLoop + 1 Wend The examples above are very similar to a For-Next loop, so there is no real advantage to using it. The real power of a While-Wend loop is when you don't know when the condition will be met. For example, if you are reading lines of text from a file, you don't know in advance how many lines you will read until you reach the end of the file. So, instead you test for the end of file marker: While Not EOF(1) ' read a line from the file Wend Our test is for when we reach the end of the file, that's what EOF means. We will explore this in more detail in the next topic. The Do-While Loop This will execute code while the condition is true. Identify the three basic parts in the loops below initialize, test, increment/decrement Dim intLoop as Integer Dim intTriangle as Integer Dim intFactorial as Integer intLoop = 1 Do 'this loop will calculate the triangle value of a number ' and the factorial value of a number intTriangle = intTriangle + intLoop intFactorial =intFactorial * intLoop intLoop = intLoop + 1 Loop While intLoop <= intNumber The Do-Until Loop This will execute code until the condition is true. Identify the three basic parts in the loops below initialize, test, increment/decrement Dim intLoop as Integer Dim intTriangle as Integer Dim intFactorial as Integer intLoop = 1 Do 'this loop will calculate the triangle value of a number ' and the factorial value of a number intTriangle = intTriangle + intLoop intFactorial =intFactorial * intLoop intLoop = intLoop + 1 Loop Until intLoop > intNumber Some Additional concepts used with Loops: Counters A counter is always incremented by 1 inside of a loop, and is used to count how many Accumulators An accumulator is used to calculate a cumulative quantity inside a loop Example: To calculate your test average, you would first need to count how many tests you've take. This value is stored in a counter variable. Then, you would need to know the sum of all the grades on your tests. This value is stored in an accumulator variable. Finally, divide the sum of the grades (accumulator) by the number of tests (counter) to get your average. A final note: The book does not discuss While-Wend loops in this chapter. However, when we move on to the next topic, you will see a specific application of this kind of loop. Classroom Exercises: On pages VB342-344, follow the directions in the beige boxes for Examples 1-2 On pages VB348-354, follow the directions in the beige boxes for two Examples Control Arrays An array is simply a collection of related items. A common example is top 10 lists. If you want to know the 5th most popular song on the pop chart for last week, you scan down the list until you find the 5, then you read the song title listed in that spot. The 5 is the index, the value you use for locating the item. The song title is the contents at that index position. This would be an example of a String array, a list of String values. Arrays are index starting at 0, so the first item on the list has an index of 0, the second an index of 1, and so on. A control array is a collection of related controls. You refer to a specific control in the array by using the name of the control and the index value. If you have an array of Option Buttons named optStates, the first Option Button in the list is referred to as optStates(0). For your homework, you'll be creating two arrays of Option Buttons. One array will for States, the other for Capitals. It's very important that the Option Buttons for the arrays are drawn onto the Frame that holds them. Remember, a Frame is a way of grouping together related controls. There are two ways to label the arrays. In the first example, the arrays are indexed in ascending order, top to bottom. States Capitals 0 California 0 Denver 1 Colorado 1 Madison 2 Illinois 2 Sacramento 3 Oregon 3 Salem 4 Wisconsin 4 Springfield To determine if the user has selected matching State and Capital, you need to do the following: 1. Loop through the array of states to find the one selected. 2. For the one selected, determine if the correct capital was selected. The code is complex, but looks something like this: 'loop through the array of states 'which are indexed from 0 to 4 For intLoop = 0 to 4 'the loop variable is also being used as the index to the array 'test if the Option Button at the current loop position is selected If optStates(intLoop).Value Then 'we know we have a state selected, but which one? Select Case intLoop 'the first state Option Button is selected Case 0 'test to see if the matching capital is selected If optStates(2).Value Then strMessage = "Correct" End If End Select End If Next The second example matches the index values of the states and capitals. States Capitals 0 California 1 Denver 1 Colorado 4 Madison 2 Illinois 0 Sacramento 3 Oregon 3 Salem 4 Wisconsin 2 Springfield The code for this is simpler. 1. Loop through the array 2. If both state and capital at the loop variable index position are selected, then set the message For intLoop = 0 to 4 'the loop variable is also being used as the index to the array 'test if the Option Button at the current loop position is selected If optStates(intLoop).Value = True And optCapitals(intLoop) = True Then strMessage = "Correct" End If Next Additional Clarification of the Do Loop The type of loop is determined by when the condition is tested. If the condition is tested BEFORE the code is executed, then it is a PRE-TEST loop If the condition is tested AFTER the code is executed, then it is a POST-TEST loop Pre-Test Do While x < 5 [code] Loop Do Until x = 5 [code] Loop Post-Test Do [code] Loop While x < 5 Do [code] Loop Until x = 5