Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Do Loop The Do Loop statements have three different forms, as shown below: a) Do While condition Block of one or more

VB statements Loop b) Do Block of one or more VB statements Loop While condition c) Do Until condition Block of one or more VB statements Loop d) Do Block of one or more VB statements Loop Until condition

9.2 Exiting the Loop Sometime we need exit to exit a loop prematurely because of a certain condition is fulfilled. The syntax to use is known as Exit Do. You can examine Example 9.2 for its usage. Example 9.1 Do while counter <=1000 num.Text=counter counter =counter+1 Loop * The above example will keep on adding until counter >1000. The above example can be rewritten as Do num.Text=counter counter=counter+1 Loop until counter>1000 Example 9.2 Dim sum, n As Integer Private Sub Form_Activate() List1.AddItem "n" & vbTab & "sum" Do n=n+1 Sum = Sum + n List1.AddItem n & vbTab & Sum If n = 100 Then Exit Do End If Loop End Sub Explanation In the above example, we compute the summation of 1+2+3+4++100. In the design stage, you need to insert a ListBox into the form for displaying the output, named List1. The program uses the AddItem method to populate the ListBox. The statement List1.AddItem "n" & vbTab & "sum" will display the headings in the ListBox, where it uses the vbTab function to create a space between the headings n and sum. 9.3 For....Next Loop The format is: For counter=startNumber to endNumber (Step increment) One or more VB statements Next Please refer to example 9.3a,9.3b and 9.3 c for its usage.

Sometimes the user might want to get out from the loop before the whole repetitive process is executed, the command to use is Exit For. To exit a For.Next Loop, you can place the Exit For statement within the loop; and it is normally used together with the If..Then statement. Lets examine example 9.3 d. Example 9.3 a For counter=1 to 10 display.Text=counter Next Example 9.3 b For counter=1 to 1000 step 10 counter=counter+1 Next Example 9.3 c For counter=1000 to 5 step -5 counter=counter-10 Next *Notice that increment can be negative Example 9.3 d Private Sub Form_Activate( ) For n=1 to 10 If n>6 then Exit For End If Else Print n End If End Sub 9.4 The While.Wend Loop The structure of a While.Wend Loop is very similar to the Do Loop. it takes the following format: While condition Statements Wend The above loop means that while the condition is not met, the loop will go on. The loop will end when the condition is met. Lets examine the program listed in example 9.6 where it produces the same result as example 9.4 and example 9.5 Example 9.6 Dim sum, n As Integer Private Sub Form_Activate() List1.AddItem "n" & vbTab & "sum" While n <> 100 n=n+1 Sum = Sum + n List1.AddItem n & vbTab & Sum Wend End Sub -For counter [ As datatype ] = start To end [ Step step ] [ statements ] [ Continue For ] [ statements ] [ Exit For ] [ statements ] Next [ counter ] You use a For...Next structure when you want to repeat a set of statements a set number of times. In the following example, the index variable starts with a value of 1 and is incremented with each iteration of the loop, ending after the value of index reaches 5. VB For index As Integer = 1 To 5 Debug.Write(index.ToString & " ") Next Debug.WriteLine("")

' Output: 1 2 3 4 5 In the following example, the number variable starts at 2 and is reduced by 0.25 on each iteration of the loop, ending after the value of number reaches 0. The Step argument of -.25 reduces the value by 0.25 on each iteration of the loop. VB For number As Double = 2 To 0 Step -0.25 Debug.Write(number.ToString & " ") Next Debug.WriteLine("") ' Output: 2 1.75 1.5 1.25 1 0.75 0.5 0.25 0 Tip A While...End While Statement (Visual Basic) or Do...Loop Statement (Visual Basic) works well when you don't know in advance how many times to run the statements in the loop. However, when you expect to run the loop a specific number of times, a For...Next loop is a better choice. You determine the number of iterations when you first enter the loop. Nesting Loops You can nest For loops by putting one loop within another. The following example demonstrates nestedFor...Next structures that have different step values. The outer loop creates a string for every iteration of the loop. The inner loop decrements a loop counter variable for every iteration of the loop. VB For indexA = 1 To 3 ' Create a new StringBuilder, which is used ' to efficiently build strings. Dim sb As New System.Text.StringBuilder() ' Append to the StringBuilder every third number ' from 20 to 1 descending. For indexB = 20 To 1 Step -3 sb.Append(indexB.ToString) sb.Append(" ") Next indexB ' Display the line. Debug.WriteLine(sb.ToString) Next indexA ' Output: ' 20 17 14 11 8 5 2 ' 20 17 14 11 8 5 2 ' 20 17 14 11 8 5 2 When nesting loops, each loop must have a unique counter variable. You can also nest different kinds control structures within each other. For more information, see Nested Control Structures (Visual Basic). Exit For and Continue For The Exit For statement immediately exits the ForNext loop and transfers control to the statement that follows the Next statement. The Continue For statement transfers control immediately to the next iteration of the loop. For more information, see Continue Statement (Visual Basic). The following example illustrates the use of the Continue For and Exit For statements. VB For index As Integer = 1 To 100000 ' If index is between 5 and 7, continue ' with the next iteration. If index >= 5 And index <= 8 Then Continue For End If ' Display the index. Debug.Write(index.ToString & " ") ' If index is 10, exit the loop. If index = 10 Then Exit For End If Next Debug.WriteLine("") ' Output: 1 2 3 4 9 10 You can put any number of Exit For statements in a ForNext loop. When used within nested ForNextloops, Exit For exits the innermost loop and transfers control to the next higher level of nesting.

Exit For is often used after you evaluate some condition (for example, in an If...Then...Else structure). You might want to use Exit For for the following conditions: Continuing to iterate is unnecessary or impossible. An erroneous value or a termination request might create this condition. A Try...Catch...Finally statement catches an exception. You might use Exit For at the end of theFinally block. You have an endless loop, which is a loop that could run a large or even infinite number of times. If you detect such a condition, you can use Exit For to escape the loop. For more information, seeDo...Loop Statement (Visual Basic). 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

You might also like