Wednesday, November 24, 2010

lecture notes

Nesting: putting one loop in another
Loop thru values of X in (0 thru 2)
    Loop thru values of y in (0 thru 3)
        Print (x, y)
    End Loop
End Loop

LISTS.TXT
    ' the names of other phonebook files
    CLIENTS.TXT
    FRIENDS.TXT
    KINFOLK.TXT


Loop thru filenames in the LISTS.TXT file
    Open the current filename
    Loop thru the entries in the current file
        see if name occurs in current file
    End Loop
End Loop

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
  Dim foundFlag As Boolean
  Dim fileName As String
  Dim name As String = ""
  Dim phoneNum As String = ""
  Dim sr1 As IO.StreamReader = IO.File.OpenText ("LISTS.TXT")

  txtNumber.Text = "Name not found."
  ' by the way, i use a goto to break out multiple
  ' levels when we find the actual word
  Do While sr1.Peek <> -1
    fileName = sr1.ReadLine
    Dim sr2 As IO.StreamReader = IO.File.OpenText(fileName)
    Do While sr2.Peek <> -1
      name = sr2.ReadLine
      phoneNum = sr2.ReadLine
      If name = txtName.Text Then
    txtNumber.Text = name & "     " & phoneNum
        Goto Here
      Endif
    Loop
  Loop
  sr1.Close()
Here:

End Sub

HW:
Processing Lists of Data with Do Loops
question 20-24, even

Write a program to find and display the largest of a collection of positive numbers contained in a text file. (Test the program with the collection of numbers 89, 77, 95, and 86.)

Dim max as Integer
max = 0

Do While sr.Peek <> -1
    current = sr.ReadLine
    ' is my optimistic assumption wrong?
    If current > max Then
        max = current
    EndIf
Loop

Debug.Print("The max value is: " & max)

No comments:

Post a Comment