Loops
Many machines do their work by using repeated motions, cycles. The engine in your car performs the same motions over and over again as it burns gasoline to provide power. Electric motors are similar; they convert electric power into spinning motion. Both of these machines are useful because they do the same things over and over and can keep going as long as we want.Computer programs, also, use cycles. Much of the usefulness of computer software comes from doing things in cycles. In programming, cycles are called loops. When a program has a loop in it, some statements are done over and over as long as is needed to get the work done. Most computer programs execute millions of program statements each time they are used. Most of these statements are the same statements being executed over and over many times. This chapter discusses loops in QBasic.
Chapter Topics:
- The
DO...LOOP - Using CONTROL-BREAK to exit a loop.
- The loop body.
- Indenting loop bodies.
- Sequential Execution and Looping Combined.
- The
SOUNDstatement.
QUESTION:
Think of some machines (mechanical or otherwise) that use cycles.Here is a program that uses a loop:
' Example of a loop ' DO PRINT "Type a number" INPUT NUMBER PRINT "6% of the number is", NUMBER * 0.06 LOOP ENDThe
DO statement marks the beginning of several statements that
will be done again and again.
The LOOP statement marks the end of those statements.
When this program runs this is what happens:
- The program starts with the
DOstatement. TheDOshows the beginning of the loop. - The first PRINT statement is executed and prints "Type a number".
- The INPUT statement is executed and gets a number to put in the
variable NUMBER.
- Say that the user typed in 100.
- The second PRINT statement prints "6% of the number is" and then computes
and prints six percent of NUMBER.
- If the NUMBER is 100, the program will print "6% of the number is 6"
- The
LOOPstatement marks the end of the loop.
DO and LOOP have each
executed one time.
They are about to execute a second time.
QUESTION:
What does the monitor look like at this time?Here is the example program, again:
' Example of a loop DO PRINT "Type a number" INPUT NUMBER PRINT "6% of the number is", NUMBER * 0.06 LOOP ENDThe statements between
DO and LOOP
have each been executed once.
The variable NUMBER has the value 100 in it:
| 100 |
DO:
- For a second time, the
PRINTstatement prints "Type a number". - For a second time, the
INPUTstatement gets a number to put in the variableNUMBER.- Say that the user typed in 50. The number 50 will replace what was previously in the variable.
- For a second time, the second
PRINTstatement prints "6% of the number is" and then computes and prints 50 * 0.06. - The
LOOPstatement marks the end of the loop.
NUMBER now has the new value of 50 in it:
| 50 |
Type a number ? 100 6% of the number is 6 Type a number ? 50 6% of the number is 3
QUESTION:
What do you suppose happens next. Have the user enter the number 200.Now the monitor shows:
Type a number ? 100 6% of the number is 6 Type a number ? 50 6% of the number is 3 Type a number ? 200 6% of the number is 12The variable
NUMBER has the new value of 200 in it:
| 200 |
DO ... LOOP again and again.
If you run this program,
you will have to stop it,
somehow.
To stop the program, hit CONTROL-BREAK.
(Hold down the key marked "Ctrl" then tap the key marked "Break.")
QUESTION:
Why not just let the program run until it ends?Look again at the program:
' Example of a loop DO PRINT "Type a number" INPUT NUMBER PRINT "6% of the number is", NUMBER * 0.06 LOOP ENDThe
DO and the LOOP are matched like brackets ( ) or [ ].
The statements inside of them will be
done over and over,
starting with the first enclosed statement and going on in sequence.
You can have as many statements as you need between DO
and LOOP:
DO ' start of loop
' first statement to be repeated
' second statement to be repeated
' third statement to be repeated
'
'
' last statement to be repeated
LOOP ' end of loop
END
QUESTION:
Do you see anything in theDO or the LOOP
statement that says how many times the
statements between them are to be repeated?The statements between the
DO and LOOP are called
the body of the loop.
So you can say that for the example program, the
loop body is repeated endlessly.
Soon you will learn how to write loops that do not repeat endlessly. Sometimes, however, endlessly repeating loops are useful.
A Story Problem
Say that you are in charge of putting price tags on new merchandise in a clothing store. A new shipment of several hundred items has just arrived. You have a list that tells you the wholesale price of each item. The markup in your store is 50 percent, so the price tag should be 1.5 times the wholesale price. You would like a program that asks you for the wholesale price of an item and then prints the retail price, and keeps doing that over and over (since you have hundreds of items).
No hay comentarios:
Publicar un comentario