Module 4 - Project |
Objectives: After studying the Module 1 Preparation Chip, you will be able to:
|
YOUR LESSON Introduction There are three fundamental logic structures in computer programming: the sequence structure, the decision structure, and the loop structure.
Any program can be written using a combination of these three structures. Loops can really increase the efficiency of a computer program as you will soon see. They will also make your programs somewhat shorter because you won't need so many variables. |
Looping A simple program is shown below. A little preview of Basic 2 is included--the RND function, which generates a random number. Programming languages have numerous built-in functions like RND to simplify programming tasks. The code that executes functions like RND is hidden from your view, but that's okay; right now, we are just interested in what it can do for us. Try typing in the following short program, run it and observe the results. In fact, run it 3 or 4 times to see if you get the same results each time. REM
Random Number Generator Did you get three random numbers each time you ran the program? Were the numbers different each time? Hopefully you answered yes to both questions. If not, please carefully review the code and try again.
Instead of just three random numbers, let's assume that we need to generate 10 different random numbers. We could simply add seven more LET statements to return the random numbers and seven more PRINT statements to print them out. That would work, but it seems like a lot of effort. Or, imagine that you had to modify this program to produce 1000 random numbers. That's a lot of coding! Here's an important lesson that you need to learn as a computer programmer. When you find yourself writing code that seems to be repetitive (that is, the same steps repeated over and over again), it is usually time to ask:
Programs that require repetitive tasks usually take advantage of the third programming logic structure: the looping structure. QBasic has three different methods of handling repetitive programming tasks. You will learn about the easiest one, the FOR-NEXT loop, in this lesson and the other two in Basic 2. The FOR-NEXT loop is perfect for handling our need to generate many random numbers. |
The FOR-NEXT Loop The FOR-NEXT loop is used when you need to execute a repetitive task a specific number of times. Consequently, a FOR-NEXT loop is also sometimes called a counting loop because a counter is used to control the exact number of times a loop is repeated. The syntax of a typical FOR-NEXT loop is shown below. (Don't worry, it makes more sense with an example, which you will see shortly.) FOR counter = start value TO terminal value STEP increment value some block of code would go here NEXT counter The FOR-NEXT loop is pretty easy to understand. The loop continues as long as the value of the counter is less than or equal to the terminal value.
Take a look at the following short example of a simple counting loop that prints a line of code. You know what to do next--type it in and watch it run! FOR
n
= 1 TO 10 STEP
1 You should begin developing the ability to look at a program segment like this and predict exactly what it will do.
Given just this little bit of information, you should be able to predict the output of this program. |
What
would be the output of the previous program?
|
Something that every programmer eventually does is put their name in the PRINT statement shown above (for example, PRINT "QB"). Go ahead and satisfy your urge to do that before we move on...nobody will know! As long as you know the start value, the terminal value, and the increment value, it is pretty easy to determine how many times a FOR-NEXT loop will count. The increment value can control a FOR-NEXT loop in two important ways.
Let's look at another quick example. Type in the program and see what happens. FOR
n
= 1 TO 10 STEP
1 This little program is not very impressive, but this type of looping structure is one of the workhorses of computer programming in any language. Experiment a little bit because inquiring minds will want to do the following. Start by making the change shown inside the parentheses below. Notice that the value(s) in bold type are suggestions of what to change. After you try the suggested changes, make some of your own.
You probably have many other questions about FOR-NEXT loops, so be sure to take some time to continue exploring on your own. After you finish, we will see how to make the loop count backwards. There are also
some things that can go wrong with FOR-NEXT
loops. Try changing the counter variable in the FOR
statement to x, but leave
it as n in the NEXT
statement. Also, try leaving out the entire FOR or the entire NEXT statement and see what happens. In an earlier example, you were able to leave out the STEP part of the FOR statement, and the loop still counted by 1s. What happens if you leave out the STEP part of the FOR statement when the loop is counting backwards? |
Loops can count forward and loops can count backwards. The direction depends on the start value, the terminal value, and the increment value. If the following conditions are met, the loop will count backwards.
That seems too easy, but it's true. Let's try it out. Make the start value 10, the terminal value 1, and the increment value -1 as shown in the following example. FOR
n
= 10 TO 1 STEP
-1 That's pretty neat. Now, ask yourself some questions similar to those in the previous example of the FOR-NEXT loop and learn some more interesting things! |
The FOR-NEXT Loop: Visualizing the Process Flowcharts are an easy way to visualize a computer algorithm. Most of the major keywords in QBasic have a flowchart symbol, and FOR-NEXT is no exception. The flowchart symbol used to create FOR-NEXT loops is shown to the right and indicates three important values:
|
A generalized example of a FOR-NEXT loop is shown below. Every FOR-NEXT loop follows this pattern. | |
Steps 2 and 3 are repeated until the value of the counter variable is greater than the terminal value. |
|
Make sure you can clearly visualize the operation of a FOR-NEXT loop before moving on to the following example. |
The FOR-NEXT Loop: A "Random" Example The FOR-NEXT
loop performs a simple, but very important programming task.
It allows repetition of a statement (or a block of statements) a specific
number of times. Assign a "correct" start value, terminal
value, and increment value and the loop
will repeat flawlessly. |
|
Take a look at the flowchart to the right. Does it look somewhat familiar? It should--it is a program to generate random numbers. Let's dissect how it works.
|
|
The algorithm illustrated
in the flowchart can be coded into the following simple program. |
|
REM
Program: Ten Random Numbers REM Programmer: Random Rudy REM DATE: 12/24/01 ' REM Set up the loop with to generate random numbers RANDOMIZE TIMER FOR N = 1 TO 10 STEP 1 LET newnumber = RND PRINT newnumber NEXT N END |
|
Do you recognize this program? It basically does the same thing as the program in this lesson's first example. (Instead of printing three random numbers, it prints ten.) Are you clear about how the program works? You can always email your instructor if you have questions! Can you modify the loop to produce 100 random numbers? How about 1000 random numbers? Feeling pretty confident about FOR-NEXT loops? Try the following challenge. |
YOUR ASSIGNMENT Let's do some simple math with two random numbers and a FOR-NEXT loop. Save your program as loop.bas.
|
1. Write a program that repeats 20 times. 2. Inside the loop,
assign random numbers to two variables
with the RND function. 3. Now perform simple math with the two random numbers. Add the two random numbers, subtract the two random numbers, multiply the two random numbers, and divide the two 4. Inside the loop, use a PRINT USING statement to print your answers in | Here is an image which includes headers of how a program might be displayed. To add the headers, you would need to add a PRINT statement before the LOOP starts. The PRINT USING statement for the numbers would be inside the LOOP just before NEXT N. (Each time you run the program, the numbers should change.)
|
Important Note: Remember that all random numbers are decimal numbers less than 1, so you will have to be careful with the format code in your PRINT USING statement. For example, if the computer chooses .934 and .123, when these are divided you will get a decimal number greater than 1 (7.593). That means if you use a format code.###, it will not print the answer correctly. When you run your program, you can immediately spot a format code problem because there will be a percent sign in front of the number (%7.593). All you have to do to fix this is add more # symbols in front of the decimal point in the format code (ex. ##.###). When you have completed this assignment, submit it through the In Box for Assignment 04.10. |
A Quick Review Before you move on to the next assignment, please make sure that you can do the following.
FOR-NEXT loops are extremely important in programming. They provide the key to writing much more complicated and sophisticated programs. |
After you complete this assignment, please continue to Assignment 04.11. Attention: Be sure to read the instructions carefully in the next page because 04.11B is a timed test. In addition, you will be taking the 04.11A Oral Review and should do it before you complete the 04.11B test. |