Introduction To SmartBASIC
Lesson Four
by Charles A. Smith


Here is the fourth lesson in Charles Smith's series, Introduction to SmartBASIC. Our thanks to Charles for his continuing commitment to our education. BDC.

In the last lesson reference was made to some parts of the SmartBASIC Manual for study and I thought we might begin this discussion with a look at those aspects of programming:

ARITHMETIC ORDER OF OPERATIONS - What does '2 plus 3 times four' mean? The answer could be 20; or it could be 14 depending on the order in which you do it. ADAM has a rule; it multiplies and divides first and then adds and subtract. Try this:

 ] PRINT 10-2*3+72/9*5

so, according to the rule:

 10 - 2*3 + 72/9 * 5
       6      8  * 5
                   40
		
 10 -  6  +  40  = 44

You might wonder why ADAM doesn't multiply 9*5 and then divide that into 72 in which case the final answer would be 5.6. In order to avoid any confusion I always use brackets. ADAM will set aside his built-in rules and perform all bracketed operations separately. If your future programming is to involve powers, roots, logarithmic and trigonometric functions along with other exotica you will soon get lost without brackets.

Even if you use brackets within brackets ADAM will do as you did in High School; clear the inside brackets first. Try this:

 ] PRINT 10-(2*3)+((72/9)*5)

 44 is the answer. Now try this:L
 ] PRINT 10-(2*3)+(72/(9*5)) and you will get:A
 5.6

If you are using letter variables in your program, brackets are essential. When I do that sort of arithmetic juggling I will periodically check it out by substituting numerals and running it through on a calculator.

STRING VARIABLES - We learned earlier that a numerical value can be represented by a letter. It is then referred to as a 'variable' because the letter may have different values from time to time within the program.

We can also assign letters to represent names, words or groups of words and even a combination of letters and numbers. In that case it is called a 'String Variable' and can be one or two letters followed by a dollar sign. eg, A$="HELLO"

If that is included in a progam, every time you command:


 ] PRINT A$ - you get

 HELLO

String Variables are very useful. Try this short program:

 ] NEW
 ] 10 HOME:PRINT"Please type in you name- ":INPUT N$
 ] 20 PRINT:PRINT"What city do you live in "N$"? ":INPUT C$
 ] 30 PRINT:PRINT"I've heard that "C$" is a pretty city"

Note that the contents of a String Variable must always be within quotation marks. You will notice that the SmartBASIC manual shows a different method for String output. Both methods will work but I think the method above is a little better because it allows you to insert the variable anywhere within the PRINT command.

IF/THEN COMMANDS - An extremely useful feature both in arithmetic as well as string variables. Basically, what you are telling ADAM with this command is; when one event occurs, another event must follow. Like so:

 ] NEW
 ] 10 HOME:PRINT"Want to play chess? Y/N ":INPUT A$
 ] 20 IF A$="Y" THEN GOTO 50
 ] 30 PRINT:PRINT"Later maybe."
 ] 40 END
 ] 50 PRINT:PRINT"You want Black or White? ":INPUT B$ (etc)

You'll notice that we didn't have to say 'IF A$="N" THEN...' it is obvious to ADAM that if A$ does not equal "Y", he is to go on to the next command which in this case is Line 30. We put in the 'END' command that ADAM would not just go from there to the PRINT command in Line 50.

One of the early truths to recognize in programming is that the computer responds not to mathematical or equational reasoning but to pure deductive logic. Here is a perfectly reasonable command:

 ] 100 IF B<>1 THEN B=1

What you have told ADAM is that, say at the end of some calculation, that you want B equal to 1 regardless of its current value. (The symbols, <>, mean 'not equal to').

 ] 200 IF B=1 AND C=2 THEN B+C=10

Again, a logical command and ADAM won't argue with your arithmetic. In this case you ordered a conclusion based upon two premises; perfectly OK but you must always use just one IF for every THEN.

Next lesson we will look at some of the other useful commands you can use to advantage in your programming such as:

FOR/NEXT

GOSUB/RETURN

DATA/READ

(Just for fun see if you can create a program that will convert from pounds to kilograms and vice versa.)
 

Back to Top