|
|
xxxxxxxxxxxxxxxxxx |
BASIC LESSON 2
| |||||||||||||||||||
Examples :
1) INPUT "What is your age"; ageIn the first example (with a semicolon), Basic adds a question mark after the prompt, while in the second and third (with commas) no question mark is added. In the third example the user is prompted for two quantities at once.
2) INPUT "Please enter your age: ", age
3) INPUT "Enter your age and weight : ", A, W
| SQR (square root) | ABS (absolute value) | |
| SIN (sine) | COS (cosine) | |
| TAN (tangent) | ATN (arctangent) | |
| EXP (exponential) | LOG (natural logarithm) | |
| INT (largest integer function) | SGN (sign function) |
The trig functions work in radians. The value INT(x) is the largest integer less than or equal to x. Also, SGN(x) is + 1 if x > 0, it is - 1 if x < 0, and it is 0 if x = 0.
Examples :
1) IF x > 0 THEN PRINT "x is positive"The part of the statement immediately following "IF" is some condition the computer can check; the part following "THEN" is some command to be carried out. Note that in the above examples the complete "IF … THEN" statement fits on one line. An alternative is to place the command following "THEN" on a separate line; but in this case you must end the construction with an "END IF" statement. You can also insert more than one command after "THEN".
2) IF x < y THEN z = x + y
Examples :
| 1) | IF x > 0 THEN | 2) | IF x >= 0 THEN | ||||||
| PRINT "x is positive" | y = SQR(x) | ||||||||
| END IF | PRINT "The root is"; y | ||||||||
| END IF | |||||||||
(The commands after "THEN" are indented only to make the program easier to read.)
Examples :
| 1) | IF x >= 0 THEN | ||
| y = SQR(x) | |||
| PRINT "The square root of x is"; y | |||
| ELSE | |||
| PRINT "Since x is negative it has no square root." | |||
| END IF | |||
| 2) | IF x > y THEN | ||
| u = x - y | |||
| PRINT "x is greater than y by the amount"; u | |||
| ELSEIF x = y THEN | |||
| PRINT "x = y" | |||
| ELSEIF x < y THEN | |||
| v = y - x | |||
| PRINT "x is less than y by the amount"; v | |||
| END IF | |||
In the first example, Basic performs the "ELSE" command only if the "IF" condition is not fulfilled. In the second example, Basic runs through the list of conditions and carries out only the first command whose condition is fulfilled.
| < | (less than) | |
| > | (greater than) | |
| = | (equals) | |
| <> | (does not equal) | |
| <= | (less than or equal to) | |
| >= | (greater than or equal to) . |
Examples :
| 1) | IF x1 = x2 AND x1 = x3 THEN | ||
| PRINT "The three roots are equal." | |||
| END IF | |||
| 2) | IF x > 0 AND y > 0 AND z > 0 THEN | ||
| PRINT "All coordinates are positive." | |||
| END IF | |||
| 3) | IF sum1 < 0 OR sum2 < 0 THEN | ||
| PRINT "At least one sum is negative." | |||
| END IF | |||
| 4) | IF A = 0 OR B = 0 OR C = 0 OR D = 0 OR E = 0 THEN | ||
| PRINT "You have a zero coefficient." | |||
| END IF | |||
In the first example the PRINT command is executed only when both equalities are true. In the second example, all three inequalities must hold for the PRINT to be executed; otherwise it is ignored. In the third example if either or both of the inequalities hold then the PRINT is executed. In the fourth example, the PRINT is executed if one or more of the equations is valid. In general, for conditions connected by AND, all conditions must hold for the subsequent command to be executed. For conditions connected by OR, the command is executed whenever at least one condition holds.
| x | X | z4 | A! | age | zebra! | . |
| x# | Y3# | tiger# | . |
Example :
| IF AGE < 0 THEN | |
| PRINT "Error - your age cannot be negative!" | |
| END | |
| END IF | |