Download presentation
Presentation is loading. Please wait.
1
Introduction to Computing Dr. Nadeem A Khan
2
Lecture 23
3
Processing List of data!
4
Processing List of Data ► File PHONE.TXT contains the following four lines: “Ahmad”, “5884184” “Aslam”, “5886185” “Bhati”, “5861613” “Jedallah”, “5887164” =>Write a program to display names and numbers?s
5
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
6
Processing List of Data (Contd.) How to write the same program without knowing the number of enteries ?
7
Processing List of Data (Contd.) ► End of File Function EOF(n) where n is the reference number of the open file
8
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
9
Counters/Accumulators ► What are: Counters and Accumulators? ► Identify them in the following program
10
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
11
Flag ► Flag: A variable to keep track whether a certain event has occurred ► How should we realize the following task?
12
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
13
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
14
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
15
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
16
Exit Do ► Can we not exit the do loop when it is useless to continue its execution further?
17
Exit Do (Contd.) ► Exit Do: Exits the Do loop ► Modify the previous program using Exit Do
18
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
19
Nested Loops ► Nested loop: Loop inside a loop ► What will be printed by the following program?
20
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.)
21
► The result: 1 2 3 4 5 6 7 8 9 10 Nested Loops
22
► The same: DointCounter=intCounter+1 Loop Until intCounter =10 DointCounter=intCounter+1 Loop While intCounter <10 Other variants of Do
23
► While condition statementsWend While.. Wend
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.