Title: CS 4 Intro to Programming using Visual Basic
1CS 4 Intro to Programming using Visual Basic
- For Loops
- Patchrawat UthaisombutUniversity of Pittsburgh
based on lecture notes by D. Schneider
2Chapter 6 Repetition
- 6.1 Do Loops
- 6.2 Processing Lists of Data with Do
- Loops
- 6.3 For...Next Loops
- 6.4 A Case Study Analyze a Loan
36.3 ForNext Loops
- Declarations of Control Variables
- Nested For Next Loops
4ForNext Loops
- Used when we know how many times we want the loop
to execute - A counter controlled loop
5Sample
- 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
6Do While equivalent
- i 1
- Do While i lt 5
- lstTable.Items.Add(i " " i 2)
- i 1
- Loop
7ForNext Loop Syntax
8Set control variableto initial value
Is control variable gtterminating value?
Yes
No
ExecuteStatementsin loop body
Incrementcontrol variable
Executestatementsfollowing the loop
9Example 1 Output
10Example 1 Code
- Dim yr As Integer
- Dim pop as Double 300000
- Dim fmtStr As String _
- "0,41,12N0"
- For yr 2006 To 2010 lstPop.Items.Add(String.Form
at( _ - fmtStr, yr, pop)
- pop 0.03 pop
- Next
11Example 2
Start value
Control variable
Stop value
Amount to add to index
- For index 0 To n Step s
- lstValues.Items.Add(index)
- Next
12Example with Negative Step
- Dim j As Integer
- For j 10 To 1 Step -1
- lstBox.Items.Add(j)
- Next
- lstBox.Items.Add("Blastoff")
13Declaration 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
14Playing with For Loop
15Playing with For Loop
- Private Sub btnDisplay_Click() Handles
btnDisplay.Click - Dim n, s As Double
- Dim index As Double
- Â
- n CDbl(txtN.Text)
- s CDbl(txtStep.Text)
- Â
- lstDisplay.Items.Clear()
- Â
- For index 0 To n Step s
- lstDisplay.Items.Add(index)
- Next
- End Sub
16Nested ForNext Loops
17Example Seats on an Airplane
- 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
-
Outer loop
Inner loop
18Multiplication Table
19Multiplication Table
- Private Sub btnDisplay_Click() Handles
btnDisplay.Click - Dim row, entry As String
- Â
- lstTable.Items.Clear()
- For j As Integer 1 To 5
- row ""
- For k As Integer 1 To 3
- entry j " x " k " " (j
k) - row entry " "
- Next
- lstTable.Items.Add(row)
- Next
- End Sub
20For 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.
21Start, 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.
22Altering 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.
23Non-integer Step Values
- Can lead to round-off errors with the result that
the loop is not executed the intended number of
times.
24Non-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