Introduction to Computing Dr. Nadeem A Khan
Lecture 22
Loop structure: General Format ► Do While condition statementsLoop
Flow chart: loop structure No Process Step(s) Is condition true ? Yes
► 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: Do Loops
► 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 Loops (Contd.)
Do-Loop-Until: General Format ► Do statements Loop Until condition Flow chart?
Do-Loop-Until: General Format ► Modify example 2 to Do-loop-Until Format
► 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-Until (Contd.)
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-2Loop End Sub
Note: ► Read: Schneider (Section 6.1) ► Read comments and do practice problems of these sections of these sections ► Attempt some questions of Exercises