The CBasic Clinic.(part 2)John A. Libertine.

The CBasic Clinic

Part Two

In our first session, we learned that CBasic is probably the most advanced of the Basic family of computer languages. It lends itself to structured, modular programming, and has powerful file handling features. Many of its mathematical functions and variable formats are unique for a Basic and approach the methods used in Pascal and Ada.

Most important, we learned that CBasic is a compiled language. It is written on a text processor and put through a compiler to write an intermediate file which is used to run the program. We then wrote and ran a simple program to illustrate the procedure. Now we begin to dig into the details which will eventually make it possible for you to use CBasic on a practical level.

We have already had some exposure to the use of variable names. You now know a variable can have up to 31 significant characters. This allows self-explanatory names (End.of.month.total, for example) which can be a great aid to self-documentation and/or debugging. Also, you can use different variables with the very same first several characters (Total.1 and Total.2, or Name.and.address.sales$ and Name.and.address.purchasing$ ). The periods between words make for easier reading. They are not necessary, but who wants to decipher Nameandaddresspurchasing$?

Note that the variables above are all typed in upper- and lowercase. Here's another small advantage of CBasic. Variables can be written in all caps, upper and lower or all lowercase. Even key words like PRINT and GOTO can be any case you want. The compiler automatically converts them to uppercase for you.

We learned that there are three types of variables: string, integer, and real numeric. The types are distinguished by a suffix character ending. String variables end with a dollar sign ($ ); integer variables end with the percent sign (%); and real numbers have no suffix ending.

Strings refer to alphanumeric data and can consist of anycombination of letters, numerals and/or special characters (&, %, @, #, etc.). A string can have up to 255 characters in it. Strings are enclosed in quotation marks which, of course, do not print in the final readout. This presents a minor problem if you really want to print a quotation mark. The way around this is to point two quotes together as: ""I agree,'' said John. This will print out as: "I agree,' said John. If numbers are enclosed in quotes, they will be treated as strings. As we will see, this sometimes can be useful.

Integers

Integers are whole numbers with no decimal or fractional parts. Integers occupy much less computer memory, and calculations are faster than for real numbers. Your computer can store four integers in the same memory needed for one real number. Therefore you should use integers whenever possible. Like most things in life, you pay a price. They are limited to the range of minus 32,768 to plus 32,767. Just remember that good programmers use them consistently whenever they can.

Valid integers would include: 123, 4567, -89. Invalid integers would be 123.4 (because of the decimal fraction) and 45678 (because it is too large). The number 5.0 would be a valid integer because it equates to 5; however, you should avoid such numbers, because your computer must convert them to whole numbers before processing them.

It is even more important to know what happens if you assign a real number as an integer variable. For example, LET Number % = 123.45 truncates the real number to 123. A number such as 1.9999 would be truncated to 1, not rounded to 2. You should be very cautious in assigning integer variables for this reason. Ideally, you will never "mix' your numerics, but there will be times when you may want to do so deliberately. Just be certain you understand the consequences.

Real numbers (also called floating point numbers) give you much more flexibility than integers. In theory, you could enter a real number with 255 digits. In practice, CBasic real numbers have 14-digit precision. This means absolute accuracy for the first 14 digits. Or to put it in dollars, absolute accuracy, to the penny, for a trillion dollars. Beyond this, CBasic rounds off the 15th digit and drops the rest.

Just remember that the most digits you can print out in regular decimal format is 15 (with the final digit a zero in the case of whole numbers). For decimal numbers, the most will be 14 (with the last digit a round off).

Longer numbers will be printed out in exponential format (for example: 3.1234567890123 E 46). Except for rather esoteric scientific uses, you are not likely to outgrow 14-digit precision. For a comparison, some simple Basics limit you to fewer than six digits of precision! The 14-digit precision of CBasic puts it at the top of the ladder.

In addition to exponential notation, CBasic allows two other numeric forms (for integer data only). You are not likely to need either until you get into some very advanced uses, but you should know that binary and hexadecimal notations are available.

In general, CBasic gives you far more freedom with variables than other languages. For example, in most cases, if you mix integer and real numbers, CBasic will automatically convert them for you. There are some important exceptions which we will point out as we come to them. You still should avoid this if you can, since the conversion consumes both memory and running time.

Keywords

You will also appreciate the advantages of CBasic when it comes to the use of key words. A key word is one used as a statement or function in the language. For example, AND, FOR, and PRINT are key words in most Basics and the use of such a key word even as part of a variable name will result in an error. Note that a variable like Total in most Basics will result in an error since TO is a key word.

In CBasic, only the variable To would cause a problem. As long as your variable is not exactly the same as a key word, you can use it.

There is one exception: FN as the first two letters in a CBasic variable indicates a user defined function (a very powerful CBasic advantage you will learn about later in this series). Thus, you cannot use FN as the first two letters in any variable. Aside from this, you can almost forget there is a key word variable problem.

Hierarchy Of Commands

We shall shortly write and run a simple program to illustrate the use of the variables we have discussed. Before that, however, we should point out that CBasic has a hierarchy of math commands which is similar to other Basics. The signs for addition and subtraction are the familiar plus (+) and minus (-).

For multiplication, you use the asterisk (} and for division, the slash (/). If you want to raise to a power, the caret sign is used (on some computers, this will be the up-arrow sign). As in almost all languages, the simple formula A + B - C = D since we want to assign the result B - C since we want to assign the result to the variable D. In other words, LET D = A + B - C. In this case, the math is done from left to right.

Now, let's take a somewhat more complex example: LET E = A + B * C - D. If we assign the numbers one through four to A through D, what is the answer? Going from left to right, we could assume: A(1) plus B(2) equals 3, times C(3) equals 9, minus D(4) equals 5. Run it through your computer and you will get the answer 3. Why? Because the CBasic hierarchy will require that multiplication be done before addition and subtraction. Thus it will multiply B * C(2 times 3) first then add A(1) to get 7 then subtract D(4) to get 3.

The order in which calculations are done is as follows: Exponentiation nraising to a power) is done first; next multiplication and division (left to right); finally, addition and subtraction (also left to right). There is, however, a way to modify the order.

