SECTION E FURTHER WORK ON LOOPS FILE INTER.E In PRIMER we looked briefly at the FOR......NEXT loop. Here we shall examine this further to find some of the uses for it and how loops are "nested" About the simplest FOR loop can be written on one line :- 340 FOR a=0-1000: NEXT What does this loop do?. Well at the start a=0, then on NEXT the program loops and a=1, loops and a=2 and so on. Nothing is printed to the screen but the line just pauses the program until a=1000, when the program is free to carry on. We have programmed in a time delay, which can be varied by increasing or decreasing the number of times the program loops. Another simple FOR loop is:- 10 FOR a=1 TO 30 20 PRINT a; 30 NEXT This loop when RUN will print the numbers 1 to 30 across the screen , Many other variations of this loop are possible . Let us consider line 10. If we rewrite it to read:- ======================== 10 FOR a=1 TO 30 step 2 , the loop would print out 1,3,5,7 etc. incrementing according to the step value. Loops can work backwards also e.g 10 FOR a=10 TO -10 step -2 is valid. Variables can also be used 10 FOR a=1 TO n, or 10 FOR a=p TO n STEP y and so can HEXADECIMAL 10 FOR a=&H1 TO &H1D So you see that we are beginning to see tremendous potential. Now let us consider line 20. =========================== If we change it to read 20 PRINT "Hello", the word 'Hello' will be printed out 30 times. Or 20 PRINT 5*a; it would print 5,10,15,20 etc. i.e 5 times each value of a from 1 to 30. However we don't have to use a print command at all. The FOR loop generates a series of numbers of your choice, and these numbers are there to use in any way that you choose. You could put the numbers into a series of equations to draw a graph, or to read DATA( more of this in the section on arrays) .You can generate a series of addresses in HEXADECIMAL to POKE machine code(Not covered in this course) FOR....NEXT loops can become quite complicated and one can branch outside the loop as long as there is a transfer back into the loop 10 FOR a=1 TO 10 20 GOSUB 100 30 NEXT . . 100 PRINT n-a: RETURN Of course, the examples shown are very simple, as I am demonstrating a principle, not advanced programming. Such an idea could be useful if the subroutine is substantial and has to be fed with series of numbers from several loops. GOTO can also be used to branch out of a FOR...NEXT loop as long as you remember to branch back in with another GOTO. Branching out of a loop is not a practice I would recommend but it's possible and might be of use to you sometime. FOR....NEXT loops can be nested, that is go one inside the other:- _______________ 10 FOR a=1 TO 3 | Note that nested loops must not 20 PRINT a _________ | cross each other but must be 30 FOR b=1 TO 4 | | ordered as indicated by the lines 40 PRINT b ______ | | ********************************* 50 FOR c=1 to 6 | | | 60 PRINT c | | | 70 NEXT c _____| | | 80 NEXT b _________| | 90 NEXT a ______________| There is no need to add a,b or c after the NEXT commands, but does make it easier to follow the listing. ************************* The WHILE ......WEND loop is the other form of loop supported by MALLARD BASIC. To use this loop a condition is set by the WHILE statement, which if true allows the program to continue as far as WEND, when the program loops to the WHILE statement again. When the WHILE statement becomes false the program 'drops through' the loop and continues. A simple one-line WHILE... WEND loop is used throughout this course program. 40080 WHILE INKEY$<>CHR$(13): WEND This loop pauses the program until [RETURN] is pressed. INKEY$ waits for a key press, CHR$(13) represents [RETURN], <> is 'not equal to' So in words " WHILE a keypress is not equal to [RETURN] then loop (WEND)". As soon as you press [RETURN] then INKEY$ = CHR$(13) and the statement becomes false, allowing the program to continue. Whereas the number of loops in a FOR...NEXT loops is decided in advance, the WHILE....WEND loop continues until a specified condition is met. If the condition is met before the loop is encountered then the program will 'drop through' the loop. As in the case of the FOR....NEXT loop, WHILE....WEND loops can be nested, with the same rules. In addition the two types of loops can be nested together How much NESTING is permissible depends upon the size of the 'stack' This is part of memory set aside for information for loops and GOSUB returns. The default size of 512 bytes is sufficient for most purposes, but if extremely deep nesting is used a larger stack size may be required. This is done with the MEMORY command, covered elsewhere. Don't worry about this too much at the moment; you may never need it, but if in the future you are using deep nesting techniques ( more then 3 or 4 nests ) and are getting problems, it may be due to insufficient stack size. You are warned with a 'Memory full' notice. Loops can also be formed with the GOTO command (very often unintentionally), and this was discussed earlier in PRIMER section d However, useful loops can be devised, possibly in conjunction with the IF...THEN GOTO construction. Why not ? End of file INTERE.TXT  possibly in conjunction with the IF...THEN GOTO construction. W