Logistics
- Roll
- Questions? Comments? Wonderings?
Review Unit IV Competency Demo
- Review each item
- Respond to questions
Repetition Concepts
We are getting close to the end of the "basics of computing"—actions, sequence, selection, iteration, and modularization. We keep finding out about more things we can use in the actions (controls and properties) and seeing ways to organize the actions. Again, the basics are actions and organizing the actions. Remember, the hardest part of programming is determining precisely what we want to accomplish.
Repetition is merely doing some task over and over again. For example:
- flip a coin 10 times
- process individual characters in a piece of text
- search items in a listbox (to find a particular value)
- process all the lines in a file
- repeatedly present arithmetic practice problems
Note that Chapter 14 of the text provides information about looping. It shows many different versions of loops. For us, it seems reasonable to concentrate on three of them. The For ... Next
loop, the Do While...
loop, and the Do Until ...
loop.
Show equivalent examples that count to 10.
Planning and Implementing repetition
We must remember that the programming fundamental of sequence, must be considered for iteration to work correctly, you must carefully determine and place the actions necessary to accomplish your task. Some actions will need to be done before the repetition, some during the repetition, and some after the repetition. If you do not put all the actions in the right place, the overall result will not be correct.
Key idea # 1 in repetition—know precisely what you want to repeat
Clearly you have to tell the computer what you want it to repeat, DUH! Remember, the computer does what it is told to do—exactly what it is told and only what it is told. Be careful that you put only the actions you want repeated inside the loop. Some things should go before or after the loop.
Also, if something has to be done to be ready for the next repetition your code must include that. Sometimes (as in a For
loop) getting ready for the next may be taken care of by the loop code itself. Even For
loops, however, may still have to include actions to prepare for the next set of actions.
Key idea # 2 in repetition—determine when to stop repeating
Another critical consideration in iteration is determining when to stop or how many times to repeat something. If you want to process the items in a collection or lines in a file, you just keep processing until you've processed them all. Sometimes you can do that by using a counting loop and sometimes you don't know or cannot tell how many items there are and so must devise some sort of conditional expression to tell you when you are done. That leads to our next key idea.
Key idea # 3 in repetition—initialize
Your code has to include any and all the actions that need to come before the loop (ofter referred to as initialization). If you are counting things, the counter needs to be set to 0. If you are searching for a value, you have to get the value to be searched for. If you are checking for errors you will need to set an error flag to True
or False
before you start.
Key idea # 4 in repetition—after the loop
Finally, there is almost always some action that needs to be done after the loop. Reporting the result from the repetition is usually called for. Sometimes repetition had two tests for determining when to stop, e.g., look at the items to see if a particular value is included. In this case we would want to stop when we found it or when we had looked at all the values. After the loop, we know we stopped but we don't know why we stopped. We will need to test (using an If
) to see why we stopped and take the appropriate action.
Examples
We are going to show some examples that illustrate the key ideas and the Visual Basic code for iteration. We will use the idea of coin flipping simulations in all of them. The plan for each is to:
- analyze the problem, making sure we understand it
- determine the actions that need to be repeated
- decide how we will to control the repetition
- determine the actions needed before the loop
- determine the actions needed after the loop
- code the solution
- test and debug the solution, making sure it seems/is correct
The examples are:
o- flip a coin 1,000 times counting the numbers of heads and tails
We will need counts for heads and tails and a mechanism for deciding heads or tails. (I will just check the value produced by
Rnd()
.) We also need to remember toRandomize
. An algorithm using a counting loop is:headCount <— 0 tailCount <— 0 for count <— 1 to 1000 if Rnd() < 0.5 headCount <— headCount + 1 else tailCount <— tailCount + 1 report headCount and tailCount
Code & test the algorithm
Comments/Questions/Wonderings
- flip a coin the number of times desired by the user, counting the numbers of heads and tails
This is almost identical to the above problem. We have to get a number-of-repetitions value from the user and (input box?) and change loop parameters to one with a variable. An algorithm is:
headCount <— 0 tailCount <— 0 repsLimit <— userInput for count <— 1 to repsLimit if Rnd() < 0.5 headCount <— headCount + 1 else tailCount <— tailCount + 1 report headCount and tailCount
Code & test the algorithm
Comments/Questions/Wonderings
- flip a coin 1,000 times, counting the numbers of heads and tails and record the longest sequence of heads in a row and the longest sequence of tails in a row
This introduces two new things that have nothing to do with the repetition—keeping track of the flips in a row and determining the longest such sequence.
- flips in a row
To keep track of flips in a row we need a counter and a variable that will let us know what the last flip was. If we know what the last flip was and what the current flip is, we can decide whether the current roll continues a streak. Algorithmically, we would have something like:
conseqHeads <— "" ' Happens before the loop ... if lastFlip = heads ' Happens inside the loop conseqHeads <— conseqHeads + 1 else conseqHeads <— 1 lastFlip <— heads
But, wait! What value does
lastFlip
have? orconseqHeads
or (none). And, If the current flip is a heads then the number of tails in a row has to go back to zero. So, we need to add a couple actions, i.e.,conseqHeads <— "" lastFlip <— "" ' Happens before the loop ... if current-flip-is-heads ' happens inside the loop if lastFlip = heads conseqHeads <— conseqHeads + 1 else conseqHeads <— 1 lastFlip <— heads conseqTails <— 0
A similar thing would happen for tails.
- longest run of heads (or tails)
If we know the current number of heads or tails in a row, we just need to test against the longest run we've seen so far to check to see if the current count is greater than the longest. But, we have to initialize that value.
Ask: When we start the program, what should be the value of the longest run of flips be?
An algorithm for it is shown below.
longHeadsRun <— 0 ' This happens before the loop! ... if current-flip-is-heads ' this happens inside the loop. if conseqHeads > longHeadsRun longHeadsRun <— conseqHeads
A similar thing would happen for tails.
Now all we have to do it put it all together!
Code & test the algorithm
Comments/Questions/Wonderings
- flips in a row
- flip a coin until you get
n
in a row (get the value ofn
from the user)Now that we know how to keep track of runs we can try an indefinite loop. We keep repeating until an end condition is met or as long as a continue condition is true. An algorithm is:
' remember to do initialization repsGoal <— userInput repeat until conseqHeads >= repsGoal Or conseqTails >= repsGoal ' code similar to above report headCount and tailCount
Code & test the algorithm
Comments/Questions/Wonderings
Next Time
- Questions & work on Unit V