Loops
Loops are essential in writing any real program
Used when you need to repeat a block of code multiple times
Different number of iterations Fixed number Conditional Infinite
Several types of loops provided For/next loop Do/while Do/until
Repeats a code block a fixed number of times
For <variable> = <start> To <end>
‘ Code here!
Next <variable>
Note: Variable name after “Next” isn’t needed Helpful with nested loops (below)
Dim anInt As Integer
For anInt = 1 To 10
MsgBox("Iteration: " & Str(anInt))
Next
What if you want to leave the loop before it has completed? Exit For
You can increment by a number different than 1 Use the “Step” command Steps can be negative!! Or not = 1
Dim anInt As Integer
For anInt = 10 To 0 Step -2 MsgBox("Iteration: " & Str(anInt)) Next
Can combine assignment and arithmetic operations in one statement Doesn’t do anything new
Just an easier way to write the statement
intTotal += 3
intTotal = intTotal + 3
These two are the same thing Just easier to code
Works with any math operator
Execute a code block while a stated condition is true
Dim myInt As Integer = 0
Do While myInt < 10
MsgBox(myInt)
myInt += 1
Loop
Loop is tested at top May have 0 executions!!
Can have a While/Do loop Test is at the end Must execute at least one time!!
Dim myInt As Integer = 0
Do
MsgBox(myInt)
myInt += 1
Loop While myInt < 10
Whenever coding a loop you must be VERY careful!!
What happens with the following loop? Dim myInt As Integer = 0
Do While myInt < 10 MsgBox("Hello") Loop
How do you fix this? What happens with this loop? Dim myInt As Integer = 1
Do While myInt <> 10 MsgBox(myInt) myInt += 2 Loop
Loop that executes until the condition is true
Do Until <condition>
‘ Statements here
Loop
This is tested at the top of the loop Executes 0 or more times
These loops are (clearly) very similar Which one you choose depends on the task
you wish to perform Choice of loop is often a matter of personal
style For loops are most common It is Critical that you pay attention to the
Boundary conditions. How many times does the loop execute? How does the loop start? How does the loop end?
Very common form of loop It is actually two loops
One within the other The inner loop executes one complete time for each time the outer loop
executes one
Dim intMins As Integer Dim intSecs As Integer Dim intTemp As Integer
For intMins = 0 To 59 txtMins.Text = intMins.ToString() For intSecs = 0 To 59 txtSecs.Text = intSecs.ToString() For intTemp = 0 To 100000 ‘ In a real program, use a timer control Next Next Next
Very useful control Each row contains data Multicolumn property allows you to set the
number of columns in the list box Add rows by using the Add property
Dim intTemp As Integer
For intTemp = 1 To 100 lboxTest.Items.Add(Str(intTemp)) Next
Other useful methods: Items.Clear, Items.Remove, ItemsRemoveAt
Similar to list boxes Same methods/properties Has an extra text box. You can either:
Select an item form the list Enter a new item in the text box
Combo box styles: Drop down combo box Simple combo box Dropdown list combo box