13
Loops

CS 102

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: CS 102

Loops

Page 2: CS 102

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

Page 3: CS 102

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

Page 4: CS 102

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

Page 5: CS 102

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

Page 6: CS 102

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

Page 7: CS 102

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

Page 8: CS 102

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

Page 9: CS 102

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

Page 10: CS 102

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?

Page 11: CS 102

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

Page 12: CS 102

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

Page 13: CS 102

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