Title: Repetition
1Chapter 6
2Outline Objectives
- Loop Structure
- Elements of a Loop Structure
- Processing Lists of Data with Do Loops
3Types of LOOP Structures
- Do While . Loop
- Do Until Loop
- For Next loop
4Basic Definition
- Looping the process of repeating a series of
statements as many times as needed. - Looping also called iteration.
5Basic 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
6The Do While . Loop
- Do While condition is true
- statement(s)
- Loop
7Flowchart for a Do While Loop
Is the condition true
No
Yes
Execute statements within the loop
Execute statements that follow the loop
8Example (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 lt 10
- picNumbers.Print num
- num num 1
- Loop
- End Sub
9The 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.
10Controlling 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.
11Example of event-controlled loops
- passWord ""
- Do While passWord ltgt "SHAZAM"
- passWord UCase(InputBox("What is the
password?")) - Loop
12Counter-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.
13Example
- num 1
- Do While num lt 10
- picOutput.Print num
- num num 1
- Loop
14Do Until . Loop
- Is executed until the condition becomes True
- Any Do While. Loop can be rewritten as a Do
Until .. Loop
15Example (requires the user to give a password
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
16Example (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 lt 0
- picResult.Print "It takes" yrs "years to
deplete the account." - End Sub
17Comparing While and Until Loops
- 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
18Processing List of Data with Loops
- EOF Function
- The EOF function tells us if we have read to the
end of a file. - Checks for the end-of-file marker.
19EOF Function
- EOF(n) where n is the reference number for the
file. - If there are more records to be read, EOF is
False. - When the end-of-file marker is reached EOF
becomes True.
20Example of EOF
- Open Phone.txt for Input As 1
- Do While Not EOF(1)
- Input 1, nom, phoneNum
- picOutput.Print nom, phoneNum
- Loop
- Close 1
21Counters and Accumulators
- A Counter is a numeric variable that keeps track
of the number of items that have been processed
in the loop. - An Accumulator is a numeric variable that totals
numbers.
22Example Counter Accumulator
- Private 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." - Close 1
- End Sub
23Example of a Data File
24Output for the Previous Example
- The value of the 6 coins is 52 cents.
25Boolean Variables and Flags
- A Boolean variable can hold one of two values
True or False. - Initial (default) value is False.
- Flags are Boolean variables
- - Used within a loop to provide information that
will be used after the loop terminates - - Provide an alternative for terminating the loop
- Dim myFlag as Boolean
26Programming Assignment 6
27For ... Next Loop
- Is used to create a counting loop.
- Loop control variable has an initial value.
- Loop control variable has a terminating value.
- Loop control variable has a Step value.
28Syntax of For Next Loop
- For loop-control-variable initial To terminal
- statement(s)
- Next loop-control-variable
29Example ( display a table of the first 5 numbers
and their square)
- Private Sub cmdDisplayTable_Click()
- Dim i As Integer
- Display a table of the first 5 numbers and
their sqares - picTable.Cls
- For i 1 To 5
- picTable.Print i i 2
- Next i
- End Sub
Loop Control variable
Terminating value
Initial Value
30Example ( step value of 2)
- For counter 1 To 5 Step 2
- picOutput.Print counter
- Next counter
31When the For statement is first encountered
- This explanation assumes a positive step value
- The initial, terminal, and (if given ) step value
expression are evaluated. - The loop control variable is assigned to the
initial value.
32This explanation assumes a positive step value
- The value of the loop control variable is tested
against the terminal value. - If the loop control variable is less than or
equal to the terminal value, the loop body is
executed. - If the loop control variable is greater than the
terminal value, the loop body is skipped, and
control passes to the first statement following
the Next.
33When the Next statement is encountered
- The step value is added to the loop control
variable. If there is no step value, 1 is added. - A check is performed to determine if the value of
the loop control variable exceeds the terminal
value.
34Continued
- If the loop control variable is less than or
equal to the terminal value, control transfers
back to the statement after the For statement
and the loop is repeated. - Otherwise, the loop is exited, and execution
continues with the statement following the Next.
35Rules for Using For ... Next loop
- The initial, terminal, and step values cannot be
modified in the loop body. - You should never modify the value of the loop
control variable in the loop body. - Each For statement must end with a Next statement.
36Example (display 10 stars)
- Private Sub cmdDisplay_Click()
- Dim i As Integer
- ' Display a row of ten stars
- picOutput.Cls
- For i 1 To 10
- picOutput.Print ""
- Next i
- End Sub
37Example (request a number and display a row of
that many stars)
- Private Sub cmdDisplay_Click()
- Dim i As Integer, stars As Integer
- ' Display a row of stars
- picOutput.Cls
- stars Val(InputBox("Row length (1-20) "))
- For i 1 To stars
- picOutput.Print ""
- Next i
- End Sub
38Example (the step value is negative)
- For Counter 8 To 1 Step -2
- picOutput.Print Counter
- Next Counter
39Nested Loops
- For Outer 1 To 4
- For Inner 1 To 2
- ..
- ..
- Next Inner
- Next Outer
40Example (display a 10 by 10 array of stars)
- Private Sub cmdDisplay_Click()
- Dim i As Integer, j As Integer
- ' Display 10 x 10 square of stars
- For i 1 To 10
- For j 1 To 10
- picOutput.Print ""
- Next j
- picOutput.Print
- Next i
- End Sub
41Guidelines for Choosing a Loop
- If counting loop, use a For Next Loop.
- If trailer-values and body is executed at least
once, use Do Until.. Loop. - If trailer-value and nothing is known about the
first execution, use Do While. Loop. - If you are not sure use Do While.. Loop.