lunes, 7 de abril de 2014

FOR Loops

FOR Loops

This chapter discusses another way of doing loops: the FOR 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 FOR statement.
  • Syntax of the FOR statement.
  • Making tables of values using the FOR statement.
  • Common bugs with FOR statements.
  • 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.
The statements that do these things are spread out in the program:
' 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
END
Statement 1 does three things:
  • It uses the variable COUNT for 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
END
As always, use indenting to show the statements that are contained between two brackets. The NEXT COUNT has one other purpose: it shows where COUNT 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
  • COUNT starts out at 0, stays 0 for the PRINT statement, then changes to 1 at the NEXT COUNT statement.
  • Execution returns to the top of the loop
  • AT the top of the loop COUNT is 1, it stays 1 for the PRINT statement, then changes to 2 at the NEXT COUNT statement.
  • Execution returns to the top of the loop
  • At the top of the loop (again) COUNT is 2, it stays 2 for the PRINT statement, then changes to 3 at the NEXT COUNT statement.
  • 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 counter
Remember 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:
  1. The counter is a numeric variable.
  2. You can use counter in the loopBody, but don't change it (this happens automatically with the NEXT.)
  3. startingValue and endingValue can be arithmetic expressions or variables.
  4. loopBody can be any number of statements.
  5. NEXT counter shows the end of the loop body, and shows where counter will change to its next value.
These rules look worse than they are. A few examples will make them clear. Here is a correct program:
' 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.
  1. counter is a numeric variable.
  2. You can use counter in the loopBody, but don't change it (this happens automatically with the NEXT.)
  3. startingValue and endingValue can be arithmetic expressions or variables.
  4. loopBody can be any number of statements.
  5. NEXT counter shows the end of the loop body.
Here is the program with four of the notes illustrated.
' 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
END
This program prints the integers from one to five, all on one line.

QUESTION:

Does syntax tell you how the FOR statment works (not just how it looks)?

 
Here is the general form of the FOR (again):
FOR counter = startingValue TO endingValue
  loopBody
NEXT counter
Some rules about how the FOR statement works:
  1. The first time loopBody is executed, counter has the value startingValue.
  2. Each time control hits the NEXT counter statement, counter is increased by 1, and control is sent back to the top.
  3. The final time loopBody is executed, counter has the value endingValue.
  4. If startingValue is greater than endingValue, the loopBody will not be executed even once.
The last rule looks a little odd, but sometimes it is important. Here is a program that illustrates it:
' Counting Loop 
'
LET START = 7
LET FINISH = 5
FOR I = START TO FINISH
  PRINT I;  
NEXT I  
END

QUESTION:

  1. Is the program's syntax correct?
  2. What does the program print out?
The program is correct in syntax. The startingValue in the 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
'
END
Sometimes 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 START and 14 for FINISH what will the program print?
  • If the user types 5 for START and 3 for FINISH what will the program print?
Say that you want a program to write out the squares of numbers from zero to 25. Remember that the square of a number is the number times itself. You want the output to look something like this:
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

QUESTION:

Fill in the blanks so that the program works.


 

No hay comentarios:

Publicar un comentario