Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 6 Repetition. 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists.

Similar presentations


Presentation on theme: "1 Chapter 6 Repetition. 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists."— Presentation transcript:

1 1 Chapter 6 Repetition

2 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists of Data with Do Loops Processing Lists of Data with Do Loops

3 3 Types of LOOP Structures Do While ……. Loop Do While ……. Loop Do Until …… Loop Do Until …… Loop For …… Next loop For …… Next loop

4 4 Basic Definition Looping: the process of repeating a series of statements multiple times until a criteria is met Looping: the process of repeating a series of statements multiple times until a criteria is met

5 5 Basic Components of Loops Loop control variable: A variable used to determine whether a loop will be executed Loop control variable: A variable used to determine whether a loop will be executed Loop body: The statement (s) that are executed each time a loop repeats Loop body: The statement (s) that are executed each time a loop repeats

6 6 The Do While ……. Loop Do While condition is true statement(s) statement(s)Loop

7 7 Flowchart for a Do While Loop Is the condition true Execute statements within the loop Execute statements that follow the loop Yes No

8 8 Example (Displays the numbers from 1 through 10) Private Sub cmdDisplay_Click() Dim num As Integer Dim num As Integer ' Display the numbers from 1 to 10 ' Display the numbers from 1 to 10 num = 1 num = 1 Do While num <= 10 Do While num <= 10 picNumbers.Print num; picNumbers.Print num; num = num + 1 num = num + 1 Loop Loop End Sub

9 9 The Do While ……. Loop Is executed as long as the condition is True. Is executed as long as the condition is True. If condition is False then the next statement after the Loop is executed. If condition is False then the next statement after the Loop is executed.

10 10 Controlling Loops Methods of controlling loops: Methods of controlling loops: Counter-controlled loopsCounter-controlled loops repeat a specific number of timesrepeat a specific number of times Event-controlled loopsEvent-controlled loops repeat until something happens in the loop body to change the value of loop control variable.repeat until something happens in the loop body to change the value of loop control variable.

11 11 Example of event-controlled loops passWord = "" Do While passWord <> “ ADMIN" passWord = UCase(InputBox("What is the password?")) passWord = UCase(InputBox("What is the password?"))Loop

12 12 Counter-controlled Loops Is useful when the programmer knows how many times the loop should be executed. Is useful when the programmer knows how many times the loop should be executed. Initialize the counter by setting it to a beginning value before entering the loop. Initialize the counter by setting it to a beginning value before entering the loop. The counter is incremented (or decremented) by the same value during each repetition. The counter is incremented (or decremented) by the same value during each repetition.

13 13 Example num = 1 Do While num <= 10 picOutput.Print num; picOutput.Print num; num = num + 1 num = num + 1Loop

14 14Example

15 15 Dim x As Integer Dim s As Integer Do While x >= 2 x = x + 1 s = s + x Loop picOutput.Print x; s Example

16 16 Dim x As Integer Dim s As Integer Do While x <= 5 If x <= 2 Then x = x + 2 x = x + 2Else x = x + 1 x = x + 1 End If s = s + x Loop picOutput.Print x; s Example

17 17Example

18 18 Dim x As Integer Dim s As Integer Do x = x + 1 s = s + x Loop While x >= 2 picOutput.Print x; s Example

19 19 Try solving the following Problems 1. Write a program to print numbers from 1 to 10 2. Print 10 to 1 3. Print 50 to 100 4. Print the even [odd] numbers from 1 to 20 5. Summation of numbers from 100 to 200 6. Average of 10 marks 7. Find max, min

20 20 Do Until ……. Loop Is executed until the condition becomes True Is executed until the condition becomes True Any Do While …. Loop can be rewritten as a Do Until ….. Loop Any Do While …. Loop can be rewritten as a Do Until ….. Loop

21 21 Dim x As Integer Dim s As Integer Do Until x >= 2 x = x + 1 s = s + x Loop picOutput.Print x; s Example

22 22 Dim x As Integer Dim s As Integer Do x = x + 1 s = s + x Loop Until x >= 2 picOutput.Print x; s Example

23 23 Dim x As Integer Dim s As Integer Do x = x + 1 s = s + x Loop Until x <= 2 picOutput.Print x; s Example

24 24 Example (requires the user to give a password before opening a file) Private Sub cmdDisplay_Click() Dim passWord As String, info As String Dim passWord As String, info As String If UCase(txtName.Text) = "SECRET.TXT" Then If UCase(txtName.Text) = "SECRET.TXT" Then Do Do passWord = UCase(InputBox("What is the password?")) passWord = UCase(InputBox("What is the password?")) Loop Until passWord = "SHAZAM" Loop Until passWord = "SHAZAM" End If End If Open txtName.Text For Input As #1 Open txtName.Text For Input As #1 Input #1, info Input #1, info picItem.Cls picItem.Cls picItem.Print info picItem.Print info Close #1 Close #1 End Sub

