isual Basic loop structures allow you to run one or more lines of code repetitively. You can repeat the statements in a loop structure until a condition is True, until a condition is False, a specified number of times, or once for each element in a collection.
The following illustration shows a loop structure that runs a set of statements until a condition becomes true.
Running a set of statements until a condition becomes true
Introduction
Visual Basic allows a procedure to be repeated as many times as long as the processor could support. This is generally called looping . Visual Basic supports several versions of the Do statement.
Do While loop
The Do While loop is perhaps the most common looping statement that you'll put in Visual Basic programs.
Here is the format of the Do While loop:
Code: vb
-------------------------------------------------
Do While condition 'Block of one or more VB statements
Loop
The block of code continues looping as long as condition is true. Whether you insert one or several lines of code for the block doesn't matter. It's vital, however, for the block of code to somehow change a variable used in condition .
The block of code keeps repeating as long as the Do While loop's condition continues to stay true. Eventually, condition must become false or your program will enter an infinite loop and the user will have to break the program's execution by pressing the Ctrl+Break key combination. An infinite loop is a loop that never terminates.
The Do While loop continues executing a block of Visual Basic statements as long as condition is true. As soon as condition becomes false, the loop terminates.
As long as condition is true, the block of code in the body of the loop continues executing. When condition becomes false, the loop terminates. After the loop terminates, Visual Basic begins program execution at the statement following the Loop statement because Loop signals the end of the loop. As soon as Do While's condition becomes false, the loop terminates and doesn't execute even one more time. The Do While's condition appears at the top of the loop. Therefore, if condition is false the first time the loop begins, the body of the loop will never execute.
Example:
Code: vb
-------------------------------------------------
Do while counter <=1000 num.Text=counter counter =counter+1
Loop
The above example will keep on adding until counter >1000.
The Do Until Loop
Whereas the Do While loop continues executing the body of the loop as long as the condition is true, the Do Until loop executes the body of the loop as long as the condition is false. The program's logic at the time of the loop determines which kind of loop works best in a given situation.
Do Until works almost exactly like the Do While loop except that the Do Until loop continues executing the body of the loop until the condition is true. Like the Do While, the Do Until is a multiline looping statement that can execute a block of code that's one or more lines long.
Code: VB
-------------------------------------------------
Do Until (condition) Block of one or more Visual Basic statements
Loop
Remember that the condition must be false for the loop to continue. You can use the Do While or the Do Until for almost any loop.
Example:
Code: vb
-------------------------------------------------
Do until counter>1000 num.Text=counter counter=counter+1
Loop
Other loops
Another pair of Do loops work almost exactly like the two previous loops. Do...Loop While and Do...Loop Until look very much like their counterparts that you learned about earlier. But these new loop formats check their comparison tests at the bottom of the loop rather than at the top. To complete the loop statements, Visual Basic also supports a Do...Loop Until statement. Like the Do...Loop While, the Do...Loop Until statement tests condition at the bottom of the loop. Therefore, the body of the loop executes at least once, no matter what comparison test turns out to be. The loop continues as long as the comparison test result stays false
If a loop begins with a single Do statement, the loop ends with either Loop While or Loop Until. Here is the format of Do...Loop While:
Code: vb
-------------------------------------------------
Do 'Block of one or more VB statements
Loop While condition
Example:
Code: vb
-------------------------------------------------
Do num.Text=counter counter =counter+1
Loop while counter <=1000
Here is the format of Do...Loop Until
Code: vb
-------------------------------------------------
Do 'Block of one or more Visual Basic statements
Loop Until condition
Example:
Code: vb
-------------------------------------------------
Do num.Text=counter counter =counter+1
Loop Until counter <=1000
The For Loop
The For loop (sometimes referred to as the For...Next loop) also creates a loop. Unlike the Do loops, however, the For loop repeats for a specified number of times. The format of the For loop looks a little more daunting than that of the Do loops, but after you master the format, you'll have little trouble implementing For loops when your code needs to repeat a section of code for a specified number of times. "For....Next" Loop the format is:
Code: vb
-------------------------------------------------
For counter=startNumber to endNumber (Step increment) One or more VB statements
Next
Example:
Code: vb
-------------------------------------------------
'(a)
For counter=1 to 10 display.Text=counter
Next
'(b)
For counter=1000 to 5 step -5 counter=counter-10
Next
While...End While Statement (Visual Basic)
Runs a series of statements as long as a given condition is True. While condition [ statements ] [ Continue While ] [ statements ] [ Exit While ] [ statements ] End While
Parts
Term | Definition | condition | Required. Boolean expression. If condition is Nothing, Visual Basic treats it as False. | statements | Optional. One or more statements following While, which run every time condition is True. | Continue While | Optional. Transfers control to the next iteration of the While block. | Exit While | Optional. Transfers control out of the While block. | End While | Required. Terminates the definition of the While block. |
Remarks
Use a While...End While structure when you want to repeat a set of statements an indefinite number of times, as long as a condition remains True. If you want more flexibility with where you test the condition or what result you test it for, you might prefer the Do...Loop Statement (Visual Basic). If you want to repeat the statements a set number of times, the For...Next Statement (Visual Basic) is usually a better choice. The While keyword is also used in the Do...Loop Statement (Visual Basic), the Skip While Clause (Visual Basic) and the Take While Clause (Visual Basic). |
If condition is True, all of the statements run until the End While statement is encountered. Control then returns to the While statement, and condition is again checked. Ifcondition is still True, the process is repeated. If it’s False, control passes to the statement that follows the End While statement.
The While statement always checks the condition before it starts the loop. Looping continues while the condition remains True. If condition is False when you first enter the loop, it doesn’t run even once.
The condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean Data Type (Visual Basic) value (True or False). This expression can include a value of another data type, such as a numeric type, that has been converted to Boolean.
You can nest While loops by placing one loop within another. You can also nest different kinds of control structures within one another. For more information, see Nested Control Structures (Visual Basic).
Exit While
The Exit While statement can provide another way to exit a While loop. Exit While immediately transfers control to the statement that follows the End While statement.
You typically use Exit While after some condition is evaluated (for example, in an If...Then...Else structure). You might want to exit a loop if you detect a condition that makes it unnecessary or impossible to continue iterating, such as an erroneous value or a termination request. You can use Exit While when you test for a condition that could cause an endless loop, which is a loop that could run an extremely large or even infinite number of times. You can then use Exit While to escape the loop.
You can place any number of Exit While statements anywhere in the While loop.
When used within nested While loops, Exit While transfers control out of the innermost loop and into the next higher level of nesting.
The Continue While statement immediately transfers control to the next iteration of the loop. For more information, see Continue Statement (Visual Basic).
Example
In the following example, the statements in the loop continue to run until the index variable is greater than 10.
VB
Dim index As Integer = 0 While index <= 10 Debug.Write(index.ToString & " ") index += 1 End While Debug.WriteLine("") ' Output: 0 1 2 3 4 5 6 7 8 9 10
The following example illustrates the use of the Continue While and Exit While statements.
VB
Dim index As Integer = 0 While index < 100000 index += 1 ' If index is between 5 and 7, continue ' with the next iteration. If index >= 5 And index <= 8 Then Continue While End If ' Display the index. Debug.Write(index.ToString & " ") ' If index is 10, exit the loop. If index = 10 Then Exit While End If End While Debug.WriteLine("") ' Output: 1 2 3 4 9 10
The following example reads all lines in a text file. The OpenText method opens the file and returns a StreamReader that reads the characters. In the While condition, thePeek method of the StreamReader determines whether the file contains additional characters.
VB
Private Sub ShowText(ByVal textFilePath As String) If System.IO.File.Exists(textFilePath) = False Then Debug.WriteLine("File Not Found: " & textFilePath) Else Dim sr As System.IO.StreamReader = System.IO.File.OpenText(textFilePath) While sr.Peek() >= 0 Debug.WriteLine(sr.ReadLine()) End While sr.Close() End If End Sub
Definition of Looping Statement * A looping statement is one in which you want to execute a statement (or many) as many number of times you want. It is useful when you want to check some constraints with a specific value. Eg: to check if all the textboxes on a form have some data entered in them, you can increment the tab index on the form and check if all textboxes !="" (not equal to 'empty') for i=1 to 10 step +1 text1.seltext="a" next i here, i is the counter which will increment 10 times and a textbox 'text1' will have the letter 'a' appended into it for all those 10 times.
* Looping conditional statements are "Do While" and "For". "Do While" is important when you want loop statements until a condition is met An example "Do While" statement could look like the below: Dim X: X = 1 Do While X < 5 X = (X + 1) Loop Debug.Print X The result of X will be 5 if the loop goes correctly. An For statement is useful when you know how many times you want to loop through some statements. Below is an example: Dim i For i = 1 to 5 Debug.Print "This is loop " & i & "." Next i There is also one more looping conditional statement that has no meaningful purpose and can be interchanged with Do While for same results. The conditional statement is called Do Until which would result oppositely from Do While.
* Looping statement can be used to do repeated execution of certain statements. There are different type of looping. for, while, etc.
Putting Loops to Work
One benefit of computer programming is the capability to repeat a set of instructions reliably after they're written. The specific commands you put at the beginning and end of a set of instructions to control their repeated execution are known as loops. These commands tell the computer when and how often to perform the set of instructions. By using loops, you can write simpler programs that would have otherwise required redundant lines of code. Loops can be used to inspect arrays (discussed in Chapter 11, "Working with Arrays"), change properties of a program's controls, and do a set of tasks for as long as a certain condition exists, or skip a set of tasks until a certain condition is met.
Reading collections with loops
Visual Basic 6.0 supports other looping commands, including collection loops that use the For Each...Next statement. For more information about collections, see Chapter 26, "Making Object-Oriented Programs with Visual Basic."
Visual Basic supports two commonly used types of loops: counter loops, which perform a task a set number of times, and conditional loops, which perform a task while a specified condition exists or until a specified condition exists.
Using For...Next Loops
Counter loops are also known as For or For...Next loops. They're called For...Next loops because the beginning and end of the loop are defined by the For statement and the Next statement, respectively. The syntax of a For...Next loop is For CounterVar = StartNum To EndNum [Step StepNum] statements Next [CounterVar]
Keyword that starts a loop
User-defined numeric variable that the loop uses as a counter
Number from which the loop starts
Keyword that separates StartNum from EndNum
Number at which the loop stops
Optional keyword that indicates that loop should step
Indicates what size step increment/decrement should be (can be negative)
Keyword that completes the loop
Identifies which CounterVar is updated by the Next keyword
At the beginning of a For...Next loop, you define a counter variable, as well as the beginning and end points of the variable's value. The first time the loop is run, the counter variable is set to the value of the beginning point. Each time the program runs through the loop, the value of the counter increments. If the Step keyword is used, the counter variable increments as dictated by the number following the Step keyword. For example, in the following statement, intCntr will increment by 2: For intCntr = 0 To 10 Step 2
As the counter variable increments, it's checked against the value of the end point. If the counter is larger than the end point, the program skips out of the loop and onto the first statement following the loop.
If the beginning value of the loop is greater than the ending value, the loop won't execute at all, unless you set up the loop to count backward. For example, the following loop doesn't cause an error, but because the start boundary of the loop (9) is greater than the end boundary (0), the loop is ignored and the message box doesn't appear:
Stepping through the loop
The default or assumed value for the Step in a For...Next loop is a positive 1, which increments the counter. Including the step value on all For...Next loops, even when the value is 1, improves readability and aids maintenance.
Concatenation
You concatenate a string when you add another string onto a string. An example of concatenating three strings onto one string is FullName = First & " " & Last, which performs two successive concatenations into the string variable FullName.
For intCntr = 9 To 0 `This loop will not run! MsgBox "This is loop number: " & Cstr(intCntr) Next intCntr
The following loop works, however, because the loop uses the Step keyword to decrement >backward (-1) from 9 to 0: For intCntr = 9 To 0 Step -1 MsgBox "This is loop number: " & Cstr(intCntr) Next intCntr
The counter variable is changed each time the loop reaches the Next statement. Unless otherwise specified with the Step keyword, the counter is increased by one for each loop.
Listing 10.1, the event handler for the For...Next button click event, shows a For...Next loop that exposes the counter variable within a larger string displayed in a TextBox (see Figure 10.1). As the loop progresses, the counter variable is converted to a string and inserted within strings, StrBegin and StrEnd. Those strings are concatenated onto a master "holding" string, StrMsg, which grows with each trip through the loop. A carriage return and line-break character is added to StrMsg at the end of the loop, and then the StrMsg is assigned to the Text property of txtDisplay. The loop then refreshes the form to repaint the TextBox.
LISTING 10.1 10LIST01.TXT--Using a For...Next Loop to Concatenate Strings 01 Private Sub cmdForNext_Click() 02 Dim intCntr As Integer 03 Dim strBegin As String 04 Dim strEnd As String 05 Dim strMsg As String 06 07 `Make a phrase for the beginning of a line 08 strBegin = "This is line: " 09 10 `Make a phrase for the end of a line 11 strEnd = " of a For...Next loop" 12 13 `Make a For...Next Loop 14 For intCntr = 0 To 20 15 16 `Put the beginning of the line in place 17 strMsg = strMsg & strBegin 18 19 `Convert the counter integer to a string 20 `and place it in the middle of the string 21 `that is being constructed 22 strMsg = strMsg & CStr(intCntr) 23 24 `Add the end of the message 25 strMsg = strMsg & strEnd 26 27 `Put in a line break constant 28 strMsg = strMsg & vbCrLf 29 `Display the resulting string in the textbox 30 txtDisplay.Text = strMsg 31 Refresh `force a repaint of the screen 32 Next intCntr 33 End Sub
Documenting the end of a For...Loop
For ease of reading your program code, it's good practice to include the variable name in the Next statement (like on line 32 of Listing 10.1). This is especially important in nested loops.
FIGURE 10.1 When you set a TextBox's MultiLine property to True, make sure that you set the TextBox's ScrollBars property accordingly. Otherwise, you may not see all the text.
vbCrLf is a Visual Basic global constant that could have been substituted with the string "Chr(13) & Chr(10)". Both the VBA constant and this literal string are equivalent to a carriage return/line feed combination. For more information about using VBA constants, see Chapter 7, "Using Data Types, Constants, and Variables."
An alternative method would be to only update the screen after leaving the loop. You would move the assignment statement immediately after the Next command to prevent the program from displaying any text until the loop is completed. In the case of a long-running process, you should give users a visual indication that the program is running. In long-running loops, if you wait to the end of the loop to display the results onscreen, your users may think the program has locked up.
Although you can use any numeric variable for the counter, you need to be aware of the limits of each variable type. For example, trying to run a loop 40,000 times starting from 0 with a step of 1 and an integer variable as the CounterVar causes an overrun error during execution. This is because an integer has a maximum positive value of 32,767. Using a Long integer will solve this problem when larger loop counters are required. The limitation on Long integers is negative 2,147,483,648 to positive 2,147,483,647.
Using floating-point variables such as Singles and Doubles for a CounterVar will work. There's even the capability to support fractional Step variables, although these can become difficult to maintain and should be avoided unless required by the functionality of the statements inside the loop.
You should exercise great care when manually changing the value of the counter variable inside the loop. Never reset the value of the counter variable inside a For...Next loop, because doing so may create an infinite loop or a program lockup. For example, the following code causes the For...Loop to go into an infinite loop, because the end value of 4 is never reached: For intCntr = 0 to 4 intCntr = 0 ` This line causes an infinite loop Next intCntr
Terminating the For...Next Loop Early
Typically, your For...Next loop should run through all the values of the counter variable, but sometimes you'll want the loop to terminate early. To do this, simply place an Exit For statement where you want the loop to stop. This statement is typically associated within an If...Then statement.
Listing 10.2 shows an enhancement to the code in Listing 10.1. Lines 16 through 22 show an added If...Then statement at the beginning of the For...Next loop. When the loop begins, the code enters the If...Then statement to take a look at the value of the CheckBox on the form. Selecting the CheckBox on the form tells the program that the loop needs to terminate when the value of the counting variable exceeds the quantity 10 (see Figure 10.2). Therefore, if this condition exists (the CheckBox is checked), the Exit For statement--contained in the If...Then statement within the first If...Then statement--forces the program out of the For...Next loop.
LISTING 10.2 10LIST02.TXT--Setting Many Alternative Actions Within a
For...Next Loop 01 Private Sub cmdForNext_Click() 02 Dim intCntr As Integer 03 Dim strBegin As String 04 Dim strEnd As String 05 Dim strMsg As String 06 07 `Make a phrase for the beginning of a line 08 strBegin = "This is line: " 09 10 `Make a phrase for the end of a line 11 strEnd = " of a For...Next loop" 12 13 `Make a For...Next Loop 14 For intCntr = 0 To 20 15 16 `Take a look to see if the checkbox 17 `on the form is checked 18 If chkLimit.Value = 1 Then 19 `if it is, then Exit the For statement 20 `when intCntr is greater than 10 21 If intCntr > 10 Then Exit For 22 End If 23 `Put the beginning of the line in place 24 strMsg = strMsg & strBegin 25 26 `Convert the counter integer to a string 27 `and place it in the middle of the string 28 `that is being constructed 29 strMsg = strMsg & CStr(intCntr) 30 31 `Add the end of the message 32 strMsg = strMsg & strEnd 33 34 `Put in a line break constant 35 strMsg = strMsg & vbCrLf 36 `Display the resulting string in the textbox 37 txtDisplay.Text = strMsg 38 Refresh `force a repaint of the screen 39 Next intCntr 40 End Sub
FIGURE 10.2 CheckBoxes are a good means of control when you need to set or clear a condition.
Using Do...Loop Statements
A Do...Loop is a conditional loop, which is active as long or until a condition exists. The key feature of a conditional loop is, of course, the condition (any expression that can return a True or False value). This True/False can be a return value from a function; the value of a property, such as the Value property of an OptionButton; or an expression, such as NumVal < 15. (Chapter 18, "Writing Reusable Code with Subs and Functions," discusses functions fully.) The two basic types of conditional loops are Do...While, which repeats while the condition is True, and Do...Until, which repeats until the condition is True.
Using Do...While Loops
A Do...While loop works pretty much as the name implies--it does something while a certain condition is true. For example, to keep adding one to MyNum while MyNum is less than 20, you would use this: Do While MyNum <20 MyNum = MyNum + 1 Loop
The keyword While in the Do...While statement tells the program that the loop will be repeated while the condition expression is true. When the condition in a Do...While loop becomes false, the program moves out of the loop and onto the next statement after the Loop statement.
The syntax for a Do...While loop is Do While condition statement(s) Loop
The keyword that denotes the beginning of the loop
The keyword that denotes what kind of loop it is
The state that must exist (for example, x = 10, MyVal <> True, or y < x)
The statement(s) to execute while the loop is active
The keyword that indicates the end of the loop block
Both statements must be present
Or you can use this equally valid syntax: Do statement(s) Loop While condition
As you can see in this example, the Do...While loop has two forms. The difference between the two is the placement of the condition--at the beginning of the loop or at the end.
Listing 10.3 looks similar to the For...Next loop code in Listing 10.2. When you take a closer look at Lines 13 through 41, however, you'll notice that where there was a For...Next loop, there's now a Do...While loop that runs while the value of intCntr is less than or equal to 20 (see Figure 10.3). Line 21 has also been changed from Exit For to Exit Do because the loop is now change to a Do loop. The Exit Do command is used with both types of Do loops.
Do loops will always run once if tested at the end
When the condition statement is placed after the Loop keyword in a Do...While or Do...Until loop, the set of instructions contained between Do and Loop is always executed once. The condition is checked only after the first iteration of the loop.
LISTING 10.3 10LIST03.TXT--A Do...While Loop That Does the Same Thing as a For...Next Loop 01 Private Sub cmdDoWhile_Click() 02 Dim intCntr As Integer 03 Dim strBegin As String 04 Dim strEnd As String 05 Dim strMsg As String 06 07 `Make a phrase for the beginning of a line 08 strBegin = "This is line: " 09 10 `Make a phrase for the end of a line 11 strEnd = " of a Do...While loop" 12 13 `Make a Do..While Loop 14 Do While intCntr <= 20 15 16 `Take a look to see if the checkbox 17 `on the form is checked 18 If chkLimit.Value = 1 Then 19 `if it is, then Exit the Do statement 21 If intCntr > 10 Then Exit Do 22 End If 23 24 `Put the beginning of the line in place 25 strMsg = strMsg & strBegin 26 27 `Convert the counter integer to a string 28 `and place it in the middle of the string 29 `that is being constructed 30 strMsg = strMsg & CStr(intCntr) 31 32 `Add the end of the message 33 strMsg = strMsg & strEnd 34 35 `Put in a line break constant 36 strMsg = strMsg & vbCrLf 37 `Display the resulting string in the textbox 38 txtDisplay.Text = strMsg 39 Refresh `force a repaint of the screen 40 intCntr = intCntr + 1 41 Loop 42 End Sub
FIGURE 10.3 You can us Do...While to loop while intCntr is less than or equal to 20.
By placing the While condition clause in the Do statement, you tell the program that you want to evaluate the condition before you run any statements inside the loop. If the condition is True, the repetitive statements between the Do statement and the Loop statement are run. Then the program returns to the Do statement to evaluate the condition again. As soon as the condition is False, the program moves to the statement following the Loop statement.
Put conditional clauses in only one place
Don't put the While condition clause in both the Do and Loop statements because this causes an error when you try to run your program.
When the form of the loop is located with the While keyword right after the Do keyword, the statements inside the loop may never be run. If the condition is False before the loop is run the first time, the program just proceeds to the statements after the loop. To run the Do...While loop at least once, use the second form of the Do...While loop, which places the condition after the Loop statement. For example, Do Text1.Text = Text1.Text & CStr(intCntr ) intCntr = intCntr + 1 Loop While intCntr < 10
This form of the Do...Loop tells the program that you want the loop to run at least once and then evaluate the condition to determine whether to repeat the loop.
Replace While...Wend loops with Do...While
If you're working on code developed by someone else, you may find a loop that starts with a While statement and ends with a Wend statement. This type of loop works the same as a Do...While loop with the While clause in the Do statement. Visual Basic still supports a While...Wend loop, but it's recommended to that you use the Do...While type of loop because it's more flexible.
When using a Do...While loop, don't forget to increment the conditional variable in your code. Unlike the For...Next loop, the conditional or counting variables in Do loops are your responsibility to manage. Also, it's good practice to initialize the loop variable rather than rely on the default. For example, the following goes on infinitely because the While condition is never changed: Dim intCntr
intCntr = 0 txtMessage.Text = "" Do While intCntr < 10 txtMessage.Text = txtMessage.Text _ & CStr(intCntr) Loop
The following code loops, terminates, and displays the TextBox's text just fine: Dim intCntr intCntr = 0 txtMessage.Text = "" Do While intCntr < 10 txtMessage.Text = txtMessage.Text _ & CStr(intCntr) intCntr = intCntr + 1 Loop
Using Do...Until Statements
The Do...Until loop is basically the same as the Do...While loop, except that the statements inside a Do...Until loop are run only as long as the condition is false--in other words, as long as the condition isn't met. When the condition becomes true, the loop terminates.
The syntax for a Do...Until loop is Do Until condition statement(s) Loop
There are two forms of syntax for the Do...Until loop, just as there are two forms of syntax for the Do...While loop--one with the condition in the Do statement and one with the condition in the Loop statement. If you place the condition in the same line as the Do keyword, the condition is evaluated before the statements in the loop are executed. If you place the condition in the same line as the Loop keyword, the loop is run at least once before the condition is evaluated.
Listing 10.4 shows the event procedure for the cmdDoUntil button's Click event (see Figure 10.4). A Do...Until loop within the event procedure runs until the counter variable, intCntr, is greater than 20, or the Stop at 10 CheckBox is selected. This code is similar to the code for the cmdDoWhile_Click() event procedure in Listing 10.3. The only difference is that the Do...While loop is changed to a Do...Until loop (Lines 13 through 41). Also, the StrEnd string on Line 11 is changed from " of a Do...While loop" to " of a Do...Until loop". As you can see, Do...While loops and Do...Until loops are closely related. Generally, which one you use is a matter of personal style.
LISTING 10.4 10LIST04.TXT--Terminating the Loop When the Condition Is True 01 Private Sub cmdDoUntil_Click() 02 Dim intCntr As Integer 03 Dim strBegin As String 04 Dim strEnd As String 05 Dim strMsg As String 06 07 `Make a phrase for the beginning of a line 08 strBegin = "This is line: " 09 10 `Make a phrase for the end of a line 11 strEnd = " of a Do...Until loop" 12 13 `Make a Do..Until Loop 14 Do Until intCntr > 20 ... 41 Loop 42 End Sub
FIGURE 10.4 The Do...Until code still checks the value of the CheckBox.
Breaking an Infinite Loop
Terminate an infinite loop
If your program is running within the Visual Basic IDE, press Ctrl+Break. When you press these keys, the loop breaks at a line of code within the infinite loop. If your program isn't running within the IDE, the only way to terminate it is to end the program from within the Windows Task Manager. (For detailed information about the Task Manager, read the online documentation that comes with Windows.)
Getting comfortable using loops takes time. The more you use them, the better you'll get at identifying situations in which to use them. Loops can be a powerful programming tool, but they can cause enormous headaches if not properly written.
The following shows a programmer's worst nightmare--the infinite loop: Dim intCntr as Integer Do While intCntr = 0 intCntr = 0 Loop
The command inside the loop sets the conditional variables and prevents the program from going beyond loop. So the code tells the program to loop forever. When the program enters this loop, it's locked into the loop; everything else waits for the loop to complete, which never happens. If you run this program within the Visual Basic IDE, be aware that infinite loops will often lock it up and you may lose any program changes you made since your last update.
The best way to avoid an infinite loop is to carefully double-check any code you write that changes the conditional or counter variables in your loops. This may seem simplistic, but the power of loops is also the danger of loops. A loop can just as quickly tie up your system doing a desired productive task as it can counting to 2 billion for no reason.
Nesting Loops
In some situations, it becomes important to repeat a subtask several times during a larger task that's also being repeated. The way you write this type of program is by including a loop inside the set of instructions being repeated by a loop. This loop-within-a-loop structure is called nesting, or a nested loop. Nested loops can be a powerful programming tool, but they're confusing to understand if they're not properly written and documented.
Working with Multiple Loops
Listing 10.5 shows an example of multiple loops that change the Background Color property of the vpjLoops form and CheckBox during runtime. The main form and the CheckBox are changed to include all the visible background on the frame. This sample program is run from the Simple Color command button (refer to Figure 10.4).
Download this code
The code for this example is included in the prjLoops project. To use the code without having to type it all in, you can download this project from the Web at http://www. mcp.com/info. You'll be asked to enter an ISBN; enter 078971633x, and then click the Search button to go to the Book Info page for Using VB6.
LISTING 10.5 10LIST05.TXT--An Example of Multiple For Loops 01 Private Sub cmdForColor_Click() 02 Dim lngBlu As Long 03 Dim lngGrn As Long 04 Dim lngRed As Long 05 Dim SaveBack As Long 06 `Assign intColor Variables to initial values 07 `This will display white because all colors are 255 08 lngBlu = 255 09 lngGrn = 255 10 lngRed = 255 11 12 `Save the current background color of the form 13 SaveBack = frmLoop.BackColor 14 `Both the Form and CheckBox background are the same 15 16 `Loop 255 times reducing the Red color by 1 17 For lngRed = 255 To 1 Step -1 18 `Set the background color of the form and CheckBox 19 frmLoop.BackColor = RGB(lngRed, lngGrn, lngBlu) 20 chkLimit.BackColor = frmLoop.BackColor 21 Refresh `force a repaint of the screen 22 Next lngRed 23 24 `Loop 255 times reducing the Green color by 1 25 For lngGrn = 255 To 1 Step -1 26 `Set the background color of the form and CheckBox 27 frmLoop.BackColor = RGB(lngRed, lngGrn, lngBlu) 28 chkLimit.BackColor = frmLoop.BackColor 29 Refresh `force a repaint of the screen 30 Next lngGrn 31 32 `Loop 255 times reducing the Blue color by 1 33 For lngBlu = 255 To 1 Step -1 34 `Set the background color of the form and CheckBox 35 frmLoop.BackColor = RGB(lngRed, lngGrn, lngBlu) 36 chkLimit.BackColor = frmLoop.BackColor 37 Refresh `force a repaint of the screen 38 Next lngBlu 39 40 `Loop 255 times increasing the Red color by 1 41 For lngRed = 0 To 254 Step 1 42 `Set the background color of the form and CheckBox 43 frmLoop.BackColor = RGB(lngRed, lngGrn, lngBlu) 44 chkLimit.BackColor = frmLoop.BackColor 45 Refresh `force a repaint of the screen 46 Next lngRed 47 48 `Loop 255 times increasing the Green color by 1 49 For lngGrn = 0 To 254 Step 1 50 `Set the background color of the form and CheckBox 51 frmLoop.BackColor = RGB(lngRed, lngGrn, lngBlu) 52 chkLimit.BackColor = frmLoop.BackColor 53 Refresh `force a repaint of the screen 54 Next lngGrn 55 56 `Loop 255 times increasing the Blue color by 1 57 For lngBlu = 0 To 254 Step 1 58 `Set the background color of the form and CheckBox 59 frmLoop.BackColor = RGB(lngRed, lngGrn, lngBlu) 60 chkLimit.BackColor = frmLoop.BackColor 61 Refresh `force a repaint of the screen 62 Next lngBlu 63 64 `Reset the background color on both to original 65 frmLoop.BackColor = SaveBack 66 chkLimit.BackColor = frmLoop.BackColor 67 End Sub
Make code easier to read
Indenting code nested inside a loop or other structure (such as an If statement) makes the code easier to read.
The program first saves the current background color and then proceeds to remove each of the three screen colors through a set of 255 increments. The first color removed is red, then green, and finally blue. The second set of three loops does just the opposite: first it adds in the red, then green, and finally blue. The last two lines (65 and 66) put both elements back to their original background color settings.
There are six separate loops for 67 total lines of code. These loops have many lines in common, and with a little effort, you can identify those common elements.
Using Nested Loops to Eliminate Loops
You'll notice the use of a long integer array to accomplish the function of the inner loop. Arrays are covered in depth in Chapter 11, "Working with Arrays."
Listing 10.6 shows Listing 10.5 rewritten as two nested loops. The first loop dims the background color by color. The second, as in Listing 10.5, adds the colors back in. This sample program is run from the Nested Color command button (see Figure 10.5).
Download this code
The code for this example is also included in the prjLoops project, which you can download from http://www.mcp.com/ info.
LISTING 10.6 10LIST06.TXT--An Wxample of Two Nested Loops That Use a Long Integer Array 01 Private Sub cmdNestColor_Click() 02 Dim intColorArray(1 To 3) As Long 03 Dim SaveBack As Long 04 Dim intPass As Integer 05 Dim intHue As Integer 06 `Assign intColor Variables to initial values 07 `This will display white because all colors are 255 08 intColorArray(1) = 255 `Red 09 intColorArray(2) = 255 `Green 10 intColorArray(3) = 255 `Blue 11 12 `Save the current background color of the form 13 SaveBack = frmLoop.BackColor 14 `Both the Form and CheckBox background are the same 15 16 `Top Loop does each color in turn Red, Green, Blue 17 For intPass = 1 To 3 18 `Then Loop2 255 times reducing color by 1 19 For intHue = 255 To 1 Step -1 20 intColorArray(intPass) = intHue 21 frmLoop.BackColor = RGB(intColorArray(1), _ intColorArray(2), intColorArray(3)) 22 chkLimit.BackColor = frmLoop.BackColor 23 Refresh `force a repaint of the screen 24 Next intHue 25 Next intPass 26 27 `Top Loop does each color in turn Red, Green, Blue 28 For intPass = 1 To 3 29 `Then Loop2 255 times increasing color by 1 30 For intHue = 0 To 254 Step 1 31 intColorArray(intPass) = intHue 32 frmLoop.BackColor = RGB(intColorArray(1), _ intColorArray(2), intColorArray(3)) 33 chkLimit.BackColor = frmLoop.BackColor 34 Refresh `force a repaint of the screen 35 Next intHue 36 Next intPass 37 38 `Reset the background color on both to original 39 frmLoop.BackColor = SaveBack 40 chkLimit.BackColor = frmLoop.BackColor 41 End Sub
There are only 41 lines of code and only two nested loops to maintain in this new version of the program. This reduces line count by 35 percent with no loss of functionality. The key benefits that nested loops provide are consistent operation and ease of maintenance. If the same code is being used every time a loop is run, it becomes easy to make sure that it does the same tasks.
Nested loops require more effort to write and debug than simple loops. Reusable code is harder to develop than redundant code by at least a factor of 2. This topic is covered more in Chapter 18, "Writing Reusable Code with Subs and Functions."
Visual Basic's IDE makes it very tempting to write a simple loop and then cut and paste it six, seven, or 200 times in a row. Most programming tools make it very easy to copy lines of code. The challenge comes in learning to write better programs that are easier to maintain and that perform consistently.
Loading Data with Nested Loops
There are more useful examples where nested loops are invaluable. The final example in this chapter is based on the MSFlexGrid ActiveX control and how it could be loaded from a text file by using a nested loop. In this example, the control isn't bound to any data source. The example program uses a simple text file that contains one field or cell per row. The nested loop is a big help here, as it can be used to step through the rows in the grid column by column. This allows the read or input statement to appear only once in the program.
Loading the MSFlxGrd.ocx file
An enhanced version of MSFlexGrid comes with VB6. Before you can use the MSFlexGrid control in your application, you must add the MSFlxGrd.ocx file to your Microsoft Windows System directory. For more information about adding an ActiveX control to a project, see Chapter 4, "Using the Intrinsic Controls."
Listing 10.7 lists the setup commands for the MSFlexGrid control that are executed when the form loads. These commands set the width of the four columns used in the example. (The details of this are covered in Chapter 32, "Enhancing Your Programs Using the Advanced Data Controls.")
LISTING 10.7 10LIST07.TXT--Parameters for the MSFlexGrid Control 01 Private Sub Form_Load() 02 `These values are in TWIPS and set the columns on the 03 `MSFlexGrid at run time. If you change these values 04 `you may need to also readjust the size of the Control. 05 MSFlexGrid1.ColWidth(0) = 640 06 MSFlexGrid1.ColWidth(1) = 1280 07 MSFlexGrid1.ColWidth(2) = 1280 08 MSFlexGrid1.ColWidth(3) = 1440 09 End Sub
By default, the Load event fires when a form is displayed. Figure 10.5 shows the grid after it has been loaded. By using this event, the program adjusts the width of the four columns to values that fit the amount of data in each column. These were identified by trial and error, but a routine could have been written to inspect the data in each grid cell and then determine the right width for each column. This function would work like the Formatmenu's Column and Autofit command in Microsoft Excel.
FIGURE 10.5 When you load the MSFlexGrid ActiveX control, you can specify runtime properties.
Now that the control is set up, the actual program is straightforward. An ASCII text file contains 24 lines or rows. Each line is read in turn and loaded into the grid. Listing 10.8 shows a partial listing of the text file; the full listing can be downloaded from the Web site, along with the sample program and the actual IMPORT.TXT file.
LISTING 10.8 10LIST08.TXT--Part of IMPORT.TXT, Which is Read by the cmdNextData Subroutine 01 StoreID 02 Location 03 Manager 04 Sales 05 1566 06 Minneapolis 07 James Archie 08 "$14,187,944" ... 21 1032 22 New York 23 Elmer Doren 24 "$5,437,104"
IMPORT.TXT is an ASCII file
The IMPORT.TXT file was created with Microsoft Notepad and can be edited. The name of the file, its location, and the number of rows is part of the program statements and must be changed to read different filenames.
Rows 1 through 4 of Listing 10.8 are used as the column headings in the grid control. There's nothing special about these rows, other than they are first, and the background and font in the MSFlexGrid control was formatted. Rows 5 though 24 are the detailed records for each of five stores, including the store ID, location, manager's name, and total sales.
The program in Listing 10.9 shows the 39 lines of code required to accomplish the task of loading the grid. Lines 21 through 34 are the nested loop that reads each line from the file and moves it into the proper cell of the grid.
LISTING 10.9 10LIST09.TXT--Data Read Into Control a MSFlexGrid Control From a Txt File with a Nested Loop 01 Private Sub cmdNestData_Click() 02 Dim strBuff As String 03 Dim intFile As Integer 04 Dim intCol As Integer 05 Dim intRow As Integer 06 07 `This message informs the user of the runtime path 08 `where the IMPORT.TXT file must be located. If 09 `the file is not found a run-time error will result. 10 11 MsgBox ("The current system path is set to: " _ & CurDir & vbCrLf & "The program will run only" _ & " if: IMPORT.TXT is found at the current path") 12 13 ` The next avaialble file is assigned to intFile 14 ` and file is opened for input. 15 intFile = FreeFile 16 Open CurDir & "\IMPORT.TXT" For Input As #intFile 17 18 `The MSFlexGrid was set-up for 6 rows and 4 columns 19 ` Zero(0) is the default first value 20 21 `First Loop steps through each row 22 For intRow = 0 To 5 23 ` Second Loop steps through each column 24 For intCol = 0 To 3 25 `Reads file #intFile into variable strBuff 26 Input #intFile, strBuff 27 `Set pointer to proper column 28 MSFlexGrid1.Col = intCol 29 `Set pointer tp proper row 30 MSFlexGrid1.Row = intRow 31 `Assigns the read value to the Cell text 32 MSFlexGrid1.Text = strBuff 33 Next intCol 34 Next intRow 35 36 Close #intFile `close files as soon as you can 37 End Sub
Lines 7 through 18 in Listing 10.9 are for finding and opening the text file for input. The MessageBox displays the current default path of the local user's system. This is important because the default path is used with the file handler number given by the FileFree function to locate and open IMPORT.TXT. If the file isn't at this location, an error message will occur.
When the Click event on the Nested Data Read command button is fired, IMPORT.TXT is read row by row. Figure 10.6 show the grid control once the data has been read and loaded into the cells.
You May Also Find These Documents Helpful
-
Microsoft visual Basic 6.0 is a third-Generation event-driven programming language and integrated (IDE) from Microsoft for its COM programming model first released in 1991.…
- 1214 Words
- 5 Pages
Powerful Essays -
1. A while loop will not execute the body of the code if you have your test condition incorrect.…
- 3740 Words
- 15 Pages
Satisfactory Essays -
2. What type of instruction is used to execute different code based on a condition? -Jumps…
- 239 Words
- 2 Pages
Satisfactory Essays -
3. What happens when you give the following commands if the file named done already exists?…
- 2815 Words
- 12 Pages
Powerful Essays -
The process of how the subroutine works within an operating environment is as follows. While the main part of the program is running, there will be a call command sent to the CPU to tell it to stop wherever the program is at, store the W, Z and status along with where in the main program the CPU is at in the stack. Then the main program is cleared from memory and the subroutine is loaded for the CPU to start processing the code. Once the subroutine has finished, the subroutine code is deleted from memory and the CPU will load up the main program back on and will retrieve from stack the stored information (W, Z and status) which will allow the CPU know exactly where it needs to start from.…
- 645 Words
- 3 Pages
Good Essays -
5. What three actions do count-controlled loops typically perform using the counter variable? Initialization: Before the loop begins, the counter variable is initialized to a starting value. Test : The loop tests the counter variable by comparing it to a maximum value. If the counter variable is less than or equal to the maximum, the loop iterates. If the counter is greater than the maximum value, the program exits the loop. Increment : To increment a variable means to increase its value. During each iteration, the loop increments the counter variable by adding a predetermined amount to it…
- 431 Words
- 2 Pages
Satisfactory Essays -
The shell tries to find an executable program with the same name as the command. When it does, the shell executes the program. When finishing the command put an & ampersand symbol at the end of the command and the command will run in the background. Then a cursor will be displayed immediately to allow the next command to be entered.…
- 1031 Words
- 5 Pages
Powerful Essays -
4. The AND operator connects two Boolean expressions into one compound expression. Both must be true for the compound expression to be true.…
- 423 Words
- 3 Pages
Satisfactory Essays -
2. A pretest loop is a loop tests the conditions before performing the iteration. A posttest loop performs the iteration then test the condition.…
- 261 Words
- 2 Pages
Satisfactory Essays -
If two steps are indented at the same level, the uppermost statement is executed first. ans)All of the above…
- 4209 Words
- 17 Pages
Powerful Essays -
7. Write a pseudocode statement that assigns the value of 27 to the variable count.…
- 313 Words
- 2 Pages
Satisfactory Essays -
8.) A nested if statement is used to ask multiple questions before deciding what processing path to take.…
- 754 Words
- 6 Pages
Satisfactory Essays -
The program will terminate because it is expecting an integer value and a “Wingding” will cause an error in the program which will cause it to crash.…
- 469 Words
- 2 Pages
Satisfactory Essays -
Two of the greatest civilizations of the ancient world were the civilizations of Greece and Rome. These two civilizations were especially significant from the time periods prior to the Roman Republic. These two civilizations both affected Europe and the Mediterranean regions including the rest of the world around them. Though the two civilizations differed in location, they also had many aspects that were very similar. One of these includes the government, with both showing the beginnings of the representative government. Another aspect between these civilizations was the military with similar strategies, tactics, and organizational similarities. Finally, the last similarity between the two ancient cities of Greece and Rome was the religion. Both civilizations worshipped multiple deities and constructed grandiose shrines and temples in their honor to show their dedication. Though there were differences between the two societies the similarities between the two were far more important.…
- 2141 Words
- 9 Pages
Good Essays -
It can be summed up that these common stereotypical roles marginalized Latina women. The power of this medium could create more positive roles if only given a true opportunity. The film is not just an escape from reality, it has the power to send messages to audiences everywhere. To a certain degree, U.S. government understood this power and made a good attempt at depicting Latin Americans in a better light, even if their reasons behind weren’t to inspire Latinos of aiming higher in life. All the beautiful women discussed in this paper have experienced the same obstacles, accepting to play these stereotypical roles in hopes they can attain the fame required to gain power and respect in Hollywood.…
- 316 Words
- 2 Pages
Satisfactory Essays