FOR Loops
This chapter discusses another way of doing loops: theFOR statement.
FOR statements are especially convenient for implementing
counting loops.
DO WHILE loops can be used for the same purpose,
but are less convenient.
Chapter Topics:
- The three parts of a loop.
- The
FORstatement. - Syntax of the
FORstatement. - Making tables of values using the
FORstatement. - Common bugs with
FORstatements. - Adding up a list of numbers.
QUESTION:
(Review:) What will this loop print out?' Counting Loop ' LET COUNT = 0 'Statement 1 ' DO WHILE COUNT <= 2 'Statement 2 PRINT COUNT 'Statement 3 LET COUNT = COUNT + 1 'Statement 4 LOOP END
That was an easy question. But there is a good chance that you got it wrong. This is because there are three things you have to think about to get the correct answer:
- The starting value of the counter.
- How the counter is tested in the
WHILE. - How the counter is incremented in the body of the loop.
' Counting Loop ' LET COUNT = 0 'Statement 1: start the counter ' DO WHILE COUNT <= 2 'Statement 2: test the counter PRINT COUNT 'Statement 3 LET COUNT = COUNT + 1 'Statement 4: increment the counter LOOP END But all you want to do (in this program) is to count 0, 1, 2.
QUESTION:
Wouldn't it be nice to have an easy way to say "count up from zero to two" (or whatever range you wanted)?Here is a program that does the same thing as the previous program, but uses a
FOR loop to say "count
up from zero to two."
' Counting Loop---using a FOR ' FOR COUNT = 0 TO 2 'Statement 1 PRINT COUNT 'Statement 2 NEXT COUNT 'Statement 3 ENDStatement 1 does three things:
- It uses the variable
COUNTfor counting. - It starts the count at zero.
- It lets the count go up to the value 2.
QUESTION:
What statement is in the body of the loop?It would be OK to say that statement 3 is also in the loop body, but really it is a "bracket" that shows where the loop body ends (like the
LOOP statement in a
DO WHILE loop.)
' Counting Loop---using a FOR ' FOR COUNT = 0 TO 2 'Statement 1 PRINT COUNT 'Statement 2 NEXT COUNT 'Statement 3 ENDAs always, use indenting to show the statements that are contained between two brackets. The
NEXT COUNTCOUNT
changes from its current value to the next value.
This change is done automatically.
You should not put in a statement to do it.
QUESTION:
What is the next value in this loop after 0?Here is a diagram that shows how the
COUNT changes as the program runs:
' Counting Loop---using a FOR ' ' values of COUNT changing over time ---> ' FOR COUNT = 0 TO 2 0 1 2 3 PRINT COUNT 0 1 2 NEXT COUNT 1 2 3 END
COUNTstarts out at 0, stays 0 for thePRINTstatement, then changes to 1 at theNEXT COUNTstatement.- Execution returns to the top of the loop
- AT the top of the loop
COUNTis 1, it stays 1 for thePRINTstatement, then changes to 2 at theNEXT COUNTstatement. - Execution returns to the top of the loop
- At the top of the loop (again)
COUNTis 2, it stays 2 for thePRINTstatement, then changes to 3 at theNEXT COUNTstatement. - Execution returns to the top of the loop
- Now at the top of the loop it is 3, but 3 is greater than the ending count 2, so the loop is exited.
QUESTION:
What does the following program print?' Counting Loop FOR COUNT = 1 TO 4 PRINT COUNT NEXT COUNT END
You can use any variable for the counter variable in a FOR loop. But usually you should dedicate one variable to the task of counting, and use other variables for what ever other purposes you have. The variable that is dedicated to keeping the count is sometimes called a loop control variable. It is an ordinary numeric variable. There is nothing special about it except in how it is used.
Here is a program that counts from 5 to 10. The loop control variable is named
I.
' Counting Loop---count from 5 to 10 ' FOR I = 5 TO 10 PRINT I; ' notice the semicolon, but don't worry about it just now NEXT I END
QUESTION:
What is printed by the above program?In general, the
FOR statement looks like this:
FOR counter = startingValue TO endingValue loopBody NEXT counterRemember that the term syntax means "the grammar of a program." Syntax tells you how a program (or part of a program) should look. Here are some rules about
FOR statements:
- The counter is a numeric variable.
- You can use counter in the loopBody,
but don't change it (this happens automatically with the
NEXT.) - startingValue and endingValue can be arithmetic expressions or variables.
- loopBody can be any number of statements.
- NEXT counter shows the end of the loop body, and shows where counter will change to its next value.
' Counting Loop ' LET START = 1 LET FINISH = 5 FOR I = START TO FINISH PRINT I; NEXT I END
QUESTION:
Find at least 4 rules from the above list that this program illustrates.The program is correct, so all the syntax notes are followed. The loop body in this program only has one statement.
- counter is a numeric variable.
- You can use counter in the loopBody,
but don't change it (this happens automatically with the
NEXT.) - startingValue and endingValue can be arithmetic expressions or variables.
- loopBody can be any number of statements.
- NEXT counter shows the end of the loop body.
' Counting Loop ' LET START = 1 ' Note 3 LET FINISH = 5 ' Note 3 FOR I = START TO FINISH ' Notes 1 and 3 PRINT I; ' Notes 2 and 4 NEXT I ' Note 5 ENDThis program prints the integers from one to five, all on one line.
QUESTION:
Does syntax tell you how theFOR statment works
(not just how it looks)?
Here is the general form of the FOR (again):
FOR counter = startingValue TO endingValue loopBody NEXT counterSome rules about how the
FOR statement works:
- The first time loopBody is executed, counter has the value startingValue.
- Each time control hits the NEXT counter statement, counter is increased by 1, and control is sent back to the top.
- The final time loopBody is executed, counter has the value endingValue.
- If startingValue is greater than endingValue, the loopBody will not be executed even once.
' Counting Loop ' LET START = 7 LET FINISH = 5 FOR I = START TO FINISH PRINT I; NEXT I END
QUESTION:
- Is the program's syntax correct?
- What does the program print out?
FOR statement is
7.
This is OK so far as syntax goes.
The program asks to start I out at 7 and count up to 5.
Since I is already larger than 5, the loop is just skipped.
Here is a program where this behavior makes better sense:
' Counting Loop with user input ' PRINT "What start do you want" INPUT START ' PRINT "What finish do you want" INPUT FINISH ' FOR COUNT = START TO FINISH PRINT COUNT; NEXT COUNT ' ENDSometimes the data input from a user means that only part of a program should execute. It is nice that this happens automatically without additional statements.
QUESTION:
- If the user types 10 for
STARTand 14 forFINISHwhat will the program print? - If the user types 5 for
STARTand 3 forFINISHwhat will the program print?
Number Square
0 0
1 1
2 4
3 9
. . .
25 625
The program needs to do two things: (i) count from zero to 25, and,
(ii) calculate the square of each number.
This sounds like a good place for a FOR loop.
Here is a start on the program:
' Make a table of squares from 0 to 25 ' PRINT "Number", "Square" ' LET START = ____________ ' LET FINISH = ____________ ' FOR NUM = START TO FINISH PRINT ______________, _______________ NEXT NUM ' END
No hay comentarios:
Publicar un comentario