25 25 Example (years to deplete a saving account) Private Sub cmdEstimate_Click() Dim amt As Single, yrs As Integer Dim amt As Single, yrs As Integer ' Years to deplete savings account ' Years to deplete savings account picResult.Cls picResult.Cls amt = 15000 amt = 15000 yrs = 0 yrs = 0 Do Do amt = amt * 1.05 - 1000 amt = amt * 1.05 - 1000 yrs = yrs + 1 yrs = yrs + 1 Loop Until amt <= 0 Loop Until amt <= 0 picResult.Print "It takes"; yrs; "years to deplete the account." picResult.Print "It takes"; yrs; "years to deplete the account." End Sub

26 26 Comparing While… and Until Loops The Do While … Loop executes while the condition is true The Do While … Loop executes while the condition is true The Do Until ….. Loop executes until the condition is true The Do Until ….. Loop executes until the condition is true Both can be used to create any type of loop Both can be used to create any type of loop

27 27 Converting While loop to Until loop Do While Condition Action(s)Loop Do Until Not(Condition) Action(s)Loop Equivalents to Do While x <= y Action(s)Loop Do Until x > y Action(s)Loop Equivalents to Do While x =10 Action(s)Loop Do Until Not(x =10) Action(s) Loop Equivalents to

28 28 Review num = 11 Do While num <= 10 picOutput.Print num; picOutput.Print num; num = num + 1 num = num + 1Loop num = 11 Do picOutput.Print num; num = num + 1 Loop until num <= 10 How many times will the following loops execute? 0 Infinite loop

29 29 Review i = 1 Do While i < 10 i = i + 1 i = i + 1Loop i = 0 Do i = i + 1 Loop While i < 10 Which loop is infinite? i = 11 Do Until i < 10 Loop i = 0 Do i = i + 10 Loop Until i < 10 NO Yes

30 30 EOF Function EOF(n) is True if the end of the file having reference number n has been reached. Otherwise, it is False EOF(n) is True if the end of the file having reference number n has been reached. Otherwise, it is False

31 31 Dim count As Integer count = 1 Open “DATA.TXT” For Input As #1 Do While NOT EOF(1) Input #1, x Input #1, x count= count + 1 s = s + x Loop will be true if the end of file has been reached, and false otherwise If all entries in the file are read, the EOF returns True When the file is opened, the EOF returns False until all entries in the file are readExample

32 32 Counters and Accumulators A counter is a numeric variable that keeps track of the number of items that have been processed in a loop. A counter is a numeric variable that keeps track of the number of items that have been processed in a loop. An accumulator is a numeric variable that holds a sub-total during multiple passes through a loop. An accumulator is a numeric variable that holds a sub-total during multiple passes through a loop.

33 33 Example: Counter & Accumulator Private Sub cmdAnalyze_Click() Dim numCoins As Integer, sum As Single Dim numCoins As Integer, sum As Single Dim value As Single Dim value As Single Open "COINS.TXT" For Input As #1 Open "COINS.TXT" For Input As #1 numCoins = 0 numCoins = 0 sum = 0 sum = 0 Do While Not EOF(1) Do While Not EOF(1) Input #1, value Input #1, value numCoins = numCoins + 1 numCoins = numCoins + 1 sum = sum + value sum = sum + value Loop Loop picValue.Print "The value of the"; numCoins; "coins is"; sum; "cents." picValue.Print "The value of the"; numCoins; "coins is"; sum; "cents." End Sub 50 10 5 25 COINS.TXT

34 34 Compare Do While ……. Loop Do While ……. Loop Do ……. Loop While Do ……. Loop While Do ……. Loop Until Do ……. Loop Until Do Until ……. Loop Do Until ……. Loop

35 35 How many times the following code executes the print statement a = 0 a = 0 Do Do Print a + 1 Print a + 1 Loop Until a <= 10 Loop Until a <= 10 1

36 36 How many times the following code executes the print statement a = 1 a = 1 Do Until a < 10 Do Until a < 10 Print a + 2 Print a + 2 Loop Loop 0

37 37 How many times the following code executes the print statement a = 0 a = 0 Do Do Print a + 1 Print a + 1 Loop While a <= 10 Loop While a <= 10 a = 1 a = 1 Do While a < 10 Do While a < 10 Print a + 2 Print a + 2 Loop Loop Infinite loop

38 38 What is the output of the following code Dim month As Integer x = 1 Do Select Case (x / 2 - 2) Select Case (x / 2 - 2) Case 0 Case 0 Print x; Print x; End Select End Select x = x + 1 x = x + 1 Loop Until (x > 5) 4

