Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computing Dr. Nadeem A Khan. Lecture 8.

Similar presentations


Presentation on theme: "Introduction to Computing Dr. Nadeem A Khan. Lecture 8."— Presentation transcript:

1 Introduction to Computing Dr. Nadeem A Khan

2 Lecture 8

3 Flow chart: loop structure No Process Step(s) Is condition true ? Yes

4 ► 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

5 ► The result: 1 2 3 4 5 6 7 8 9 10 Do Loops

6 ► 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.)

7 ► 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.)

8 Do-Loop-Until: General Format ► Do statements Loop Until condition Flow chart?

9 Do-Loop-Until: General Format ► Modify example 2 to Do-loop-Until Format

10 ► 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.)

11 ► 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.)

12 Do-Loop: Another Example ► When will the following program come to its end?

13 Do-Loop: Another Example Sub Command1_Click ( ) Dim num As Single num = 7 Do While num <> 0 num=num-2Loop End Sub

14 Processing List of data!

15 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

16 ► 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+1Loop Close #1 End Sub Processing List of Data (Contd.)

17 How to write the same program without knowing the number of enteries ?

18 Processing List of Data (Contd.) ► End of File Function EOF(n) where n is the reference number of the open file

19 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

20 Counters/Accumulators ► What are: Counters and Accumulators? ► Identify them in the following program

21 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

22 Exit Do ► Can we not exit the do loop when it is useless to continue its execution further?

23 Exit Do (Contd.) ► Exit Do: Exits the Do loop

24 Nested Loops ► Nested loop: Loop inside a loop ► What will be printed by the following program?

25 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.)

26 ► The result: 1 2 3 4 5 6 7 8 9 10 Nested Loops

27 ► The same: DointCounter=intCounter+1 Loop Until intCounter =10 DointCounter=intCounter+1 Loop While intCounter <10 Other variants of Do

28 ► While condition statementsWend While.. Wend

29 ► The general format with step of 1: For i = m To n Statements Next i For…Next Loops

30 ► Convert to For.. Next Loop Sub Command1_Click ( ) Dim num As Integer Let num=1 Do While num<=10 Picture1.Print num; Let num=num+1 Loop End Sub For Next Loops

31 ► Example with For.. Next Loop Sub Command1_Click ( ) Dim num As Integer For num=1 To 10 Picture1.Print num; Next num End Sub For Next Loops

32 ► The result: 1 2 3 4 5 6 7 8 9 10 For Next Loops

33 ► The general format with steps: For i = m To n Step s Statements Next i => Each time increment: i = i + s; m, n, s could be numeric expressions m, n, s could be numeric expressions For…Next Loops

34 ► Example with For.. Next Loop with steps Sub Command1_Click ( ) Dim num As Integer For num= 1 To 10 Step 2 Picture1.Print num; Next num End Sub The output? For Next Loops

35 ► The result: 1 3 5 7 9 For Next Loops

36 ► Rewrite the following program using nested For.. Next Loop Nested Loops: For…Next

37 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: For…Next

38 Sub Command1_Click ( ) Dim num As Integer, counter As Integer For counter = 1 To 4 For num =1 To 10 For num =1 To 10 Picture1.Print num; Next num Picture1.Print Next counter End Sub Nested Loops: For…Next

39 ► The result: 1 2 3 4 5 6 7 8 9 10 Nested Loops

40 ► Exit For: Works similar as Exit Do Exit For

41 Select Case Blocks

42 ► Example: Sub Command1_Click ( ) Picture1.Cls Let num = Val(Text1.Text) Select Case num Case 1 Picture1.Print “Buckle my shoe.” Case 2 Picture1.Print “Pick the sticks.” Case 3,4,5,6 Picture1.Print “Lay them straight.” Case Else Picture1.Print “Start all over again.” End Select End Sub

43 Select Case Block ► General form Select Case selector Case valueList1 action1 Case valueList2 action2.. Case Else action of last resort End Select

44 Another example

45 Sub Command1_Click ( ) Sub Command1_Click ( ) Dim x As Integer, y As Integer, num As Integer Let x=2 Let y=3 Let num = Val(Text1.Text) Select Case num Case y-x, x Picture1.Print “Buckle my shoe.” Case Is<=4 Picture1.Print “Pick up sticks.” Case x+y To x*y Picture1.Print “Lay them straight.” Case Else Picture1.Print “Start all over again.” End Select End Sub

46 Sub Command1_Click ( ) Sub Command1_Click ( ) Dim x As Integer, y As Integer, num As Integer Let x=2 Let y=3 Let num = Val(Text1.Text) Select Case num Case y-x, x Picture1.Print “Buckle my shoe.” Case Is<=4 Picture1.Print “Pick up sticks.” Case x+y To x*y Picture1.Print “Lay them straight.” Case Else Picture1.Print “Start all over again.” End Select End Sub

47 Read Chapter 5 completely!


Download ppt "Introduction to Computing Dr. Nadeem A Khan. Lecture 8."

Similar presentations


Ads by Google