1 6.2 for…next loops general form of a for…next loop nested for…next loops local type...
TRANSCRIPT
![Page 1: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/1.jpg)
1
6.2 For…Next Loops
• General Form of a For…Next Loop
• Nested For…Next Loops
• Local Type Inference
![Page 2: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/2.jpg)
2
For…Next Loops
• Used when we know how many times we want the loop to execute
• A counter controlled loop
![Page 3: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/3.jpg)
3
For…Next Loop Syntax
![Page 4: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/4.jpg)
4
SampleFor i As Integer = 1 To 5
lstTable.Items.Add(i & " " & i ^ 2)
Next
The loop counter variable, i, is
• initialized to 1
• tested against the stop value, 5
• incremented by 1 at the Next statement
The loop counter variable is often used in statements within the loop body. In this case, displaying a sequence of numbers and their squares
![Page 5: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/5.jpg)
5
Similar Do While LoopDim i As Integer = 1
Do While i <= 5
lstTable.Items.Add(i & " " & i ^ 2)
i += 1
Loop
Loop counter initialization
Loop counter initialization
increment
![Page 6: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/6.jpg)
6
Example 1: Output
![Page 7: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/7.jpg)
7
Example 1: Code
Dim pop As Double = 300000
For yr As Integer = 2010 To 2014
lstTable.Items.Add(yr & " " & FormatNumber(pop, 0)) pop += 0.03 * pop
Next
![Page 8: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/8.jpg)
Step Clause• Normally after each pass the value of the
counter variable increases by 1
• If the clause Step s is appended to the For statement, the value of s will be added to the counter variable after each pass.
• If the value of s is a negative number, the value of the counter variable will decrease after each pass.
8
![Page 9: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/9.jpg)
9
Example with Negative Step Value
For j As Integer = 10 To 1 Step -1
lstBox.Items.Add(j)
Next
lstBox.Items.Add("Blastoff")
Looping backward through the numbers. Start at 10, test to see if j >= 1, decrement by 1.
Question: What would happen with Step -2?
![Page 10: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/10.jpg)
10
Example with Negative Step Value
For j As Integer = 1 To 10 Step -1
lstBox.Items.Add(j)
Next
lstBox.Items.Add("Blastoff")
Question: What would happen in this case?
![Page 11: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/11.jpg)
Example 2
11
This is a good example for testing out various values for the loop’s test and step increment/decrement.
![Page 12: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/12.jpg)
Example 3
12
Note: Here we have a separate function that is called by the event procedure.
Function call
Function declaration
Parameter passing
![Page 13: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/13.jpg)
Example 3
13
Here, the loop counter variable is used as an index into the string. It is common practice to use loops for retrieving or manipulating successive positions of a string or an array or a listbox’s collection.
![Page 14: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/14.jpg)
Example 3
14
Loop starts at the last character of the string, and goes backward until the first.
![Page 15: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/15.jpg)
15
Nested Loops
Loops can be nested inside other loops.
![Page 16: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/16.jpg)
Example 4
16
Outer loop will execute three times
Each time the outer loop executes, the inner loop will execute three times
Therefore, the inner loop executes a total of nine times
![Page 17: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/17.jpg)
17
Example: Nested For…Next Loops
For 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
Question 1: how many times will the outer loop execute?
Question 2: how many times (total) will the inner loop execute?
![Page 18: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/18.jpg)
Initialize outer loop counter
Initialize inner loop counter
Test to continue
outer loop
Test to continue
inner loop
Outer loop statement(s)
Outer loop statement(s)
Inner loop statement(s)
Increment outer loop counter
Increment inner loop counter
Statements preceding loop
Statements following loop
No
No
Yes
Yes
The Nested for Loop -- Flowchart
![Page 19: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/19.jpg)
Example 4
19
This example produces a multiplication table.
The inner and outer loop counters are used for doing the calculations and displaying the results.
![Page 20: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/20.jpg)
20
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 21: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/21.jpg)
21
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 22: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/22.jpg)
22
Altering the Counter Variable
• The value of the counter 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 23: 1 6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f335503460f94c50922/html5/thumbnails/23.jpg)
23
Non-Integer Step Values
• Can lead to round-off errors with the result that the loop is not executed the intended number of times.
• We will only use Integers for all values in the header.