|
|
xxxxxxxxxxxxxxxxxx |
BASIC LESSON 1
| |||||||||||||||||||
Examples :
REM This program solves a quadratic equation.Do not make a REM statement so long that it runs off the right side of the page - instead divide it into two or more REM statements, as in the example below:
REM This routine computes the determinant of a matrix.
REM This program solves a cubic equation.
REM The user inputs the coefficients of the equation,
REM and the program computes the roots.
Examples :
PRINT XThe first example prints the current value of the variable X to the screen. The second prints the values of X, Y, and Z separated by a couple of spaces, while the third (with commas instead of semicolons) prints these same values but in different columns 14 spaces wide. In the fourth example, the text enclosed within quotation marks is printed verbatim, and then the value of X follows immediately; the semicolon following X ensures that the next item printed will follow directly after X and on the same line. (In the first three examples the next printing will begin on a new line.) In the fifth example, text inside quotes prints verbatim, while the mathematical expressions are evaluated using the present value of r and their values printed. The last print statement, on a line by itself, serves as a line feed - Basic will skip a line before the next printing.
PRINT X; Y; Z
PRINT X, Y, Z
PRINT "THE ROOT OF THE EQUATION IS"; X;
PRINT "The diameter is"; 2 * r; "and the area is"; 3.14 * r * r
Example :
PRINT "What is the interest rate";(The semicolon at the end of the PRINT statement puts the question mark on the same line as the prompt - without it the question mark appears on the next line.) As in the next example, it is possible to ask for several items at once - the user types the values, separated by commas, before a final enter.
INPUT R
Example :
PRINT "What are the X, Y, and Z coordinates";
INPUT X, Y, Z
| PRINT "WHAT IS R"; |
| INPUT R |
as the user most likely will not know that R refers to the interest rate. Likewise, instead of informing the user of the value of the monthly payment with a statement like
| PRINT "P = "; P |
you should write
| PRINT "THE MONTHLY PAYMENT IS "; P |
Never print a naked quantity to the screen without explanation. The command
| PRINT X |
will print the value of X to the screen, but the user will have no idea what it signifies.
It is good idea to end a program with "Bye" or something to that effect, so that the user knows the program has terminated - otherwise, the user might presume that the computer is still working or is hung up on something.
Examples :
X = 6 (assigns X the value 6)The order is important - do not write a statement like
Y = Y + 2 (replaces Y with its old value plus 2)
Z = 3X + 2Y + 4 (assigns Z the current value of 3X + 2Y + 4)
| 6 = X |
as 6 is a constant and it cannot be assigned a new value. The statement
| X = Y |
assigns X the present value of Y - it does not assign Y the present value of X.
Addition : X + YIn the absence of parentheses, exponentiation is performed first, then multiplications and divisions, and lastly additions and subtractions. But when in doubt - use parentheses!!
Subtraction : X - Y
Multiplication : X * Y (Don't type XY; Basic will think XY is a new variable.)
Division : X / Y
Exponentiation : X ^ Y (Eg. : 3 ^ 2 = 32 = 9)