Week 7 - Loops
Week 7 - Loops
• Double-click the button to create its Click event handler and add the following
bolded code:
THE FOR…NEXT LOOP
Private Sub btnForNextLoop_Click(sender As Object, e As EventArgs) Handles btnForNextLoop.Click
'Declare variable
Dim intCount As Integer
'Clear the list
ClearList()
'Perform a loop
For intCount = 1 To 5
'Add the item to the list
lstData.Items.Add("I'm item " & intCount.ToString & " in the list!")
Next
End Sub
• Now create the following method:
Private Sub ClearList()
'Clear the list
lstData.Items.Clear()
THE FOR…NEXT LOOP
• Save and run the project and then click the For Next Loop button.
• You should see results like those in Figure 4.18.
• NOTE When you’re talking about loops, you use the term iteration.
• One iteration includes one movement from the For statement to the Next
statement.
USING THE STEP KEYWORD
• You don’t have to start your loop at 1; you can pick any value you want.
• Nor do you have to increment the control value by 1 on each iteration; again, you
can increment by any value you want.
• In this Try It Out, you learn about the flexibility of the Step keyword.
• Return to the Forms Designer for the Loops project.
• Add a Button control to your form.
• Set its Name property to btnForNextLoopWithStep and its Text property to For
Next Loop w/ Step.
• Double-click the button and add the following bolded code in the Click event
handler:
USING THE STEP KEYWORD
Private Sub btnForNextLoopWithStep_Click(sender As Object, e As EventArgs) Handles btnForNextLoopWithStep.Click
• Run the project and click the For Next Loop w/Step button.
• You will see results like those in Figure 4.19.
USING THE
STEP
KEYWORD
LOOPING BACKWARD
• By using a Step value that’s less than 0 (or a negative number), you can make the loop go
backward, rather than forward,
• In this example, you will make a loop go backward.
• Return to the Forms Designer and add another Button control to your form, setting its Name
property to btnBackwardsForNextLoop and its Text property to Backwards For Next Loop.
• Double-click the button and add the following bolded code in the Click event handler:
Private Sub btnBackwardsForNextLoop_Click(sender As Object, e As EventArgs) Handles btnBackwardsForNextLoop.Click
• Run the project and click the For Each Loop button.
• You should see a list of folders that are at the root of your C drive.
THE DO…LOOP LOOPS
• The other kind of loop you can use is one that keeps happening until a certain
condition is met.
• This is known as a Do…Loop, and there are a number of variations.
• The first one you’ll learn about is the Do Until…Loop. This kind of loop keeps going
until something happens.
• Using the Do Until…Loop:For this Try It Out, you’re going to use the random
number generator that’s built into the .NET Framework.
• To create a loop that will keep generating random numbers until it produces the
number 10.
• When you get the number 10, you’ll stop the loop.
• Return to the Forms Designer in the Loops project, add another Button control to
your form, and set its Name property to btnDoUntilLoop and its Text property to Do
Until Loop.
• Double-click the button and add the following bolded code to its Click event
USING THE DO UNTIL…LOOP
Private Sub btnDoUntilLoop_Click(sender As Object, e As EventArgs) Handles btnDoUntilLoop.Click
'Declare variables
Dim objRandom As New Random
Dim intRandomNumber As Integer = 0
'Clear the list
ClearList()
'Process the loop until intRandomNumber = 10
Do Until intRandomNumber = 10
'Get a random number between 0 and 24
intRandomNumber = objRandom.Next(25)
'Add the number to the list
lstData.Items.Add(intRandomNumber.ToString)
Loop
USING THE DO
UNTIL…LOOP
• Run the project and click the
Do Until Loop button.
• You’ll see results similar to
the results shown in Figure
4.21.
• Keep clicking the button.
The number of elements in
the list is different each
time.
DO WHILE…LOOP
• The conceptual opposite of a Do Until…Loop is a Do While…Loop.
• This kind of loop keeps iterating while a particular condition is True. Let’s see it in
action.
• In this Try It Out, you will use a Do While…Loop to continue while a random
number is less than 15.
• Return to the Forms Designer and add another Button control to your form.
• Set its Name property to btnDoWhileLoop and its Text property to Do While
Loop.
• Double-click the button and add the following bolded code to the Click event
handler:
DO WHILE…LOOP
Private Sub btnDoWhileLoop_Click(sender As Object, e As EventArgs) Handles btnDoWhileLoop.Click
'Declare variables
Dim objRandom As New Random
Dim intRandomNumber As Integer = 0
'Clear the list
ClearList()
'Process the loop while intRandomNumber < 15
Do While intRandomNumber < 15
'Get a random number between 0 and 24
intRandomNumber = objRandom.Next(25)
'Add the number to the list
lstData.Items.Add(intRandomNumber.ToString)
Loop
DO WHILE…
LOOP
• Run the project and
click the Do While
Loop button.
• You’ll see something
similar to the results
shown in Figure
4.22.
OTHER VERSIONS OF THE DO…LOOP
• It’s possible to put the Until or While statements after Loop, rather than after Do.
Consider these two loops:
• NOTE When you use Loop While or Loop Until, you are saying that you want the
loop to execute at least once, no matter what.
• In general, it’s best to stick with Do While and Do Until, rather than use Loop While
and Loop Until.
Do While intX < 3 (Pre test)
intX += 1
Loop
• And
Do (Post test)
intX += 1
NESTED LOOPS
• You might need to start a loop even if you’re already working through another
loop.
• This is known as nesting, and it’s similar in theory to the nesting demonstrated
when you looked at If statements.
• In this Try It Out, you’ll see how you can create and run through a loop, even if
you’re already working through another one.
• In the Forms Designer, add another Button control to your form and set its Name
property to btnNestedLoops and its Text property to Nested Loops.
• Double-click the button and add the following bolded code to its Click event
handler:
NESTED LOOPS
Private Sub btnNestedLoops_Click(sender As Object, e As EventArgs) Handles btnNestedLoops.Click
'Clear the list
ClearList()
'Process an outer loop
For intOuterLoop As Integer = 1 To 2
'Process a nested (inner) loop
For intInnerLoop As Integer = 1 To 3
lstData.Items.Add(intOuterLoop.ToString & ", " & intInnerLoop.ToString)
Next
Next
End Sub
NESTED LOOPS