130 likes | 170 Views
CS 102. Loops. Overview. 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. For Loop.
E N D
CS 102 Loops
Overview • 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
For Loop • 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
For Loop • 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
Compound Math Operators • 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
Do/While Loop • 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!!
Do/While Loop • 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
Infinite Loops • 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
Do/Until • 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
Choosing a Loop Style • 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?
Nested Loops • 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
List Boxes • 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
Combo Boxes • 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