Calculus Computer Lab
Math 190


News

Lab Schedule

QuickBasic Notes

Fortran Notes

Tutor Room

Winplot Manual

Lab Home

xxxxxxxxxxxxxxxxxx

BASIC LESSON 2

Lesson Topics
INPUT (with Prompt)Relations
Functions (Built-in)AND and OR
IF ... THENSingle Precision
IF ... THEN ... ELSEDouble Precision
ConditionsEND (Middle of Program)
View DemosDownload Demos
# 1 # 2 # 3 # 4 # 5 # 6 # 7 # 1 # 2 # 3 # 4 # 5 # 6 # 7
Main Basic Page

INPUT (with prompt)

Basic allows you to place the prompt to an input inside the input statement itself.

Examples :

1) INPUT "What is your age"; age
2) INPUT "Please enter your age: ", age
3) INPUT "Enter your age and weight : ", A, W
In 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.

Functions (Built-in)

Basic has several built-in functions. For example, SQR(x) means the square root of x, and SIN(x) is the sine of x. You must place parentheses around the argument of the function - e.g., type SQR(9) but not SQR 9. Here are some of the functions in Basic:

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.

IF ... THEN

This construction instructs the program to do something whenever a certain condition is fulfilled. If the condition is not fulfilled the program ignores the instruction and just goes on to the next statement.

Examples :

1) IF x > 0 THEN PRINT "x is positive"
2) IF x < y THEN z = x + y
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".

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.)

IF ... THEN ... ELSE

This construction is useful whenever you want the computer to check more than one condition, and to do something different under each condition.

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.

Conditions and Relations

A condition is a logical expression that can be checked as either TRUE or FALSE. These are used in "IF … THEN" constructions to determine whether the program is to execute certain commands. For example, "x > y" is either true or false depending on x and y. The symbol ">" is an example of a relation. Some other relations used in conditions are

<(less than)
>(greater than)
=(equals)
<>(does not equal)
<=(less than or equal to)
>=(greater than or equal to) .

AND and OR

The words AND and OR have special meanings in Basic. They are examples of logical operators, and they appear often as connectors of conditions in "IF … THEN" constructions.

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.

Single and Double Precision Variables

If a variable name ends in a letter, a digit, or with the exclamation point "!", then Basic automatically considers it a so-called floating point single precision variable. Such variables can have values ranging from - 3.4 x 1038 to + 3.4 x 1038, and Basic cuts them off at six decimal places. Some examples of single precision variables are
x X z4 A! age zebra!.
If a variable name ends with the number sign "#", then Basic considers it a floating point double precision variable. These variables can have values ranging from from - 1.7 x 10308 to + 1.7 x 10308, and Basic cuts them off at 15 decimal places. Some examples of double precision variables are
x# Y3# tiger# .

END (in the middle of a program)

An END statement executed anywhere in a program - such as in an IF … THEN sequence, for example - stops the program. A program can have many END statements.

Example :
IF AGE < 0 THEN
PRINT "Error - your age cannot be negative!"
END
END IF