BASIC 101: An Informative Series››By John Kasupski, WNYAUG Part One: ›Introduction & PRINT›› Oh, no! Not another series of ›articles about BASIC programming! Why?›› Well, there are two reasons why. ›The first reason is that it was ›suggested by a few of our members. The ›second reason is that in examining the ›program listings of some public domain ›BASIC programs I've run across, I've ›seen some real kludges, so I know ›there are people out there who will ›find these articles helpful, provided ›they'll bother to read them and apply ›what's in them.›› So, yes, another series of ›articles about BASIC programming. But, ›this one will be different (I ›promise!). You see, most of the ›tutorials on BASIC programming that ›I've read in magazines, newsletters, ›etc. are aimed at one of two groups of ›people, either people who know nothing ›at all about computers, let alone ›BASIC, or people who are already ›"experts" in Atari BASIC, in which ›case the article tries to "convert" ›the reader to BASIC XE, Turbo BASIC, ›or some entirely different language ›besides BASIC.›› Obviously, if you're going to ›read an article about programming ›BASIC, you want to learn about BASIC. ›So, we will NOT try to convert you to ›programming in C, ACTION!, machine ›language, Pascal, or anything else. ›We'll deal with BASIC, period.›› And because Atari BASIC is the ›most widely used dialect of that ›language, at least among Atari 8-bit ›owners, we'll avoid trying to convert ›you to some other BASIC. Yes, I'll ›point out certain features of BASIC XE ›and Turbo BASIC (the two "other" ›BASICs I'm somewhat familiar with) ›from time to time when it's relevant ›and appropriate. But, we won't ›constantly harp on BASIC XE (which is ›a waste when the reader doesn't own ›the $60.00 cartridge) and Turbo BASIC ›(which is a waste when the reader uses ›SpartaDOS, except for the X-cart, ›because Turbo BASIC won't work with ›the disk-based SpartaDOS versions). ›What we cover here will be equally ›valid in practically any BASIC for the ›Atari 8-bit...well, maybe not. I've ›never really used MicroSoft BASIC, so ›I can't really tell if what we're ›covering applies to it.›› Another thing that will make this ›series different is that we're going ›to cut through some of the garbage ›that often appears in BASIC ›programming articles. For one thing, ›if you want to learn about the ›SETCOLOR command, look elsewhere. THIS ›series will be using POKE instead, ›because POKE is faster, simpler to ›learn, and uses less memory...which ›makes me wonder why ANYONE bothers ›with SETCOLOR in the first place. This ›series won't.›› We will also not bother with ›trying to show you how to include ›machine language subroutines in a ›BASIC program. In order to do this, ›you have to have an ML routine to ›include, which means you have to write ›one, which means you have to know how ›to use ML to write one. And if you can ›do that, you don't need a series on ›BASIC, do you?›› So, exactly what WILL we be ›covering? Well, stick with us, and ›you'll find out, and maybe learn how ›to write effective, coherent programs ›and routines along the way!›› For this month's column, we'll ›look at a simple yet powerful command, ›the PRINT statement. The most common ›application for PRINT is to display ›something on the screen. For example:››10 PRINT "This is line 10." 20 PRINT ›"This is line 20." 30 PRINT 3+6›› If you type in the above short ›program and RUN it, you'll get the ›following output:››This is line 10. This is line 20. 9››READY›› This illustrates three important ›points about BASIC and the print ›statement. The first point is that, as ›you can see from the results, BASIC ›executes lines in the order in which ›they're numbered, unless we instruct ›it to do otherwise (by using GOTO or ›GOSUB...but that's for another ›article).›› The second point is that when ›using PRINT with text, BASIC will ›print whatever we enclose in quotation ›marks after the PRINT statement. It ›could be "This is line 10.", or "My ›name is John", or ›"ASDFGHJRTYUIVBNMTYUIO", it's all the ›same to your Atari.›› The third point is that we can ›also use PRINT to tell the Atari to ›display the results of an arithmetic ›operation. When we tell BASIC to ›"PRINT 3+6) the Atari adds three and ›six and prints the result, which is 9.›› Naturally, we don't need a ›computer to tell us that 6+3=9, but ›when we want to solve ›1234+5678+3456+1357+2468, the computer ›can do it much faster than we can. So, ›you now know how to use your Atari as ›a calculator, don't you?›› The following lines show how to ›perform other math operations in ›BASIC.››50 PRINT 8-3 60 PRINT 6*3 70 PRINT 6/2 ›80 PRINT SQR(144)›› RUN the above and you'll get 5 ›(8-3), 18 (6*3), 3 (6/3), and 12 (the ›square root of 144). We can also chain ›calculations together using ›parentheses, like this:››PRINT (16+4)/2›› This will return 10. Without the ›parentheses, Atari BASIC would divide ›four by two first, arriving at two, ›then add that to 16, giving a final ›result of 18. But because your Atari ›understands the use of parentheses in ›mathematics, it will do what's in the ›parentheses first, so it adds 16+4 and ›gets 20, then divides that by 2 to get ›the final answer of 10.›› The Atari BASIC Reference Manual ›lists operator precedence as follows:››Highest Precedence: Relational ›operators used in string expressions ›have same precedence and are performed ›left to right. These include ›>,<.=.>=.<=,and <>.›› These are followed, in order of ›preference, by the unary minus (-), ›exponentiation, multiplication and ›division (* and /), addition and ›subtraction (+ and -), relational ›operations in numeric expressions ›(again, <,>,=,>=,<=, and <>), the ›unary operator NOT, the logical AND, ›and the logical OR.›› If you don't understand all the ›mathematical implications of the ›above, it's alright. It isn't the ›purpose of this article to teach math. ›But if you already know how to do math ›and want to learn how to use your ›Atari to help you, you'll find this ›information vital.›› The examples above also ›illustrate something useful about ›Atari BASIC, which is that if we want ›an immediate anser we can just enter, ›say, PRINT 2+2 and press the RETURN ›key, and the Atari will give us 4 ›right away. In a program, we can use ›line numbers, perhaps storing the ›result in a variable like this:››10 LET A=(16+4)/2 20 PRINT A›› When we RUN this we'll get 10 ›again. Also, the LET is optional here, ›we can just as easily write line 10 ›as:››10 A=(16+4)/2›› Here's another example using ›several variables.››10 A=10:B=5 20 ›AA=A+B:BB=A-B:CC=A/B:DD=A*B 30 PRINT ›"A+B=";AA 40 PRINT "A-B=";BB 50 PRINT ›"A/B=";CC 60 PRINT "A*B=";DD›› When you RUN this program you'll ›get the following:››15 5 2 50›› The above program illustrates ›another feature of PRINT. Notice how ›we used the information in quotation ›marks to set up a nice, neat-looking ›display, then followed that with a ›semicolon, followed by the variable ›name we wanted to print?›› The semicolon leaves the PRINT ›position where it left off. Otherwise ›the answer would have been printed at ›the begnning of the next line on the ›screen. Try replacing those ›semicolons. Rerite the lines as ›follows:››30 PRINT "A+B=":PRINT AA 40 PRINT ›"A-B=":PRINT BB 50 PRINT "A/B=":PRINT ›CC 60 PRINT "A*B=":PRINT DD›› Now RUN the program again and ›look at the difference in what appears ›on the screen, and you'll see what the ›semicolon did in the PRINT statements ›we used earlier.›› There's another command often ›used with PRINT, the POSITION ›statement, which allows us to tell ›BASIC exactly WHERE on the screen we ›want to print something. Let's try the ›following program which illustrates ›the effect of using POSITION with ›PRINT:››5 PRINT CHR$(125) 10 POSITION ›0,0:PRINT "This is line 10" 20 ›POSITION 0,2:PRINT "This is line 20" ›30 POSITION 0,1:PRINT "This is line ›30"›› Before we RUN this program, first ›I'd better explain line 5. Each ›character that your Atari's keyboard ›can produce is represented by a number ›from zero to 255. Some of the ›characters are letters and numbers, ›some are punctuation marks, some are ›the graphics characters produced by ›pressing CONTROL and a letter key. The ›number which represents each character ›doesn't change...32 is an A, 33 is a ›B, and so on. The character numbered ›125 represents the command which ›clears your screen. You can get it ›from the keyboard with SHIFT-CLEAR or ›CONTROL-CLEAR. In a program we can ›tell BASIC to PRINT CHR$(125), and ›when we RUN the program, BASIC will ›clear the screen when it encounters ›this command.›› Now, RUN the program. The screen ›will clear. Now look at the result. ›You'll notice that "This is line 30" ›appears on the screen BEFORE "This is ›line 20", even though we know that ›BASIC executed the statements in the ›order they're numbered. The reason ›things got printed out is because of ›the POSITION command.›› Line 10 uses "POSITION 0,0" which ›tells BASIC to move the cursor to ›column zero, line zero on the screen. ›We then print "This is line 10" ›starting from that screen position.›› Line 20 uses "POSITION 0,2". This ›tells the Atari to begin printing at ›column zero on line TWO on the screen, ›skipping over line one, which is then ›used by line 30 (POSITION 0,1).›› Now, if you ran the program you ›can see that POSITION 0,0 starts ›printing in the first space of the ›first line on the screen. Why, then, ›isn't this POSITION 1,1 instead of ›0,0? because computers count funny ›(compared to humans). To people, zero ›means nothing, so we usually start ›counting from one. But to a computer, ›everything means something, even a ›zero, and so computers start counting ›from zero.›› Thus, the upper lefthand corner ›of your screen is 0,0 to the Atari, ›not 1,1. Your Atari has 40 columns ›across, numbered from zero to 39, and ›it has 24 lines from top to bottom, ›numbered from 0 to 23. You can specify ›anyplace on the screen to print ›something provided you know the right ›x-y coordinates to use with the ›POSITION statement. Of course, in a ›program you should make sure that what ›you want to print will fit. If you ›start printing at, say, column 20, you ›have only 19 characters left on that ›line before you reach the end. When ›you reach the end the Atari will go to ›the beginning of the next line.›› POSITION is handy when you want ›to line things up in columns or print ›in a "window" of space you've set ›aside on the screen for user prompts.›› In a future column we'll examine ›the use of PEEK and POKE in Atari ›BASIC, and we'll see how we can use ›POKE to set the screen margins, ›enabling us to adjust the width of the ›screen so we can get the full 40 ›column width without using POSITION ›(the Atari defaults to a 38-character ›screen width). In the meantime, next ›month we'll look at numeric variables, ›and at some methods of setting up what ›programmers refer to as a "loop".