350 likes | 598 Views
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.
E N D
Chapter 6 Repetition Chapter 6 - Visual Basic Schneider
Outline & Objectives • Loop Structure • Elements of a Loop Structure • Processing Lists of Data with Do Loops Chapter 6 - Visual Basic Schneider
Types of LOOP Structures • Do While ……. Loop • Do Until …… Loop • For …… Next loop Chapter 6 - Visual Basic Schneider
Basic Definition • Looping: the process of repeating a series of statements multiple times until a criteria is met Chapter 6 - Visual Basic Schneider
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 Chapter 6 - Visual Basic Schneider
The Do While ……. Loop Do Whilecondition is true statement(s) Loop Chapter 6 - Visual Basic Schneider
Flowchart for a Do While Loop Is the condition true No Yes Execute statements within the loop Execute statements that follow the loop Chapter 6 - Visual Basic Schneider
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 Chapter 6 - Visual Basic Schneider
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. Chapter 6 - Visual Basic Schneider
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. Chapter 6 - Visual Basic Schneider
Example of event-controlled loops passWord = "" Do While passWord <> "SHAZAM" passWord = UCase(InputBox("What is the password?")) Loop Chapter 6 - Visual Basic Schneider
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. Chapter 6 - Visual Basic Schneider
Example num = 1 Do While num <= 10 picOutput.Print num; num = num + 1 Loop Chapter 6 - Visual Basic Schneider
Do Until ……. Loop • Is executed until the condition becomes True • Any Do While…. Loop can be rewritten as a Do Until ….. Loop Chapter 6 - Visual Basic Schneider
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 Chapter 6 - Visual Basic Schneider
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 Chapter 6 - Visual Basic Schneider
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 Chapter 6 - Visual Basic Schneider
EOF Function • EOF(n) is True if the end of the file having reference number n has been reached. Otherwise, it is False • Example: Open “PHONE.TXT” for Input As #1 Do While Not EOF(1) Input #1, nom, phoneNum picOutput.Print nom, phoneNum Loop Close #1 Chapter 6 - Visual Basic Schneider
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. Chapter 6 - Visual Basic Schneider
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 Chapter 6 - Visual Basic Schneider
Compare • Do While ……. Loop • Do ……. Loop While • Do ……. Loop Until • Do Until ……. Loop Chapter 6 - Visual Basic Schneider
Review How many times will the following loops execute? num = 11 Do While num <= 10 picOutput.Print num; num = num + 1 Loop num = 11 Do picOutput.Print num; num = num + 1 • Loop until num <= 10 Chapter 6 - Visual Basic Schneider
Review Which loop is infinite? Do While i < 10 i = i + 1 Loop Do i = i + 1 Loop While i < 10 Do i = i + 10 Loop Until i < 10 Do Until i < 10 Loop Chapter 6 - Visual Basic Schneider
For … Next Loop • A loop where the number of iterations is determined by a range of values for a numeric variable • Syntax: For controlVariable = initialToterminal statement(s) NextcontrolVariable Chapter 6 - Visual Basic Schneider
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 Control variable Terminating value Initial Value Chapter 6 - Visual Basic Schneider
Example Dim numVar As Integer FornumVar= 1 To 5 Step 2 picOutput.Print numVar; Next numVar Output: 1 3 5 Chapter 6 - Visual Basic Schneider
When a For statement is encountered • The control variable is assigned the initial value. • 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.) • Iteration continues until the terminating value is exceeded. Chapter 6 - Visual Basic Schneider
Rules for Using For ... Next loop • You should never modify the value of the loop control variable in the loop body. • Each For loop must end with a Next statement. Chapter 6 - Visual Basic Schneider
Example Private Sub cmdDisplay_Click() Dim i As Integer For i = 1 To 10 picOutput.Print "*"; Next i End Sub Output: ********** Chapter 6 - Visual Basic Schneider
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 Chapter 6 - Visual Basic Schneider
Example Dim numVar As Integer For numVar = 8 To 1 Step -2 picOutput.Print numVar; Next numVar Output: 8 6 4 2 Chapter 6 - Visual Basic Schneider
Nested Loops For outer = 1 To 4 Forinner = 1 To 2 .. .. Next inner Next outer Chapter 6 - Visual Basic Schneider
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 Chapter 6 - Visual Basic Schneider
Review Fori= -5 To -1 Step - 2 picOutput.Print i; Next i picOutput.Print i; Fori= 1 To 5 Step 2 i=i+1 picOutput.Printi; Next i picOutput.Printi; Chapter 6 - Visual Basic Schneider
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 Fori = 1 To 10 For j = 1 Toi picOutput.Print "*"; Next j picOutput.Print Next i End Sub Chapter 6 - Visual Basic Schneider