39 39 What is the value of x after the following code executes? f = True x = 10 Do While (f) If (x / 4 = 1) Then f = False If (x / 4 = 1) Then f = False x = x - 1 x = x - 1Loop Print x 3

40 40 Assume that the file Data.txt contains the following entries: Assume that the file Data.txt contains the following entries: 1, 9, 5, 3, 7, 4, 9, 2, 1, 1, 5 and the file Numbers.txt contains the following entries: and the file Numbers.txt contains the following entries:2,4,5,1Example

41 41 What is the output of the following code? Dim y As Integer Open "Data.txt" For Input As #1 Open "Numbers.txt" For Input As #2 Do Input #1, x Input #1, x If x < 7 Then If x < 7 Then Input #2, y Input #2, y End If End If Print x + y; Print x + y; Loop Until EOF(1) Or EOF(2) 3 11 9 8 12 5

42 42 What is the output of the following code? Do While x <= 10 x = x + 10 x = x + 10 c = c + 1 c = c + 1Loop Do Until x > 20 x = x * 2 x = x * 2 c = c + 1 c = c + 1Loop Picture1.Print x; c 40 3

43 43 Which one of the following code is an infinite loop? Dim x as integer Do Until x<>2 Loop Dim x as integer, y as integer x=10 y=0 Do While x + y = 10 x=x+1 y=y-1 Loop

44 44 Are the following code infinite loops? Dim x as integer Do Loop while x <> 0 Dim x as integer x=0 do x=10 Loop Until x=10

45 45 What is the output of the following code? x = 0 y = 0 Do While x > 10 Or y 10 Or y < 10 y = y + x y = y + x x = y x = yLoop Print x; y Infinite loop

46 46 Which one of the following code has the correct syntax of "Do Loop Until" structure? x= 20 Do Loop x=x-1 print x Until x<10 x= 20 Loop x=x-1 print x Do Until x<10 x=20 Do x=x-1 print x Loop Until x<10 x=20 Do x=x-1 print x Loop Until

47 47 What is the output of the following code? Assume the contents of S.txt are: 1.5, 4.2, 9.7, 3.8 Assume the contents of S.txt are: 1.5, 4.2, 9.7, 3.8 Dim s As Single Open "S.txt" For Input As #1 Do Input #1, s Input #1, s Loop Until EOF(1) Input #1, s Pic.Print s; Close #1 Run-time error

48 48 What is the output of the following code? Assume the contents of r1.txt are: 2,4,6 Assume the contents of r1.txt are: 2,4,6 Dim r As Integer, m As Integer m = 1 Open "r1.txt" For Input As #1 Do Input #1, r Input #1, r m = m * r m = m * r Loop While EOF(1) Pic.Print m; Close #1 2

49 49 How many stars (asterisks "*") will the following code print? x = 1 Do While x <= 3 y = 0 y = 0 Do Until y = x Do Until y = x Picture1.Print "*" Picture1.Print "*" y = y + 1 y = y + 1 Loop Loop x = x + 1 x = x + 1Loop 6

50 50 What is the output of the following code? Dim i As Integer i = 2 Do While i <= 9 Select Case i Case 1 Case 1 Case Is > 5 Case Is > 5 output = output & "A" output = output & "A" Case 3 To 6 Case 3 To 6 output = output & "B" output = output & "B" Case Else Case Else output = output & "C" output = output & "C" End Select End Select i = i +3 loop Print output CBA

51 51 For … Next Loop A loop where the number of iterations is determined by a range of values for a numeric variable A loop where the number of iterations is determined by a range of values for a numeric variable Syntax: Syntax: For controlVariable = initial To terminal statement(s) statement(s) Next controlVariable

52 52 Example Private Sub cmdDisplayTable_Click() Dim i As Integer Dim i As Integer ‘Display a table of the first 5 numbers and their squares ‘Display a table of the first 5 numbers and their squares For i = 1 To 5 For i = 1 To 5 picTable.Print i; i ^ 2 picTable.Print i; i ^ 2 Next i End Sub Initial Value Control variable Terminating value

53 53 Example Dim numVar As Integer For numVar = 1 To 5 Step 2 picOutput.Print numVar; picOutput.Print numVar; Next numVar Output: 1 3 5

54 54 When a For statement is encountered 1. The control variable is assigned the initial value. 2. After each loop iteration, the step value is added to the value of the control variable. (If there is no step value, 1 is added.) 3. Iteration continues until the terminating value is exceeded.

55 55 Rules for Using For... Next loop You should never modify the value of the loop control variable in the loop body. You should never modify the value of the loop control variable in the loop body. Each For loop must end with a Next statement. Each For loop must end with a Next statement.

