Programming Numbers And Variables
INTEGER, FLOATING-POINT AND STRING CONSTANTS
Consfonfi are the data vo ues thai you put in your BASIC statements. ¡3ASIC uses these values lu repieseril duiu during statement execution. CBM BA5IC can .-ecognize and manipulate three typos of constants: I! INTEGER NUMBERS 2) FLOATING-PC I NT NUMBERS 31 STRINGS
Integer constants are whole numbers (numbers withour decimal points). Integer constants mjst be between —32768 and +32767. integer consta firs do nor have decimal points or caminos between digiis. If the plus (+} sign is loft out,, the constant is assumed to be a positive number. Zeros coming before a constant are Ignored anc shouldn't be used sincc they waste memory end slow dawn your program. However, they won t cause an error. Integers are s'orec in memory as twe-byte binary numbers. Some examples ol integer constants are:
32767
MOTE: Ua NO I put csnnrr.as inside any njtnber, for example, always type 32,UOJ cs 32000. It you put □ conma ¡1 the middle t>f a numbe- you will ge- Ihe BAML error rtMitig» iSyNUX EBBOB
Floating-point constants ^re positive or negative numberj and can contain fractions. Pactional parts of a number may bp shown using a decimal point Once again remember that commas are NOT used bo *ween numhers. If tht> puis sign ( + ) is left aft the front of a numaer. the Commodore 64 assumes thai the number is positive. frf you leave off the decimal point the computer will assume that it fallows the Inst digit of the number. And as with integers, zeros that comc befare a constant are ignored Finnting-poini constants can be used In two ways
1) SIMPLE NUMBER
2) SCIENTIFIC NOTATION
Floating-point ccrstants will show you up to nine oigits on your screen, Tnese digits car represent values between -999999999, and + 9999999P9. If you enter more than nine digits the number will be rounded based on the tenth digit, If the tenth digt s greater than or equal to 5 tne number wi I be rounded upward. Less then 5 the numoer will be ro jided downward. This could be important to the f nal totals of some numbers you may wont to wo'k with.
Flcating-point numbers are stored fusing five bytes of memory) and are manipu ated in calculations with ier places of accuracy, However, the numbers are roundsd to nine digits when results nrp primed. Some examples of simple floating-point numbers are;
1.23
Numbers smaller than ,01 or larger -han 999999999. will bo printoc in scientific notation, In scientific r ulution a floating-point consicnt is made up rvf three ports:
1) THE MANTISSA
2) THE LETTE3 E
3) THE EXPONENT
The mantissa is a simple floa^iig-psiiit number. The letter E i= used to te I you that you're seeing the number in exponential form. In other wordi £ represents 4 I 0 (eg., 3E3 =3* lOf^ =3000), And the exponent is what multiplication power of iO the number it raised to.
Both the mcrtissa ard the exponent are signed (+ or ) numbers. The exponents range ts from —39 to +38 and it indicate« the number of places that the actual dccimal point in the mantissa would be moved to the lefl (-) or right ( + i if the value of rhe constant were represented as a s rnple number.
There Is a imit to rhe si;e of floating-point numbers that BASIC ran handle, even in scientific notalion: the largest number is -M ,70141183E + 38 end calculations which would resu!t ir a larger number wit display the BASIC error message ?OVERFLOW ERROR. The smallest floating-paint number is + 2.92673588E —39 and calculations iwhich result n a srnal er value give you zero as an answer and NO eTcr message. Same examples cf floating-point numbers in scientific nutaiiurl fand their decimal values) are:
235.988E-3 ;.23596S)
2359E6 ¡2359000000.)
7.09E-12 (-.00000000000709)
String con&tonis are groups of alphanumeric Information I ke letters, numbers ond symbols. When you enter a string from the keyboard, t can nave any ler glh up lo the space available in an 80-character ine
(thct s, any character spaces NOT taken up by the lire number and other required parts of the statemenl),
A string constant car contain b anks, erters, numbers, punctuation and color or ctrso1" control characters in any combination. You can even put commas between numbers. The only character which cannot be included in a st'mg is rf?c doubic quote mark ("). This is bccause the double quo'e mark is used to define the beginning crd end of the string. A string can also have a null value—which means that it con contain no character data. You can leave the ending quote mark off of a string If ifs the last item on a line or if it's followed by a colon {:>, Some examples of string constants are:
"HELLO"
"NUMBER OF EMPLOYEES"
NOTE: lite CHR$;3<I> to iricludn quotes (") 1n strings.
INTEGER, FLOATING-POINT AND STRING VARIABLES
Varjciibfes are names thai represent dulu values used in your BASIC statements. The vctiue represented by a variable can be assigned by setting it equal to a constant, or il can be the result of lqIlulations in that program. Variable data, like constants, can oe integers, floating-point numbers, or strings. If you refer to a variable name in a program before n value has been assigned, the BASIC Interpreter will automatically create the vcriable with c value of zero if it's an integer or floating-point number. Or ir will create a variable with a null value if you're using strings.
Variable names can be any length but only the first two characiers are considered significant in CBM BASIC- This means that all names jsed for variables must NOT have the sarre first two chcracters. Van-abJe names may WOT be ffie some as BASIC keywords and they may NOT contain ireywo/'ds in fhe middle of /aripbJe names. Keywords îry-clude all BA5IÇ commands, statements, function names and logical operator names, ff you accidenta ly use a keyword In the middle of a variable name, rhe BASIC error message ?SYNTAX ERROR will show up on your screen,
The characters used to form va-îable narres are the alphabet and the numbers 0—9, The firs! uhurauler of the name must be a letter. Data
BASIC PROGRAMMING RULE5 7
type declcration characters (%) and (S) ran he used as the last character of the name, The percent s gn (%) declares the variable to be an integer ana the dollar sign ($5 dec ores n string vnrabie. 'f no type declaration character is used the Interpreter will assume that the variable is a floating-point. Same examples o* variable names, value assignments and data types are:
A$ = "GR05S SALES" (itrino variable)
CP = 12 5 (floating-point variable)
SUM = FP*CNT% [floating-paint variable)
INTEGER, FLOATING-POINT AND STRING ARRAYS
An array is c tasle (or list] of associated dato items refer red lu by u single variable name. In other wards, an rrray is a sequence of related variables. A table of numbers can be seen as an array, for example. The individual numbers within the table become "elements" of the arrCy.
Arrays are n jseful shorthand way of describing n large numher of related variables. Take a 'able of numbers for instance. Let's say that ihe taale has 10 raiws af numbers with 20 numbers In each row. That mokes □ totol of 200 numbers in the table. Without a single array name to call on you would have to assign a unique name to each value in The table. But because you can use arrays ycu only need one name lor rhe array ana all rhe elements in the array are identified oy iheir individual locations within the array.
Array names can be Integers, float!ilp-puldIs ur string dula types and all elements in the array have the same dato type os the array name, Arrcvs cu'i huve o single dimension (as in u simple list) or lliey tun have multiple dineniloii (imagine a grid marked in rows and columns or a Rub k's Cube®). Each element of an array is uniquely identified and referred to by c subscript ¡or index variable) following the array nome enclosed within parentheses ( ).
The maximum number of dimensions en array can hove in theory is 255 and ibe rurriber of demerits in each cimension Is limited to 32767 But far practical purposes array siiei ere limited by the memo-y spcce available to hold their data and/cr the SC cnara-ter ogical screen line. If an array has crly one dimension and its subscript value will never exceed 10 (11 Items: 3 thru 10} Ihen the array will be created by li e Interprete' and fil ed with seres {or nu:ls it string type) the first time any elernenl of the array is referred to, utile wise the BASIC DIM Statement must be used to define the shape and size of the array. The amount of riernory required to store an army con be determined us follows:
5 bytca for the array name 4* 2 bytes far each d'mension cf "he array -I- 2 bytes per element for integers OR -i- 5 bytes per elerrert 'or floating-point OR + 3 bytes per elcrrcnt -or strings AND + 1 byte pe" character in each string element
Subscripts can be integer constants, variables, or or crithmehc expression which gives an integer result Separate subscripts, with commas between th"m, are required for each dimension of an array. Sub-scr pts con have values ;rcm rerc up to the number of elements in the respective dimensions of the array. Values outside that range will causc the BASIC error message ?BAD SUBSCRIPT. Some examples of array nctmcs, value assignments and data types are:
G?%(X) = i (integer array) CNT%(G2%(X)J=CNT%(l)-2 (integer array)
A(5) = 3 (sets the 5th element in the 1 dimensional array called "A" equal to 0)
B;5,i)=0 (sets the element in row position 5 and column position b In the 2 dimensional array called "0" equal to 0)
CSl,2.3) —0 (sets the element in row position 1, column position 2, and depth position 3 in the 3 dimeniiunol array called "C" equol to 0)
Post a comment