Title: Repetition
1Chapter 6
2Comparing 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
3Processing List of Data with Loops
- Open a file as we did previously
- File can be of any length
- EOF Function Tells us if we have read to the
end of a file. - Checks for the end-of-file marker.
4EOF Function Syntax
- 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.
5Flowchart for a Do While Loop
Open Filename.txt
EOF?
Yes
No
Get next item of data
Close Filename.txt
6Example of EOF Phone.txt
- "Bert", "123-4567"
- "Ernie", "987-6543"
- "Grover", "246-8321"
- "Oscar", "135-7900"
7Example 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
Phone.txt
"Bert", "123-4567" "Ernie", "987-6543" "Grover",
"246-8321" "Oscar", "135-7900"
8Processing List of Data with Loops Looking for
a Specific Element
- Open the file
- File may be of any length
- Compare each element against the value of that
being sought - EOF Function Tells us if we have read to the
end of a file.
9Processing List of Data with Loops Looking for
a Specific Element
- Private Sub cmdDisplay_Click()
- Dim nom As String, phoneNum As String
- Open App.Path "\PHONE.TXT" For Input As 1
- Do While (nom ltgt txtName.Text) And (Not EOF(1))
- Input 1, nom, phoneNum
- Loop
- Close 1
- picNumber.Cls
- If nom txtName.Text Then
- picNumber.Print nom, phoneNum
- Else
- picNumber.Print "Name not found."
- End If
- End Sub
Phone.txt
"Bert", "123-4567" "Ernie", "987-6543" "Grover",
"246-8321" "Oscar", "135-7900"
10Counters 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.
11Example of a Data File Analysis of Coins
- 1, 1, 5, 10, 10, 25
- (Two pennies, a nickel, two dimes and a quarter)
12Example 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
COINS.TXT
13Output for the Previous Example
- The value of the 6 coins is 52 cents.
14Boolean 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
15Boolean Variables and Flags - Example
- Data file contains a list of words
- Determine whether or not the words are in order
- Determine the number of words in the file
WORDS.TXT
"cambist", "croissant", "deification" "hydrophyte"
, "incisor", "maculature" "macerate",
"narcolepsy", "shallon"
16Boolean Variables and Flags - Example
- Private Sub cmdAnalyze_Click()
- Dim orderFlag As Boolean, wordCounter As
Integer - Dim word1 As String, word2 As String
- 'Count words. Are they in alphabetical order?
- orderFlag True 'Assume words are in
alphabetical order - wordCounter 0
- word1 ""
- Open App.Path "\WORDS.TXT" For Input As 1
- Do While Not EOF(1)
- Input 1, word2
- wordCounter wordCounter 1
- If word1 gt word2 Then 'Two words are out
of order - orderFlag False
- End If
- word1 word2
- Loop
- Close 1
WORDS.TXT
"cambist", "croissant", "deification" "hydrophyte"
, "incisor", "maculature" "macerate",
"narcolepsy", "shallon"
17Another Example Multiple Phone Lists
- Data file contains a list of Phone Lists
- Each Phone List contains entries
LISTS.TXT
"CLIENTS.TXT" "FRIENDS.TXT" "KINFOLK.TXT"
FRIENDS.TXT
CLIENTS.TXT
KINFOLK.TXT
"Sam Cooper", "999-1231" "Millie Smith",
"555-1212" "Peter Jones", "414-3145"
"Mom", "346-8667" "Grandpa", "224-8873" "Uncle
Tom", "435-7574"
"George", "345-1235" "Virginia",
"673-8896" "Stuart", "245-5576" "Bert",
"123-4567" "Ernie", "987-6543" "Grover",
"246-8321" "Oscar", "135-7900"
18Boolean Variables and Flags - Example
- Private Sub cmdDisplay_Click()
- Dim foundFlag As Boolean, fileName As String
- Dim nom As String, phoneNum As String
- Open App.Path "\LISTS.TXT" For Input As 1
- foundFlag False
- nom ""
- Do While (foundFlag False) And (Not EOF(1))
- Input 1, fileName
- Open App.Path "\" fileName For Input As
2 - Do While (nom ltgt txtName.Text) And (Not
EOF(2)) - Input 2, nom, phoneNum
- Loop
- Close 2
- picNumber.Cls
- If nom txtName.Text Then
- picNumber.Print nom, phoneNum
- foundFlag True
- End If
- Loop
LISTS.TXT
"CLIENTS.TXT" "FRIENDS.TXT" "KINFOLK.TXT"
19Programming Assignment 6
- Write a program to do several types of analysis
of the players of an athletic team.
Specifically, the program should be able to do
the following - Determine the average age of the team members
- Determine the average GPA of the team members
- Determine the average weight of the team members
- Show all team members with a GPA that is above
some level, which the user may enter - The information about the team is stored in a
single data file .
20Homework
- Page 280 1-13 Odd Problems
- 27 Turn in print-out of code
21For ... 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.