Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.

Similar presentations


Presentation on theme: "Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition."— Presentation transcript:

1

2 Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition

3 Chapter 6 - Visual Basic Schneider2 Outline & Objectives u Loop Structure u Elements of a Loop Structure u Processing Lists of Data with Do Loops

4 Chapter 6 - Visual Basic Schneider3 Types of LOOP Structures u Do While ……. Loop u Do Until …… Loop u For …… Next loop

5 Chapter 6 - Visual Basic Schneider4 Basic Definition u Looping : the process of repeating a series of statements as many times as needed. u Looping also called iteration.

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

7 Chapter 6 - Visual Basic Schneider6 The Do While ……. Loop Do While condition is true statement(s) Loop

8 Chapter 6 - Visual Basic Schneider7 Flowchart for a Do While Loop Is the condition true Execute statements within the loop Execute statements that follow the loop Yes No

9 Chapter 6 - Visual Basic Schneider8 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 While num <= 10 picNumbers.Print num; num = num + 1 Loop End Sub

10 Chapter 6 - Visual Basic Schneider9 The Do While ……. Loop u Is executed as long as the condition is True. u If condition is False then the next statement after the Loop is executed.

11 Chapter 6 - Visual Basic Schneider10 Controlling Loops u Methods of controlling loops: u Counter-controlled loops u repeat a specific number of times u Event-controlled loops u repeat until something happens in the loop body to change the value of loop control variable.

12 Chapter 6 - Visual Basic Schneider11 Example of event-controlled loops passWord = "" Do While passWord <> "SHAZAM" passWord = UCase(InputBox("What is the password?")) Loop

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

14 Chapter 6 - Visual Basic Schneider13 Example num = 1 Do While num <= 10 picOutput.Print num; num = num + 1 Loop

15 Chapter 6 - Visual Basic Schneider14 Do Until ……. Loop u Is executed until the condition becomes True u Any Do While…. Loop can be rewritten as a Do Until ….. Loop

16 Chapter 6 - Visual Basic Schneider15 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 Until passWord = "SHAZAM" End If Open txtName.Text For Input As #1 Input #1, info picItem.Cls picItem.Print info Close #1 End Sub

17 Chapter 6 - Visual Basic Schneider16 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 Until amt <= 0 picResult.Print "It takes"; yrs; "years to deplete the account." End Sub

18 Chapter 6 - Visual Basic Schneider17 Comparing While… and Until Loops u The Do While … Loop executes while the condition is true u The Do Until….. Loop executes until the condition is true u Both can be used to create any type of loop

19 Chapter 6 - Visual Basic Schneider18 Processing List of Data with Loops u EOF Function u The EOF function tells us if we have read to the end of a file. u Checks for the end-of-file marker.

20 Chapter 6 - Visual Basic Schneider19 EOF Function u EOF(n) where n is the reference number for the file. u If there are more records to be read, EOF is False. u When the end-of-file marker is reached; EOF becomes True.

21 Chapter 6 - Visual Basic Schneider20 Example of EOF Open “Phone.txt” for Input As #1 Do While Not EOF(1) Input #1, nom, phoneNum picOutput.Print nom, phoneNum Loop Close #1

22 Chapter 6 - Visual Basic Schneider21 Counters and Accumulators u A Counter is a numeric variable that keeps track of the number of items that have been processed in the loop. u An Accumulator is a numeric variable that totals numbers.

23 Chapter 6 - Visual Basic Schneider22 Example: Counter & Accumulator Private Sub cmdAnalyze_Click() Dim numCoins As Integer, sum As Single, 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." Close #1 End Sub

24 Chapter 6 - Visual Basic Schneider23 Example of a Data File 1, 1, 5, 10, 10, 25

25 Chapter 6 - Visual Basic Schneider24 Output for the Previous Example The value of the 6 coins is 52 cents.

26 Chapter 6 - Visual Basic Schneider25 For ……... Next Loop u Is used to create a counting loop. u Loop control variable has an initial value. u Loop control variable has a terminating value. u Loop control variable has a Step value.

27 Chapter 6 - Visual Basic Schneider26 Syntax of For…… Next Loop For loop-control-variable = initial To terminal statement(s) Next loop-control-variable

28 Chapter 6 - Visual Basic Schneider27 Example ( display a table of the first 5 numbers and their square) Private Sub cmdDisplayTable_Click() Dim i As Integer ‘Display a table of the first 5 numbers and their sqares picTable.Cls For i = 1 To 5 picTable.Print i; i ^ 2 Next i End Sub Initial Value Loop Control variable Terminating value

29 Chapter 6 - Visual Basic Schneider28 Example ( step value of 2) For counter = 1 To 5 Step 2 picOutput.Print counter Next counter

30 Chapter 6 - Visual Basic Schneider29 When the For statement is first encountered This explanation assumes a positive step value u The initial, terminal, and (if given ) step value expression are evaluated. u The loop control variable is assigned to the initial value.

31 Chapter 6 - Visual Basic Schneider30 This explanation assumes a positive step value  The value of the loop control variable is tested against the terminal value. u If the loop control variable is less than or equal to the terminal value, the loop body is executed. u If the loop control variable is greater than the terminal value, the loop body is skipped, and control passes to the first statement following the Next.

32 Chapter 6 - Visual Basic Schneider31 When the Next statement is encountered u The step value is added to the loop control variable. If there is no step value, +1 is added. u A check is performed to determine if the value of the loop control variable exceeds the terminal value.

33 Chapter 6 - Visual Basic Schneider32 Continued u If the loop control variable is less than or equal to the terminal value, control transfers back to the statement after the For statement and the loop is repeated. u Otherwise, the loop is exited, and execution continues with the statement following the Next.

34 Chapter 6 - Visual Basic Schneider33 Rules for Using For... Next loop u The initial, terminal, and step values cannot be modified in the loop body. u You should never modify the value of the loop control variable in the loop body. u Each For statement must end with a Next statement.

35 Chapter 6 - Visual Basic Schneider34 Example (display 10 stars) Private Sub cmdDisplay_Click() Dim i As Integer ' Display a row of ten stars picOutput.Cls For i = 1 To 10 picOutput.Print "*"; Next i End Sub

36 Chapter 6 - Visual Basic Schneider35 Example (request a number and display a row of that many stars) Private Sub cmdDisplay_Click() Dim i As Integer, stars As Integer ' Display a row of stars picOutput.Cls stars = Val(InputBox("Row length (1-20) : ")) For i = 1 To stars picOutput.Print "*"; Next i End Sub

37 Chapter 6 - Visual Basic Schneider36 Example (the step value is negative) For Counter 8 To 1 Step -2 picOutput.Print Counter Next Counter

38 Chapter 6 - Visual Basic Schneider37 Nested Loops For Outer = 1 To 4 For Inner = 1 To 2.. Next Inner Next Outer

39 Chapter 6 - Visual Basic Schneider38 Example (display a 10 by 10 array of stars) Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer ' Display 10 x 10 square of stars For i = 1 To 10 For j = 1 To 10 picOutput.Print "*"; Next j picOutput.Print Next i End Sub

40 Chapter 6 - Visual Basic Schneider39 Guidelines for Choosing a Loop: u If counting loop, use a For…… Next Loop. u If trailer-values and body is executed at least once, use Do Until….. Loop. u If trailer-value and nothing is known about the first execution, use Do While…. Loop. u If you are not sure use Do While….. Loop.


Download ppt "Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition."

Similar presentations


Ads by Google