Lesson 7 Introduction to Looping
So far our programs have just run their course from start to end in a straight top to bottom way. We take in values, do calculations then fire off some output before it's done. Once it ends we must run it all over again to do anything else. Now, we are going to let our user determine when the program ends. We do this with a logic structure known as the Do...While structure.
A looping decision structure is used when we don't know how many values we will be bringing into our program or we want to repeat an action a certain amount of times. We also call this "iterations" but don't let that word throw you, we simple mean looping.
The most simple way to loop is a called the Do...While Loop. Simply put, it means that we want to repeat certain sections of code until a certain condition evaluates to "false" then we stop doing the that repetitive code and finish out our program. Another way of putting it is that we keep DOing a part of our code WHILE a condition evaluates as true.
You want to be careful coding here. If you write your code so that the condition never evaluates as "false" then your program will go on forever. We call that being "locked" in an infinite loop and you have to shut down your program or even your computer. For that reason I highly recommend that you save your work constantly, particularly when you are ready to run the code. In some cases you might have to shut down your computer if your program doesn't allow for the program to be shut down. In my years of programming I have "locked" myself in an infinite loop many times because I was rushing to get a program completed. Believe me, it's only funny the first time :-).
I recommend you open visual logic and work through each of these lesson below so you can see how they work.
1. Let's look at a very simple loop that simply outputs that a loop is happening then stops:
![]() |
Input: Keepriding (Prompt "Would you
like a ride?" Do While keepingride = "yes" (if they said yes in the above input box then we step into the loop) Output: Weee, I'm looping (the only thing the loop does) Wait! The loop never stops! That is because we have no way of changing what is stored in keepriding to anything but "yes". We need to add an inputbox under the output to ask if they want to ride. Press the stop button in visual logic (square) |
2. Now we'll add the inputbox so that our loop will stop:
![]()
Code: |
Same as the above flowchart except new we
have a new inputbox under the output in the loop. This will ask
the question "Do you want to ride again?" If the user answers "yes" then they go around again. If they say "no" then when the computer evaluates [keepriding = "yes"], keepriding has "no" stored in it so that the expression will evaluate as false and not step into the loop. Instead it would head down the "false" side and do the final output "we are done looping new
In our code we are not going to output in our loop by using document.write. Instead we are going to use a message box. This is a function that creates a dialog box that outputs a value. msgbox(whatever you want to output) in this case we will be
outputting just text: Do While is similar to an IF keyword in that it required a test after those words that it can evaluate as true or false Loop Similar to an End If or End Select it marks the end of the decision. |
3. We are going to add some functionality to the program now.
The user says they want to be told how many times they have looped every time
they go around and then be told the total number of loops when the ride is over.
For that we need to set a "counter". This is an assignment that will
increment one time every time we go through the loop. The syntax is
counter = counter + 1. What you are saying is "I have a place in
memory called counter and I want to assign to that place in memory what is
returned when I take what is presently in the place of memory called counter and
add one to it." Let's see how this looks:
See a similar video
|
Code: <script language=vbscript> option explicit dim keepriding, counter keepriding = inputbox("Would you like to ride?") do while keepriding = "yes" counter = counter + 1 |
Our flowchart changed in three ways: 1. We added the counter (explained above) 2. We modified the output in the loop so we can tell them how many loops they have done. (You have gone around " & counter & " times" 3. We modified the final output the same way.
We add counter to the dim line
add counter = counter + 1 add the counter to the output. |
4. We want to add more functionality to the ride. Every
time they go around we want to ask them how much they would be willing to pay to
go around during that loop. The user wants us to add to the output so that
we will be telling the rider how much they owe (cumulative value) each time they
go through the loop and then again when they are done. For this we need a
new kind of assignment that is called an accumulator. Many times you
will find that we must accumulate values as we are looping. When we are
required to do this we must use an accumulator. In this case we will be
using totalrideamount = totalrideamount + rideamount where ride amount is
what the rider said they would be willing to pay for the ride. We must add
an inputbox to the flowchart where we ask the rider "how much will you page for
this ride?" and store that value in rideamount.
See a similar video
![]() Code: <script language=vbscript> |
Notice in the code we had to use the Cint() function. Remember we learned in lesson 3 that when you try to add two places in memory when those places were populated with the result of inputboxes then they will be joined not added. When we use the Cint() function we are taking the text characters inside the parentheses and changing them into binary numbers so they can be added. |