11.07.2015 Views

Here - 1000 BiT

Here - 1000 BiT

Here - 1000 BiT

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

uINSTRUCTOR NOTES 17 FOR-NEXT LOOPS uA loop is made up of a FOR statement (which may contain a STEP command) and a ^jNEXT statement. These statements may be separated by several lines and yet arestrongly interdependent. The student builds on the notion of a delay loop and learns the ^utility of repeating a set of commands in the middle of the loop.Nested loops are introduced using a case where the inside loop is a delay loop. ^jThere are subtle points not discussed in this lesson which may arise sooner or later. The \^jloop is always traversed at least once because the test for exit is made at the NEXTstatement which can be reached only by going through the loop.^The FOR statement is evaluated just once at the time the loop is entered. It puts the ^starting value of the loop variable into variable storage where it is treated just as any ^jother numeric variable. The STEP value, the ending value and the address of the firststatement after the FOR are put on a stack.From now on, all the looping action takes place at the NEXT command. Upon reaching ^NEXT, the loop variable is incremented by the value of the STEP and compared with theend value. If the loop variable is larger than the end value (or smaller in the case ofnegative STEP's) NEXT passes control to the statement after itself. Otherwise, it sends ^jcontrol to the statement after the FOR command.Because the loop variable is treated just like any other variable by BASIC, it can be used >or changed in the body of the loop. Jumping into the middle of a loop is usually a disaster. ^Jumping out of a loop before NEXT causes an exit is commonly done, but in some cases ^(especially where subroutines are involved) may give hard to find bugs.^JQUESTIONS:w1. What is the "loop variable" in this line? ^10 FOR Q= 1 TO 10f: PR I NT T$: NEXT Q2. How do you make the loop "count down" instead of "count up"?3. What is meant by "nested loops"?4. Write a loop which prints the numbers from 0 to 20 by two's. ^5. Write a "ten little Indians" program which prints the numbers from 10 down ^jto0 Indians.6. Write a pair of nested loops to print MINI in the outer loop and HA in the inner loop.Print three of the MINIs and for each print two of the HAs.^98

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!