Introduction to Computing Dr. Nadeem A Khan
Lecture 23
Processing List of data!
Processing List of Data ► File PHONE.TXT contains the following four lines: “Ahmad”, “ ” “Aslam”, “ ” “Bhati”, “ ” “Jedallah”, “ ” =>Write a program to display names and numbers?s
Processing List of Data (Contd.) ► Program 1 Sub_Command1_Click Dim nom As String, phoneNum As String count%=0 Open “PHONE.TXT” For Input As #1 Do While count<=4 Input #1, nom, phoneNum Picture1.Print nom, phoneNum count=count+1Loop Close #1 End Sub
Processing List of Data (Contd.) How to write the same program without knowing the number of enteries ?
Processing List of Data (Contd.) ► End of File Function EOF(n) where n is the reference number of the open file
Processing List of Data (Contd.) ► Program 2 Sub_Command1_Click Dim nom As String, phoneNum As String Open “PHONE.TXT” For Input As #1 Do While Not EOF(1) Input #1, nom, phoneNum Picture1.Print nom, phoneNum Loop Close #1 End Sub
Counters/Accumulators ► What are: Counters and Accumulators? ► Identify them in the following program
Sub Command1_Click ( ) Dim numCoins As Integer, sum!, value! Open “COINS.TXT” For Input As #1 Let numCoins=0 Let sum =0 Do While Not EOF(1) Input #1, value Let numCoins = numCoins +1 Let sum = sum + value Loop Picture1.Print numCoins;“Coins of value”;sum; “cents” Close #1 End Sub
Flag ► Flag: A variable to keep track whether a certain event has occurred ► How should we realize the following task?
Flags (Contd.) ► File WORDS.TXT contains the following three lines: “cambist”, “croissant”, “deification” “hydrophyte”, “incisor”, “maculature” “macerate”, “narcolepsy”, “shallon” =>Task: Count the number of words and Report if the words are in alphabetical order
Flags (Contd.) ► Solution: Sub Command1_Click ( ) ‘First Part Dim orderFlag%, wordCounter% Dim word1$, word2$ Let orderFlag=0 Let wordCounter=0 Let word1= “” Open “WORDS.TXT” For Input As #1 ‘program continues on next slide
Flags (Contd.) ‘The second part Do While Not EOF(1) Input #1, word2 Let wordCounter = wordCounter+1 If word1>word2 Then Let orderFlag =1 End If Let word1=word2 LoopClose#1 ‘Program continues on the next slide
Flags (Contd.) ‘The last part Picture1.Print “The number of words is”; WordCounter If orderFlag =0 Then Picture1.Print “Wsords are in alphabetical order.” Else Picture1.Print “Words are not in alphabetical order.” End If ‘End of program End Sub
Exit Do ► Can we not exit the do loop when it is useless to continue its execution further?
Exit Do (Contd.) ► Exit Do: Exits the Do loop ► Modify the previous program using Exit Do
Exit Do(Contd.) ‘The second part Do While Not EOF(1) Input #1, word2 Let wordCounter = wordCounter+1 If word1>word2 Then Let orderFlag =1 Do Exit‘exits the do loop if wrong order End If Let word1=word2 LoopClose#1 ‘Program continues on the next slide
Nested Loops ► Nested loop: Loop inside a loop ► What will be printed by the following program?
Sub Command1_Click ( ) Dim num As Integer, counter As Integer Let counter=1 Do While counter<=4 Let num=1 Let num=1 Do While num<=10 Picture1.Print num; Let num=num+1 Loop Let counter=counter+1 Picture1.PrintLoop End Sub Nested Loops (Contd.)
► The result: Nested Loops
► The same: DointCounter=intCounter+1 Loop Until intCounter =10 DointCounter=intCounter+1 Loop While intCounter <10 Other variants of Do
► While condition statementsWend While.. Wend