1 / 13

CS 102

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.

Download Presentation

CS 102

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 102 Loops

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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!!

  7. 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

  8. 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

  9. 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

  10. 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?

  11. 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

  12. 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

  13. 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

More Related