Long before you heard of Mr. Smith's 8th grade algebra class, you probably began using variables in arithmetic, perhaps in Mrs. Roberts' 4th grade math. When working with a triangle, you learned a=(b*h)/2, area = ½ base times height. You simply did not think of b and h as variables, because you could see that a triangle had a base and a height, as well as an area.
When using Just Basic, we have two classes of variables. Numeric variables can be represented with a letter, or a name: base, height, thisReallyWeirdNumber, etc. The other class of variable is a string$, which is a letter, word, or combination of letters and numbers between two quotation marks. You cannot do arithmetic with string$, think of them as pictures of letters and numbers. You can add string$ together (called concatenation) to make a bigger picture, but that doesn't change the value of the string$.
firstString$="swim"
secondString$="ming"
PRINT firstString$+secondString$
END
In Just Basic, string$ variables are identified by the $ symbol, meaning this is a picture of a character, number, or a whole bunch of them. Numeric variables don't require any special sign, and you can use them in your code, just as you would with a paper and pencil. Because the Worbles are very accurate, your code will never make a mistake, if you have written it properly.
One of the chores you will assign to a variable is counting iterations in a loop.
counter=counter+1
In everyday arithmetic, we would think that equation makes no sense. How can counter=counter+1? Well, it can't, but we're sending an instruction to the computer telling it to make counter larger by adding one to it's value. With every trip through the loop, one will be added to the value of the variable named counter.
The computer automatically does this for us when we write a FOR/NEXT loop.
FOR k=1 TO 100
PRINT k '<< (or do some other task one more time)
NEXT k
Here too, the variable k is increased by one every time the computer is told NEXT k. When k eventually equals 101, the code drops through to the next line of execution following NEXT k.
If we're writing a DO/UNTIL loop, or a WHILE/WEND loop, we must write our own code to increment a counter or the computer will not know when the task has been completed and will go round-and-round the loop endlessly, or until the Worbles get so dizzy the computer begins to smoke and eventually blows a circuit.
In other words, you must have a comparison so the Worbles will know when they are finished.
While we cannot use string$ as counters (oh, we could, but that would take several more lines of code), we can compare string$ to see if they are larger than, less than, or equal to another string$.
aString$="747"
bString$="91"
IF aString$<bString$ THEN
PRINT aString$; " is smaller than "; bString$
ELSE
PRINT aString$; " is larger than "; bString$
END IF
END
"Wait a minute, Welopez! I just entered that code into Just Basic and it told me 747 is smaller than 91! My Worbles must be dizzy or something!"
No, that answer is entirely correct. Remember, we're working with string$ in this snippet, not numbers. When Just Basic compares string$, it looks at the ASCII value of the first character of the string$. In this comparison, ASCII(7)=55, while ASCII(9)=57, so the Worbles were right, 7 is smaller than 9. If the first two characters are equal, Just Basic will look at the second character, then the third, until an inequality is found and the computer could make a comparison. A string$ is not a value, it is a picture. Potassium is smaller than Potato, but Potability is smaller than both of them.
When you assign names to the variables in your code, try to use recognizable names: diameter=2*radius, area=pi*(radius^2), newDate$="07/04/2007", so you will recognize what the variable represents six months from now when you next look at the code. The Golden Rule when naming variables is you may not use words from the Reserved Word List! See the help file topic "Reserved Word List" for words which have a special (and reserved) meaning in Just Basic.
You can use the "dot" in variable names if it will make them more meaningful to you.
pi=3.14159265
new.Radius=12
new.Circumference=pi*(new.Radius*2)
PRINT new.Circumference
PRINT "Done"
END
Just Basic uses "floating point" values for variables. This means you cannot depend upon numbers greater than 17 decimal places, as the value will simply be an approximation based upon the way binary numbers are manipulated in the computer. In normal usage, decimal fractions will be displayed with a maximum of 9 digits, i.e., 3.14159265 (the decimal point is a digit also), but you can force Just Basic to display a more precise value with the PRINT USING() function.
pi=3.14159265358979323846
PRINT USING("##.####################", pi)
END
"Wait a minute, Welopez! My Worbles made another mistake! They printed 3.14159265358979399680!"
Well, yes and no. The difference in the last four digits is due to the way computers handle "floating point" values. You can rely upon the output for the first 16 digits following the decimal, but the 17th digit and more, will only be approximations as the computer tries to convert binary values to decimal. What does this do to your accuracy?
The distance from the Earth to the moon is pretty close to 238,000 miles (using lasers, scientists can measure it accurately to within 1/8th inch). Multiplied by 5,280 feet per mile, the distance is 1,256,640,000 feet, multiplied by 12 inches, the distance is 15,079,680,000 inches. In Just Basic, you can work with the distance of the moon to an accuracy of .000001507 inches before you have to worry about "floating point" errors, but you'll need a really big ball of string before you can measure such a tiny distance