For example, we can come up with the answer 5 in the above formula by changing it to read: LET E = (A + B) * C - D. See those parentheses around A + B? CBasic performs calculations within parentheses first. Then it goes back to do the rest of the calculations. In this case, it will add A + B(1 + 2) first, then multiply by C(3) to get 9, then subtract D (4) to get 5.

The computer will follow your instructions, so you must be sure you really know in what order the calculations should be performed. These are fundamental facts you must remember, so let's review it once more. The order of calculations is: 1) operations within parentheses; 2) raising to a power; 3) multiplication and division; 4) addition and subtraction. All operations are performed from left to right.

The proper use of parentheses is most important. You can, for example, have nested parentheses (parentheses within parentheses). In this case, the innermost operations are carried out first. Using the same numbers as before, what is the answer to: LET E = (A - (B + C) * D?

The answer should be -16. If you didn't come up with that, you had better re-read the paragraphs above.

There is an additional use for the plus (+) sign in CBasic. This is to add one string to another. Actually, the term is not add but concatenate and is used only with strings (alphanumerics). For example: if A$ = "One' and B$ = "Two' and C$ = "Three,' then A$ + B$ + C$ would equal "One Two Three.'

Here is another way to see the difference. 2 + 2 will equal 4. But "2' + "2' will equal "22' because the quotes have made the numbers into strings. You probably can't see a practical use for this right now, but it does come in handy at times.

The program we are going to write will illustrate many of the features we have discussed above. To make the results look more professional, we want to clear the screen before anything prints on it. Later in this series, we shall see how to use control characters to do this efficiently. However, not every computer uses the same controls, so we shall use a roundabout way which has the advantage of working on any computer. We shall ask the computer to print blank lines over and over until everything has scrolled off the top of the screen leaving it blank.

Let's assume your computer has 24 horizontal lines. If it hasn't (some have 16), use the number of lines that apply every time you see 24. We could just print 24 blank lines this way:

etc. for a total of 24 PRINTS

Or we could put a colon between each PRINT (meaning the next instruction goes on the following line) as follows:

PRINT:PRINT:PRINT:PRINT: (24 times)

But we are going to use a much more efficient way: a FOR-NEXT loop. If you are familiar with Basic, you will see that it works very much the same in CBasic. It you are not, just type it out for now and we shall explain in a moment. At the same time, we are going to add another FOR-NEXT loop immediately below the first. This one will cause the program to pause (when the screen is blank) before proceeding. This is a frequently used device in programming and is sometimes quite impressive.

Entering The Program

Take a look at the program in Listing 1. Note again that we don't use line numbers in CBasic. Type the program on your word or text processor exactly as printed. Be especially careful to type commas, semicolons, and colons exactly, and note spacing within a string. Frequently, there is a space before the final quotation mark. Now, store the program. Let's call it: VARIABLE.BAS (don't forget the .BAS).

Now be sure your CBasic programs (CBAS2.COM and CRUN2.COM) from our first session are on the same disk as the new program and proceed to compile by typing: CBAS2 VARIABLE (the .BAS extension is assumed; you do not have to type it).

If you have entered your program correctly, the note "No errors detected' will appear at the bottom of the screen. If there are errors, you must go back to your text processor, correct them, and compile again. The most likely errors will be misspelling a key word (PRINT instead of PRINT), leaving out a quotation mark at the end of a string, or omitting a closing parenthesis.

Once the program compiles with no errors, type in the command to run the program: CRUN2 VARIABLE. The screen should clear and remain blank for about five seconds. Then you should get the readout shown in Figure 1. If you do not, check your program. Did you type the numbers incorrectly or leave out or misplace a parenthesis or a quotation mark?

You may very well think this program doesn't do anything and, of course, you are right. It is intended only to illustrate the use of variables. In our next session, we shall start writing programs that accomplish things. As you go on, you will write your own programs to solve problems or set up useful procedures. Before that can happen, you must learn the rules of the road so be patient for now.

Before we end this session, let's take a look at the two FOR-NEXT loop functions used in the variable program. It is a very frequently used and powerful tool in any Basic. Think of it as meaning "FOR every given number of times I tell you to, do this thing until you come to the word NEXT and start over again.' In these simple examples, we are asking the computer to print a blank line 24 times and to pause before continuing. Figure 2 is a line-by-line explanation of the first loop.

The next loop is even simpler. We set up a loop to be processed 3000 times (this figure might have to be changed depending on the speed of your computer). Since there are no instructions between the set-up and the NEXT line, the loop does nothing. However, the computer must still look for what to do 3000 times. This takes time, hence the pause.

Many times FOR-NEXT loops are used as a subroutine. You might, for example, want to clear the screen many times during a program run. Rather than re-type the loop over and over, we put it in the program once and refer to it in a GOSUB statement. In this case, the loop would have a line number at the beginning and a RETURN line after it to take the program back to where the GOSUB left off. We shall go into GOSUB (and GOTO) uses in much more detail in our next session.

In the meantime, play with the program listed here. Make changes in both the string and numeric variables. See what happens when you move the parentheses in math formulas. Try changing the 3000 figure in the pause loop to 1000 and to 6000 to see the difference in time. Just remember that these are fundamental statements and functions in CBasic so you should understand them thoroughly and be very familiar with their uses and variations.

Also, try changing the spaces before the end of a string. Take it out in one place and add several extra spaces elsewhere (as in "One', for example). Also try putting one or more spaces after the first quotation mark in a string (i.e., "One'). The results will be quite different. More important, you can use this method to format your printouts (whether on the screen or your printer). For example, in the concatenation of strings, you can get the printout to read:

One Two Three

(instead of OneTwoThree) by simply inserting spaces as:

A$ = "One' B$ = "Two'

No space is needed after "Three,' of course.

Table: Listing 1.

Table: Figure 1.

Table: Figure 2.