Do statements Loop Until condition

Download Report

Transcript Do statements Loop Until condition

Introduction to
Computing
Dr. Nadeem A Khan
Lecture 18
Assignment 4
►
Check the website
Loop structure: General Format
► Do
While condition
statements
Loop
Flow chart: loop
structure
Is
condition
true ?
No
Yes
Process
Step(s)
Do Loops
►
Example 1:
Sub Command1_Click ( )
Dim num As Integer
Let num=1
Do While num<=10
Picture1.Print num;
Let num=num+1
Loop
End Sub
Do Loops
► The
result:
1 2 3 4 5 6 7 8 9 10
Do Loops (Contd.)
►
Example 2:
Sub Command1_Click ( )
Dim passWord as String, info As String
If Ucase(Text1.Text) = “SECRET.TXT” Then
Let passWord=“”
Do While passWord <> “SHAHID”
Let passWord = InputBox$(“Enter passWord”)
Let passWord = Ucase(passWord)
Loop
End If
‘ Remaining statements on next slide
Do Loops (Contd.)
►
Example 2 (Contd.):
‘Continues from the previous slide
Open Text1.Text For Input As #1
Input #1, info
Picture1.Print info
Close #1
End Sub
Do-Loop-Until: General Format
► Do
statements
Loop Until condition
Flow chart?
Do-Loop-Until: General Format
► Modify
Format
example 2 to Do-loop-Until
Do-Loop-Until (Contd.)
►
Example 1:
Sub Command1_Click ( )
Dim passWord as String, info As String
If Ucase(Text1.Text) = “SECRET.TXT” Then
Do
Let passWord = InputBox$(“Enter passWord”)
Let passWord = Ucase(passWord)
Loop Until passWord = “SHAHID”
End If
‘ Remaining statements on next slide
Do-Loop-Until (Contd.)
►
Example 1 (Contd.):
‘Continues from the previous slide
Open Text1.Text For Input As #1
Input #1, info
Picture1.Print info
Close #1
End Sub
Do-Loop: Another Example
► When
will the following program
come to its end?
Do-Loop: Another Example
Sub Command1_Click ( )
Dim num As Single
num = 7
Do While num <> 0
num=num-2
Loop
End Sub
Do-Loop-Until: General Format
► Do
statements
Loop Until condition
Flow chart?
Processing List of data!
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
► Program
1
Sub_Command1_Click
Dim nom As String, phoneNum As String
Dim count As Integer
count=0
Open “PHONE.TXT” For Input As #1
Do While count<=4
Input #1, nom, phoneNum
Picture1.Print nom, phoneNum
count=count+1
Processing
List
Loop
of Data
Close #1
(Contd.)
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
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
Nested Loops
►
►
Nested loop: Loop inside a loop
What will be printed by the following
program?
Nested Loops (Contd.)
Sub Command1_Click ( )
Dim num As Integer, counter As Integer
Let counter=1
Do While counter<=4
Let num=1
Do While num<=10
Picture1.Print num;
Let num=num+1
Loop
Let counter=counter+1
Picture1.Print
Loop
End Sub
Nested Loops
► The
result:
1
1
1
1
5
5
5
5
2
2
2
2
3
3
3
3
4
4
4
4
6
6
6
6
7
7
7
7
8
8
8
8
9
9
9
9
10
10
10
10
Other variants of Do
► The
same:
Do
intCounter=intCounter+1
Loop Until intCounter =10
Do
intCounter=intCounter+1
Loop While intCounter <10
While.. Wend
►
While condition
statements
Wend