500 likes | 510 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 Output: 1 2 3 4 5 6 7 8 9 10 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 <> “ADMIN" 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 for Counter-controlled loop num = 1 Do While num <= 10 picOutput.Print num; num = num + 1 Loop Output: 1 2 3 4 5 6 7 8 9 10 Chapter 6 - Visual Basic Schneider
Example Dim x As Integer Dim s As Integer Do While x >= 2 x = x + 1 s = s + x Loop picOutput.Print x; s
Example Dim x As Integer Dim s As Integer Do x = x + 1 s = s + x Loop While x >= 2 picOutput.Print x; s
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 Dim x As Integer Dim s As Integer Do x = x + 1 s = s + x Loop Until x >= 2 picOutput.Print x; s
Example Dim x As Integer Dim s As Integer Do x = x + 1 s = s + x Loop Until x <= 2 picOutput.Print x; s
Example Dim x As Integer Dim s As Integer Do Until x >= 2 x = x + 1 s = s + x Loop picOutput.Print x; s
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
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
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 Chapter 6 - Visual Basic Schneider 24 num = 11 Do While num <= 10 picOutput.Print num; num = num + 1 Loop
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
EOF Function EOF(n) is True if the end of the file having reference number n has been reached. Otherwise, it is False Chapter 6 - Visual Basic Schneider
Dim count As Integer Open “DATA.TXT” For Input As #1 Do While NOT EOF(1) Input #1, x count= count + 1 s = s + x Loop Print count;s;x 5 150 50 Example will be true if the end of file has been reached, and false otherwise 27
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 COINS.TXT 50 10 5 25 Chapter 6 - Visual Basic Schneider
Compare Do While ……. Loop Do ……. Loop While Do ……. Loop Until Do Until ……. 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
which of the following statements is true about the following code For i = 1 To 5.8 Step 1 pic1.Print i; Next I • This code will print numbers from 1 to 6 • This code will print numbers from 1 to 5 • Run-time error • This code will print numbers from 1 to 5.8
What is the output of the following code? Dim i As Integer For i = 9 To 7 Print i; Next i Nothing will be printed
What is the output of the following code? Dim i as integer i=4 for i=3 To 5 print 2*i; Next i print i 6 8 10 6
Example Dim x as integer x = 0 for x=3 To 1 print x; Next x print x 3
How many stars (*) are printed when the following code is executed? Dim i as Integer for i = 1 To 6 Step 1 print "*" i=i+2 next i 2 stars
Private Sub Command1_Click() For x = 1 To 3 Step 0 Print x; Next Print " finally "; x End Sub Infinite loop
Nested Loops For outer = 1 To 4 Forinner = 1 To 2 .. .. Next inner Next outer Chapter 6 - Visual Basic Schneider
Example: Display a 3x3 rectangle of stars Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer For i = 1 To 3 For j = 1 To 3 picOutput.Print "*"; Next j picOutput.Print Next i End Sub *** *** *** Chapter 6 - Visual Basic Schneider
Review 2 5 7 Fori= 1 To 5 Step 2 i=i+1 picOutput.Printi; Next i picOutput.Printi; -5 Fori= -5 To -1 Step - 2 picOutput.Print i; Next i picOutput.Print i; Chapter 6 - Visual Basic Schneider
Review Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer For i = 1 To 3 For j = i To 3 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 3 For j = 1 To i picOutput.Print "*"; Next j picOutput.Print Next i End Sub Chapter 6 - Visual Basic Schneider
How many times the statement print "Good Luck" will be executed Dim i as integer, j as integer for i=0 To 3 for j=0 To 0 print "Good Luck" Next j Next i 4