types of loop structures do while ……. loop do until …… loop for …… next loop
Post on 20-Dec-2015
271 views
TRANSCRIPT
![Page 1: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/1.jpg)
Types of LOOP Structures
Do While ……. Loop Do Until …… Loop For …… Next loop
![Page 2: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/2.jpg)
Basic Definition
Looping: the process of repeating a series of statements multiple times until a criteria is met
![Page 3: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/3.jpg)
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
![Page 4: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/4.jpg)
The Do While ……. Loop
Do While condition is true
statement(s)
Loop
![Page 5: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/5.jpg)
Flowchart for a Do While Loop
Is the condition true
Execute statementswithin
the loop
Execute statementsthat follow the loop
Yes
No
![Page 6: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/6.jpg)
Example
What is the output?
![Page 7: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/7.jpg)
Output (Displays the numbers from 1 through 10)
![Page 8: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/8.jpg)
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.
![Page 9: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/9.jpg)
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.
![Page 10: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/10.jpg)
Example of event-controlled loops
strPassWord = ""
Do While strPassWord <> “BIS"
strPassWord = UCase(InputBox("What is the password?"))
Loop
![Page 11: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/11.jpg)
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.
![Page 12: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/12.jpg)
Example
intNum = 1
Do While intNum <= 10
picOutput.Print num;
intNum = intNum + 1
Loop
![Page 13: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/13.jpg)
Do Until ……. Loop
Is executed until the condition becomes True
Any Do While…. Loop can be rewritten as a Do Until ….. Loop
![Page 14: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/14.jpg)
$15000 is deposited at 5% interest. If $1000 years to deplete a saving account) is withdrawn at the end of each year, how many years will it take to empty the account?
![Page 15: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/15.jpg)
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
![Page 16: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/16.jpg)
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 #1Do While Not EOF(1) Input #1, nom, phoneNum picOutput.Print nom, phoneNumLoopClose #1
![Page 17: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/17.jpg)
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.
![Page 18: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/18.jpg)
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
![Page 19: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/19.jpg)
For … Next Loop
A loop where the number of iterations is determined by a range of values for a numeric variable
Syntax:
For controlVariable = initial To terminal
statement(s)
Next controlVariable
![Page 20: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/20.jpg)
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
![Page 21: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/21.jpg)
Example
Dim numVar As Integer
For numVar = 1 To 5 Step 2
picOutput.Print numVar;
Next numVar
Output: 1 3 5
![Page 22: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/22.jpg)
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.
![Page 23: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/23.jpg)
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.
![Page 24: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/24.jpg)
Example
Private Sub cmdDisplay_Click()
Dim i As Integer
For i = 1 To 10
picOutput.Print "*";
Next i
End Sub
Output: **********
![Page 25: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/25.jpg)
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 iEnd Sub
![Page 26: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/26.jpg)
Example
Dim numVar As Integer
For numVar = 8 To 1 Step -2
picOutput.Print numVar;
Next numVar
Output: 8 6 4 2
![Page 27: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/27.jpg)
Nested Loops
For outer = 1 To 4
For inner = 1 To 2
..
..
Next inner
Next outer
![Page 28: Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop](https://reader033.vdocuments.site/reader033/viewer/2022061607/56649d455503460f94a22081/html5/thumbnails/28.jpg)
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