First Steps: A-Z of Atari Basic part 2

by Mark Hutchinson



Issue 15

May/Jun 85

Next Article >>

<< Prev Article



FOR (F.)

This is used with NEXT and, sometimes, with STEP to form loops.

Example: 10 FOR A=1 TO 10: ? A NEXT A

A will start off as 1, the current value of A will be printed, and A will then be incremented by 1 to equal 2. This will repeat until A=10 when the program will print this value and finish. STEP changes the size of the increment and, if it is a negative number, will also decrement the value.


10 FOR S=0 TO 255 STEP 5: SO.0,S,10,10: NEXT S
10 FOR S=255 TO 0 STEP-5: SO.0,S,10,10: NEXT S

If no other commands are included in the FOR/NEXT loop, then the computer will run the loop while doing nothing. This is usually termed a 'wait' or 'timing' statement. You can have a loop inside a loop (termed 'nesting').


10 FOR S=250 TO 0 STEP -10: SO.0,S,10,10: FOR WAIT= 1 TO 50: NEXT WAIT: NEXT S

It is not recommended to use too many nested loops. Sometimes an error 13 will occur (no matching FOR statement). This can be caused by using the same variable too many times. Just change the variable (say, S to SND) to clear this. Of course, you will need to check that your loops do coincide, that the first FOR corresponds with the last NEXT. In the last example if you change NEXT WAIT: NEXT S to NEXT S: NEXT WAIT it will not run properly.
This can be a very powerful statement in graphics. Instead of a series of PLOT/DRAWTO, it is possible to use a FOR/ NEXT loop, incrementing X or Y, and one PLOT/DRAWTO saving lots of memory.


10 GR8: C.1: FOR X=0 TO 155 STEP 5: PL X,0: DR. X,319: NEXT X


This will test for, and return, the amount of free user RAM in bytes. It takes the form of ? FRE(0), where 0 is a dummy variable. It can also be used in the deferred mode.

Example: 10 IF FRE(0)<X THEN ? FRE(0);" BYTES LEFT'


This is usually used in graphics with the POSITION statement and will return the data under the cursor and place it in a variable. This will be a character or colour, depending on the graphics mode. NOTE: If you wish to PRINT after GET it is necessary to PUT the data back as quite often the pixel value has been corrupted. By OPENing a channel, it is possible to read a byte from a file designated by the channel and store it in the variable. This is normally done when retrieving screen displays or other large chunks of data.

Example: 10 GET #1,X


This statement will send the program to a sub-routine. This is a self contained routine outside the main program. The sub-routine must finish with RETURN to go back to the main sequence. Care must be taken when using nested GOSUBs or a GOTO in the routine. GOSUB uses the system stack to remember where to return to. POP will clear the stack if the sub-routine has not been allowed to finish normally, but this is a sign of bad programming and, after debugging, is not recommended. Calling an often used routine through GOSUB will save memory, but you must use it often to be of value.

Example: 10 GOSUB 100: GOSUB SOUND1


Similar to GOSUB but does not need a RETURN. Again, be careful of creating endless loops with too many GOTOs.

Example: 10 GOTO 100: GOTO SOUNDI


Used to select one of the 16 graphic modes. Apart from GR.0, these modes will have a text window at the bottom unless 16 has been added to the GR. number. By adding 32 to the GR. mode, you can set up the text window without clearing the screen or switching off the sound.





GR. 39




A conditional statement used with THEN. When the IF part is true, the THEN part is executed. If it is not true, the THEN part is ignored. If any other commands follow IF/ THEN on the same command line, they will only be executed when the conditional statement is true. Nesting can occur in this statement.


10 IF X=5 THEN ? "5"


In line 10 X must equal 5 for the PRINT to occur. In line 20 (nesting) both X and Y must equal 5 for the GOTO to occur. 

NOTE GOTO or GOSUB is not necessary in this statement, as a line number or reference would suffice.


Example: 10 IF X=5 THEN 100

Make sure that there is a THEN for each FOR


This statement looks for a keyboard response from the user. It will place this response into a variable or string, to be used later in the program. Unfortunately the program will halt until this response is entered with RETURN. Several responses to one INPUT can be entered at once by using commas. When used with an IOCB number(#) it will request data (string or numbers) from a specified device, providing that IOCB# has been OPENed first.






Warning: An input without a variable may not cause an error report. Also, an input of more than 128 bytes will overwrite the start of page 6, the so called 'safe area'.


This returns the integer (the whole number preceding the decimal point) and sign of a number. Unlike calculators, it will return the whole number nearest to the complete number given.


A=INT(3.44): REM *** Returns 3 

A=INT(-3.99): REM *** Returns -4


This will give the length of a string in bytes. Until something has been stored in a string, its length will be 0.

Example: A$="PAGE 6": ? LEN(A$): REM *** Will return 6


This will define a variable or variable name. It can be left out (implied LET). However, if you wish to use a command word as a variable, you must use LET.


LET X=1.2
X=1.2: REM *** Implied LET
LET SOUND=1000: REM *** Compulsory LET


This is a way of saving a program in a form that can be loaded (using ENTER) without clearing memory, either to cassette or to disk. LIST by itself will list out the resident progam, to the screen, in full. If line numbers are specified then only those lines will be listed. Likewise, if the printer is specified the program (or required lines) will be printed.



LIST 20,100

LIST "P.",10;50


This will load a program from disk into RAM. It will clear any existing memory and will need the command RUN to start the program.

Example: LOAD "D:PART1.BAS"


For some reason, this is seldom seen now in programs, maybe because people do not understand it It is really quite simple.

Example: LOCATE X,Y,Z

This will position the cursor (visible or invisible) at point X,Y and place the information stored there in the variable Z. This will be a number, either O-255 for a text character or 0-4 for a colour. This is the same as using POSITION X,Y: GET #6,Z. The only problem is that a PRINT or GET after LOCATE may corrupt the data under the cursor. You may need to reposition the cursor and PUT the data back.


This will return the natural logarithm of a variable or expression.

Example: L=LOG(55.2)


This will open a channel to the line printer and will print in direct or deferred modes. As with ?, LP. by itself will skip a line.

Example: LP."PRINTER"


This will clear the contents of RAM in either direct or deferred mode. Unfortunately it will clear all dimensioned tables.


Used with FOR please refer to that section.


A logical operator. It will return a 1 if the result is not true. Be careful of ? A=NOT B, it will put the computer to sleep. Be sure to use a space after NOT as confusion could occur with NOTE.

Example: A=NOT E


This is used in disk operations to open a channel to the drive and store the current sector and byte in the variables. The DOS manual gives an example program of this command. The first variable is the IOCB#, the second is the sector, and the third is the byte.

Example: NOTE #2,SEC,BYTE


Used mainly with GOSUB or GOTO, but can be used with other commands. When the variable value is known, the program will go to the subroutine that corresponds to the value.

Example: 10 ON X GOTO 100,200,300,400

If X=3 then the program will branch to the third choice, ie. line 300. This will be a topic for 'First Steps' later in the year.

continued next issue