Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 to 1 Tested against the stop value, 5 Incremented by 1 at the Next statement

2 Chapter 6 - VB 2005 by Schneider2 For…Next Loop Syntax

3 Uthaisombut & Schneider3 Set control variable to initial value Execute Statements in loop body Increment control variable Is control variable > terminating value? Execute statements following the loop Yes No

4 Chapter 6 - VB 2005 by Schneider4 Example 2 For index = 0 To n Step s lstValues.Items.Add(index) Next Control variable Start value Stop value Amount to add to index

5 Chapter 6 - VB 2005 by Schneider5 Example with Negative Step Dim j As Integer For j = 10 To 1 Step -1 lstBox.Items.Add(j) Next lstBox.Items.Add("Blastoff")

6 Chapter 6 - VB 2005 by Schneider6 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

7 Chapter 6 - VB 2005 by Schneider7 Nested For…Next Loops

8 Chapter 6 - VB 2005 by Schneider8 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.

9 Chapter 6 - VB 2005 by Schneider9 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.

10 Loop in Action For i = 1 to 4 Picture1.print i * 2 Next Chapter 6 - VB 2005 by Schneider10

11 Loop in Action For i = 1 to 4 Picture1.print i * 2 Next Chapter 6 - VB 2005 by Schneider11 Value of i Picture1 Output

12 Loop in Action For i = 1 to 4 Picture1.print i * 2 Next Chapter 6 - VB 2005 by Schneider12 Value of i 1 Picture1 Output 2

13 Loop in Action For i = 1 to 4 Picture1.print i * 2 Next Chapter 6 - VB 2005 by Schneider13 Value of i 2 Picture1 Output 2 4

14 Loop in Action For i = 1 to 4 Picture1.print i * 2 Next Chapter 6 - VB 2005 by Schneider14 Value of i 3 Picture1 Output 2 4 6

15 Loop in Action For i = 1 to 4 Picture1.print i * 2 Next Chapter 6 - VB 2005 by Schneider15 Value of i 4 Picture1 Output 2 4 6 8

16 Loop in Action For i = 1 to 4 x = i mod 5 storeMe(i) Picture1.print i * 2 Next Chapter 6 - VB 2005 by Schneider16


Download ppt "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."

Similar presentations


Ads by Google