chapter 6 - vb 2005 by schneider1 chapter 6 – repetition 6.1 do while and do until loops 6.2...

23
Chapter 6 - VB 2005 by Sc hneider 1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Upload: benjamin-briggs

Post on 01-Jan-2016

227 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

1

Chapter 6 – Repetition

• 6.1 Do While and Do Until Loops

• 6.2 Processing Lists of Data with Do

Loops

• 6.3 For...Next Loops

Page 2: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

2

6.1 Do Loops

• A loop is one of the most important structures in programming.

• Used to repeat a sequence of statements a number of times.

• The Do loop repeats a sequence of statements either as long as or until a certain condition is true.

Page 3: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

3

Do Loop Syntax

Do While condition

statement(s)

Loop

Condition is tested,If it is True,

the loop is run.If it is False,

the statements following the

Loop statementare executed.

These statements are inside the body of the loop and are run if the condition

above is True.

Page 4: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

4

Pseudocode and Flow Chart for a Do Loop

Page 5: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

5

Example 1Private Sub btnDisplay_Click(...) _ Handles btnDisplay.Click 'Display the numbers from 1 to 7 Dim num As Integer = 1 Do While num <= 7 lstNumbers.Items.Add(num) num = num + 1 'Add 1 to the value of num LoopEnd Sub

Page 6: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

6

Post Test Loop

Do

statement(s)

Loop Until condition

Loop is executed once and then the conditionis tested. If it is False, the loop is run again.

If it is True, the statements following the Loop statement are executed.

Page 7: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

7

Example 2

Private Sub btnDisplay_Click(...) _

Handles btnDisplay.Click

'Display the numbers from 1 to 7

Dim num As Integer = 1

Do

lstNumbers.Items.Add(num)

num = num + 1 'Add 1 to the value of num

Loop Until num > 7

End Sub

Page 8: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

8

Pseudocode and Flowchart for a Post-Test Loop

Page 9: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

9

6.3 For…Next Loops

• Declarations of Control Variables

• Nested For … Next Loops

Page 10: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

10

For…Next Loops

• Used when we know how many times we want the loop to execute

• A counter controlled loop

Page 11: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

11

SampleDim i As Integer

For i = 1 To 5

lstTable.Items.Add(i & " " & i ^ 2)

Next

The loop control variable, i, is • Initialized to 1• Tested against the stop value, 5• Incremented by 1 at the Next statement

Page 12: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

12

Do While equivalent

i = 1

Do While i <= 5

lstTable.Items.Add(i & " " & i ^ 2)

i += 1

Loop

Page 13: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

13

For…Next Loop Syntax

Page 14: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

14

Example 2

For index = 0 To n Step s

lstValues.Items.Add(index)

Next

Controlvariable

Startvalue

Stopvalue

Amountto add to

index

Page 15: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

15

Example with Negative Step

Dim j As Integer

For j = 10 To 1 Step -1

lstBox.Items.Add(j)

Next

lstBox.Items.Add("Blastoff")

Page 16: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

16

Declaration Inside For Statement

Dim i As Integer

For i = 1 To 5

lstTable.Items.Add(i & " " & i ^ 2)

Next

For i As Integer = 1 To 5

lstTable.Items.Add(i & " " & i ^ 2)

Next

Page 17: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

17

Nested For…Next Loops

Page 18: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

18

Example: Seats on an AirplaneFor i As Integer = 65 To 70

For j As Integer = 1 To 25

lstBox.Items.Add(Chr(i) & j)

Next

Next

OUTPUT: A1

A2

A3

:

Innerloop

Outerloop

Page 19: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

19

For and Next Pairs

• For and Next statements must be paired.

• If one is missing, the automatic syntax checker will complain with a wavy underline and a message such as

“A ‘For’ must be paired with a ‘Next’.”

Page 20: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

20

Start, Stop, and Step values

• Consider a loop beginning with

For i As Integer = m To n Step s.

• The loop will be executed exactly once if m equals n no matter what value s has.

• The loop will not be executed at all if m is greater than n and s is positive,

or if m is less than n and s is negative.

Page 21: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

21

Altering the Control Variable

• The value of the control variable should not be altered within the body of the loop.

• Doing so might cause the loop to repeat indefinitely or have an unpredictable number of repetitions.

Page 22: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

22

Non-integer Step Values

• Can lead to round-off errors with the result that the loop is not executed the intended number of times.

Page 23: Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops

Chapter 6 - VB 2005 by Schneider

23

Non-integer Step Example• A loop beginning with

For i As Double = 1 To 2 Step 0.1

will be executed only 10 times instead of the intended 11 times. It should be replaced with

For i As Double = _

1 To 2.01 Step 0.1