Chapter 6
description
Transcript of Chapter 6
![Page 1: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/1.jpg)
Chapter 6 - Visual Basic Schneider 1
Chapter 6Chapter 6
Repetition
![Page 2: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/2.jpg)
Chapter 6 - Visual Basic Schneider 2
Outline & ObjectivesOutline & Objectives
Loop Structure Elements of a Loop Structure Processing Lists of Data with Do Loops
![Page 3: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/3.jpg)
Chapter 6 - Visual Basic Schneider 3
Types of LOOP StructuresTypes of LOOP Structures
Do While ……. Loop Do Until …… Loop For …… Next loop
![Page 4: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/4.jpg)
Chapter 6 - Visual Basic Schneider 4
Basic DefinitionBasic Definition
Looping: the process of repeating a series of statements multiple times until a criteria is met
![Page 5: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/5.jpg)
Chapter 6 - Visual Basic Schneider 5
Basic Components of LoopsBasic Components of Loops
Loop control variable: A variable used to determine whether a loop will be executed
Loop body: The statement (s) that are executed each time a loop repeats
![Page 6: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/6.jpg)
Chapter 6 - Visual Basic Schneider 6
The Do While ……. LoopThe Do While ……. Loop
Do While condition is true
statement(s)
Loop
![Page 7: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/7.jpg)
Chapter 6 - Visual Basic Schneider 7
Flowchart for a Do While LoopFlowchart for a Do While Loop
Is the condition true
Execute statementswithin
the loop
Execute statementsthat follow the loop
Yes
No
![Page 8: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/8.jpg)
Chapter 6 - Visual Basic Schneider 8
Example Example (Displays the numbers from 1 through 10)(Displays the numbers from 1 through 10)
Private Sub cmdDisplay_Click()
Dim num As Integer
' Display the numbers from 1 to 10
num = 1
Do While num <= 10
picNumbers.Print num;
num = num + 1
Loop
End Sub
![Page 9: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/9.jpg)
Chapter 6 - Visual Basic Schneider 9
The Do While ……. LoopThe Do While ……. Loop
Is executed as long as the condition is True. If condition is False then the next statement after
the Loop is executed.
![Page 10: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/10.jpg)
Chapter 6 - Visual Basic Schneider 10
Controlling LoopsControlling Loops
Methods of controlling loops: Counter-controlled loops
repeat a specific number of times
Event-controlled loops repeat until something happens in the loop body to change
the value of loop control variable.
![Page 11: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/11.jpg)
Chapter 6 - Visual Basic Schneider 11
Example of event-controlled loopsExample of event-controlled loops
passWord = ""
Do While passWord <> "SHAZAM"
passWord = UCase(InputBox("What is the password?"))
Loop
![Page 12: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/12.jpg)
Chapter 6 - Visual Basic Schneider 12
Counter-controlled LoopsCounter-controlled Loops
Is useful when the programmer knows how many times the loop should be executed.
Initialize the counter by setting it to a beginning value before entering the loop.
The counter is incremented (or decremented) by the same value during each repetition.
![Page 13: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/13.jpg)
Chapter 6 - Visual Basic Schneider 13
ExampleExample
num = 1
Do While num <= 10
picOutput.Print num;
num = num + 1
Loop
![Page 14: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/14.jpg)
Chapter 6 - Visual Basic Schneider 14
Do Until ……. LoopDo Until ……. Loop
Is executed until the condition becomes True Any Do While…. Loop can be rewritten as a Do
Until ….. Loop
![Page 15: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/15.jpg)
Chapter 6 - Visual Basic Schneider 15
Example Example (requires the user to give a password (requires the user to give a password
before opening a file)before opening a file)
Private Sub cmdDisplay_Click()
Dim passWord As String, info As String
If UCase(txtName.Text) = "SECRET.TXT" Then
Do
passWord = UCase(InputBox("What is the password?"))
Loop Until passWord = "SHAZAM"
End If
Open txtName.Text For Input As #1
Input #1, info
picItem.Cls
picItem.Print info
Close #1
End Sub
![Page 16: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/16.jpg)
Chapter 6 - Visual Basic Schneider 16
Example Example (years to deplete a saving account)(years to deplete a saving account)
Private Sub cmdEstimate_Click()
Dim amt As Single, yrs As Integer
' Years to deplete savings account
picResult.Cls
amt = 15000
yrs = 0
Do
amt = amt * 1.05 - 1000
yrs = yrs + 1
Loop Until amt <= 0
picResult.Print "It takes"; yrs; "years to deplete the account."
End Sub
![Page 17: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/17.jpg)
Chapter 6 - Visual Basic Schneider 17
Comparing While… and Until Comparing While… and Until LoopsLoops
The Do While … Loop executes while the condition is true
The Do Until….. Loop executes until the condition is true
Both can be used to create any type of loop
![Page 18: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/18.jpg)
Chapter 6 - Visual Basic Schneider 18
EOF FunctionEOF Function
EOF(n) is True if the end of the file having reference number n has been reached. Otherwise, it is False
Example: Open “PHONE.TXT” for Input As #1Do While Not EOF(1) Input #1, nom, phoneNum picOutput.Print nom, phoneNumLoopClose #1
![Page 19: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/19.jpg)
Chapter 6 - Visual Basic Schneider 19
Counters and AccumulatorsCounters and Accumulators
A counter is a numeric variable that keeps track of the number of items that have been processed in a loop.
An accumulator is a numeric variable that holds a sub-total during multiple passes through a loop.
![Page 20: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/20.jpg)
Chapter 6 - Visual Basic Schneider 20
Example:Example: CounterCounter && Accumulator AccumulatorPrivate Sub cmdAnalyze_Click() Dim numCoins As Integer, sum As Single, value As Single Open "COINS.TXT" For Input As #1 numCoins = 0 sum = 0 Do While Not EOF(1) Input #1, value numCoins = numCoins + 1 sum = sum + value Loop picValue.Print "The value of the"; numCoins; "coins is"; sum;
"cents."End Sub
![Page 21: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/21.jpg)
CompareCompare
Do While ……. Loop Do ……. Loop While Do ……. Loop Until Do Until ……. Loop
Chapter 6 - Visual Basic Schneider 21
![Page 22: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/22.jpg)
ReviewReview
num = 11
Do While num <= 10
picOutput.Print num;
num = num + 1
Loop
Chapter 6 - Visual Basic Schneider 22
num = 11
Do
picOutput.Print num;
num = num + 1
Loop until num <= 10
How many times will the following loops execute?
![Page 23: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/23.jpg)
ReviewReview
Do While i < 10
i = i + 1
Loop
Chapter 6 - Visual Basic Schneider 23
Do
i = i + 1
Loop While i < 10
Which loop is infinite?
Do Until i < 10Loop
Do i = i + 10 Loop Until i < 10
![Page 24: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/24.jpg)
Chapter 6 - Visual Basic Schneider 24
For … Next LoopFor … Next Loop
A loop where the number of iterations is determined by a range of values for a numeric variable
Syntax:
For controlVariable = initial To terminal
statement(s)
Next controlVariable
![Page 25: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/25.jpg)
Chapter 6 - Visual Basic Schneider 25
ExampleExample
Private Sub cmdDisplayTable_Click()
Dim i As Integer
‘Display a table of the first 5 numbers and their squares
For i = 1 To 5
picTable.Print i; i ^ 2
Next i
End Sub
Initial Value
Control variable
Terminating value
![Page 26: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/26.jpg)
Chapter 6 - Visual Basic Schneider 26
ExampleExample
Dim numVar As Integer
For numVar = 1 To 5 Step 2
picOutput.Print numVar;
Next numVar
Output: 1 3 5
![Page 27: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/27.jpg)
Chapter 6 - Visual Basic Schneider 27
When a For statement is When a For statement is encounteredencountered
1. The control variable is assigned the initial value.
2. After each loop iteration, the step value is added to the value of the control variable. (If there is no step value, 1 is added.)
3. Iteration continues until the terminating value is exceeded.
![Page 28: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/28.jpg)
Chapter 6 - Visual Basic Schneider 28
Rules for Using Rules for Using For ... NextFor ... Next loop loop
You should never modify the value of the loop control variable in the loop body.
Each For loop must end with a Next statement.
![Page 29: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/29.jpg)
Chapter 6 - Visual Basic Schneider 29
ExampleExample
Private Sub cmdDisplay_Click()
Dim i As Integer
For i = 1 To 10
picOutput.Print "*";
Next i
End Sub
Output: **********
![Page 30: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/30.jpg)
Chapter 6 - Visual Basic Schneider 30
Example Example
Private Sub cmdDisplay_Click()
Dim i As Integer, stars As Integer
stars = Val(InputBox("Row length (1-20) : "))
For i = 1 To stars
picOutput.Print "*";
Next i
End Sub
![Page 31: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/31.jpg)
Chapter 6 - Visual Basic Schneider 31
ExampleExample
Dim numVar As Integer
For numVar = 8 To 1 Step -2
picOutput.Print numVar;
Next numVar
Output: 8 6 4 2
![Page 32: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/32.jpg)
Chapter 6 - Visual Basic Schneider 32
Nested LoopsNested Loops
For outer = 1 To 4
For inner = 1 To 2
..
..
Next inner
Next outer
![Page 33: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/33.jpg)
Chapter 6 - Visual Basic Schneider 33
Example: Display a 10x10 Example: Display a 10x10 rectangle of starsrectangle of starsPrivate Sub cmdDisplay_Click()
Dim i As Integer, j As Integer
For i = 1 To 10
For j = 1 To 10
picOutput.Print "*";
Next j
picOutput.Print
Next i
End Sub
![Page 34: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/34.jpg)
ReviewReview
For i= -5 To -1 Step - 2
picOutput.Print i;
Next i
picOutput.Print i;
Chapter 6 - Visual Basic Schneider 34
For i= 1 To 5 Step 2
i=i+1
picOutput.Print i;
Next i
picOutput.Print i;
![Page 35: Chapter 6](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813c6e550346895da5ffe1/html5/thumbnails/35.jpg)
Chapter 6 - Visual Basic Schneider 35
ReviewReview
Private Sub cmdDisplay_Click()
Dim i As Integer, j As Integer
For i = 1 To 10
For j = i To 10
picOutput.Print "*";
Next j
picOutput.Print
Next i
End Sub
Private Sub cmdDisplay_Click()
Dim i As Integer, j As Integer
For i = 1 To 10
For j = 1 To i
picOutput.Print "*";
Next j
picOutput.Print
Next i
End Sub