56 56 Example Private Sub cmdDisplay_Click() Dim i As Integer Dim i As Integer For i = 1 To 10 For i = 1 To 10 picOutput.Print "*"; picOutput.Print "*"; Next i Next i End Sub Output: **********

57 57 Example Private Sub cmdDisplay_Click() Dim i As Integer, stars As Integer Dim i As Integer, stars As Integer stars = Val(InputBox("Row length (1-20) :")) stars = Val(InputBox("Row length (1-20) :")) For i = 1 To stars For i = 1 To stars picOutput.Print "*"; picOutput.Print "*"; Next i Next i End Sub

58 58 Example Dim numVar As Integer For numVar = 8 To 1 Step -2 picOutput.Print numVar; picOutput.Print numVar; Next numVar Output: 8 6 4 2

59 59

60 60 Private Sub Command1_Click() For x = 1 To 3 Step 0 Print x; Next Print " finally "; x End Sub Infinite loop

61 61 Nested Loops For outer = 1 To 4 For inner = 1 To 2 For inner = 1 To 2.... Next inner Next inner Next outer

62 62 Example: Display a 3x3 rectangle of stars Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer Dim i As Integer, j As Integer For i = 1 To 3 For i = 1 To 3 For j = 1 To 3 For j = 1 To 3 picOutput.Print "*"; picOutput.Print "*"; Next j Next j picOutput.Print picOutput.Print Next i Next i End Sub ***

63 63 Review For i= -5 To -1 Step - 2 picOutput.Print i; picOutput.Print i; Next i picOutput.Print i; For i= 1 To 5 Step 2 i=i+1 picOutput.Print i; Next i picOutput.Print i; -5 2 5 7

64 64 Review Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer Dim i As Integer, j As Integer For i = 1 To 3 For i = 1 To 3 For j = i To 3 For j = i To 3 picOutput.Print "*"; picOutput.Print "*"; Next j Next j picOutput.Print picOutput.Print Next i Next i End Sub Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer Dim i As Integer, j As Integer For i = 1 To 3 For i = 1 To 3 For j = 1 To i For j = 1 To i picOutput.Print "*"; picOutput.Print "*"; Next j Next j picOutput.Print picOutput.Print Next i Next i End Sub *** ** * ** ***

65 65 How many times the statement print "Good Luck" will be executed Dim i as integer, j as integer Dim i as integer, j as integer for i=0 To 3 for i=0 To 3 for j=0 To 0 for j=0 To 0 print "Good Luck" print "Good Luck" Next j Next j Next i Next i 4

66 66 What is the output of the following code Dim a as integer a=5 a=5 for a=3 To 1 step -1 for a=3 To 1 step -1 print a; print a; Next a Next a print a print a 3 2 1 0

67 67 which of the following statements is true about the following code For i = -7 To -3 pic1.Print i pic1.Print i Next i This code will print numbers from -7 to -3 starting from -3 This code will print numbers from -7 to -3 starting from -3 This code will print numbers from -7 to -3 starting from -7 This code will print numbers from -7 to -3 starting from -7 This code will print all numbers less than -7 This code will print all numbers less than -7 This code will print all numbers greater than -7 This code will print all numbers greater than -7

68 68 which of the following statements is true about the following code For i = 1 To 5.8 Step 1 pic1.Print i; pic1.Print i; Next I This code will print numbers from 1 to 6 This code will print numbers from 1 to 6 This code will print numbers from 1 to 5 This code will print numbers from 1 to 5 Run-time error Run-time error This code will print numbers from 1 to 5.8 This code will print numbers from 1 to 5.8

69 69 What is the output of the following code? Dim i As Integer Dim i As Integer For i = 9 To 7 For i = 9 To 7 Print i; Print i; Next i Next i Nothing will be printed Nothing will be printed

70 70 What is the output of the following code? Dim i as integer Dim i as integer i=4 i=4 for i=3 To 5 for i=3 To 5 print 2*i; print 2*i; Next i Next i print i print i 6 8 10 6

71 71 Dim a as integer Dim a as integer a=5 a=5 for a=3 To 1 step -1 for a=3 To 1 step -1 print a; print a; Next a Next a print a print a 3 2 1 0Example

72 72 Dim x as integer x = 0 for x=3 To 1 print x; print x; Next x print x 3Example

73 73 How many stars (*) are printed when the following code is executed? Dim i as Integer Dim i as Integer for i = 1 To 6 Step 1 for i = 1 To 6 Step 1 print "*" print "*" i=i+2 i=i+2 next i next i 2 stars

74 74 Boolean data type: a variable that stores only two values (True or False) Example Example


Download ppt "1 Chapter 6 Repetition. 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists."

Similar presentations


Ads by Google