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 multiple times until a criteria is met

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 EOF Function u EOF(n) is True if the end of the file having reference number n has been reached. Otherwise, it is False u Example: Open “PHONE.TXT” for Input As #1 Do While Not EOF(1) Input #1, nom, phoneNum picOutput.Print nom, phoneNum Loop Close #1

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

21 Chapter 6 - Visual Basic Schneider20 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." End Sub

22 Compare u Do While ……. Loop u Do ……. Loop While u Do ……. Loop Until u Do Until ……. Loop Chapter 6 - Visual Basic Schneider21

23 Review num = 11 Do While num <= 10 picOutput.Print num; num = num + 1 Loop Chapter 6 - Visual Basic Schneider22 num = 11 Do picOutput.Print num; num = num + 1 Loop until num <= 10 How many times will the following loops execute?

24 Review Do While i < 10 i = i + 1 Loop Chapter 6 - Visual Basic Schneider23 Do i = i + 1 Loop While i < 10 Which loop is infinite? Do Until i < 10 Loop Do i = i + 10 Loop Until i < 10

25 Chapter 6 - Visual Basic Schneider24 For … Next Loop u A loop where the number of iterations is determined by a range of values for a numeric variable u Syntax: For controlVariable = initial To terminal statement(s) Next controlVariable

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

27 Chapter 6 - Visual Basic Schneider26 Example Dim numVar As Integer For numVar = 1 To 5 Step 2 picOutput.Print numVar; Next numVar Output: 1 3 5

28 Chapter 6 - Visual Basic Schneider27 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.

29 Chapter 6 - Visual Basic Schneider28 Rules for Using For... Next loop u You should never modify the value of the loop control variable in the loop body. u Each For loop must end with a Next statement.

30 Chapter 6 - Visual Basic Schneider29 Example Private Sub cmdDisplay_Click() Dim i As Integer For i = 1 To 10 picOutput.Print "*"; Next i End Sub Output: **********

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

32 Chapter 6 - Visual Basic Schneider31 Example Dim numVar As Integer For numVar = 8 To 1 Step -2 picOutput.Print numVar; Next numVar Output: 8 6 4 2

33 Chapter 6 - Visual Basic Schneider32 Nested Loops For outer = 1 To 4 For inner = 1 To 2.. Next inner Next outer

34 Chapter 6 - Visual Basic Schneider33 Example: Display a 10x10 rectangle of stars Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer For i = 1 To 10 For j = 1 To 10 picOutput.Print "*"; Next j picOutput.Print Next i End Sub

35 Review For i= -5 To -1 Step - 2 picOutput.Print i; Next i picOutput.Print i; Chapter 6 - Visual Basic Schneider34 For i= 1 To 5 Step 2 i=i+1 picOutput.Print i; Next i picOutput.Print i;

36 Chapter 6 - Visual Basic Schneider35 Review Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer For i = 1 To 10 For j = i To 10 picOutput.Print "*"; Next j picOutput.Print Next i End Sub Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer For i = 1 To 10 For j = 1 To i picOutput.Print "*"; Next j picOutput.Print Next i End Sub


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

Similar presentations


Ads by Google