1 / 74

Chapter 6

Chapter 6. Repetition. Outline & Objectives. Loop Structure Elements of a Loop Structure Processing Lists of Data with Do Loops. Types of LOOP Structures. Do While …… . Loop Do Until …… Loop For …… Next loop. Basic Definition.

Télécharger la présentation

Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 Repetition

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

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

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

  5. Basic Components of Loops 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

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

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

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

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

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

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

  12. Counter-controlled Loops 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. The counter is incremented (or decremented) by the same value during each repetition.

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

  14. Example

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

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

  17. Example

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

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

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

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

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

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

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

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

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

  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 <=3 and x >=10 Action(s) Loop Do Until Not(x <=3 and x>=10) Action(s) Loop Equivalents to

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

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

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

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

  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. An accumulator is a numeric variable that holds a sub-total during multiple passes through a loop.

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

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

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

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

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

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

  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 x = x - 1 Loop Print x 3

  40. Example • 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: 2,4,5,1

  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 If x < 7 Then Input #2, y End If Print x + y; Loop Until EOF(1) Or EOF(2) 3 11 9 8 12 5

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

  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. 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. What is the output of the following code? x = 0 y = 0 Do While x > 10 Or y < 10 y = y + x x = y Loop Print x; y Infinite loop

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

  47. What is the output of the following code? • 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 Loop Until EOF(1) Input #1, s Pic.Print s; Close #1 Run-time error

  48. What is the output of the following code? • 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 m = m * r Loop While EOF(1) Pic.Print m; Close #1 2

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

  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 Is > 5 output = output & "A" Case 3 To 6 output = output & "B" Case Else output = output & "C" End Select i = i +3 loop Print output CBA

More Related