Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " "...

16
Chapter 6 - VB 2005 by Schneider 1 Sample Dim 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

Transcript of Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " "...

Page 1: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Chapter 6 - VB 2005 by Schneider 1

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 2: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Chapter 6 - VB 2005 by Schneider 2

For…Next Loop Syntax

Page 3: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Uthaisombut & Schneider 3

Set control variableto initial value

ExecuteStatementsin loop body

Incrementcontrol variable

Is control variable >terminating value?

Executestatements

following the loop

Yes

No

Page 4: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Chapter 6 - VB 2005 by Schneider 4

Example 2

For index = 0 To n Step s

lstValues.Items.Add(index)

Next

Controlvariable

Startvalue

Stopvalue

Amountto add to

index

Page 5: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Chapter 6 - VB 2005 by Schneider 5

Example with Negative StepDim j As Integer

For j = 10 To 1 Step -1

lstBox.Items.Add(j)

Next

lstBox.Items.Add("Blastoff")

Page 6: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Chapter 6 - VB 2005 by Schneider 6

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 7: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Chapter 6 - VB 2005 by Schneider 7

Nested For…Next Loops

Page 8: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Chapter 6 - VB 2005 by Schneider 8

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 9: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Chapter 6 - VB 2005 by Schneider 9

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 10: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Loop in Action

For i = 1 to 4

Picture1.print i * 2

Next

Chapter 6 - VB 2005 by Schneider 10

Page 11: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Loop in ActionFor i = 1 to 4

Picture1.print i * 2

Next

Chapter 6 - VB 2005 by Schneider 11

Value of i

Picture1 Output

Page 12: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Loop in ActionFor i = 1 to 4

Picture1.print i * 2

Next

Chapter 6 - VB 2005 by Schneider 12

Value of i

1

Picture1 Output

2

Page 13: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Loop in ActionFor i = 1 to 4

Picture1.print i * 2

Next

Chapter 6 - VB 2005 by Schneider 13

Value of i

2

Picture1 Output

2 4

Page 14: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Loop in ActionFor i = 1 to 4

Picture1.print i * 2

Next

Chapter 6 - VB 2005 by Schneider 14

Value of i

3

Picture1 Output

2 4 6

Page 15: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Loop in ActionFor i = 1 to 4

Picture1.print i * 2

Next

Chapter 6 - VB 2005 by Schneider 15

Value of i

4

Picture1 Output

2 4 6 8

Page 16: Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.

Loop in Action

For i = 1 to 4

x = i mod 5

storeMe(i)

Picture1.print i * 2

Next

Chapter 6 - VB 2005 by Schneider 16