AND

BASin

AND

Logical Operator/Function

AND acts as a logical operator to test the truth of a combination of conditions. Only if all conditions are true is the overall combination true. AND also acts as a function to perform binary operations on two numeric or string values.

How to use AND

As a logical operator, AND links two conditions in a statement where the truth of the whole is to be tested, for example

90 IF x=y+z AND time<10 THEN PRINT "Correct"

Only if both conditions are true will the computer display "Correct". If either or both conditions are false, then the whole combination is false and in this example, the program proceeds to the next line.

AND as a function

As a function, AND can operate on two numeric values, for example

50 LET x=y AND z

AND returns the first value (y) if the second (z) is not equal to 0, and returns 0 if the second value(z) is 0.

AND may also operate on a string value providing it precedes AND. A numeric value must always follow AND, for example

50 LET a$=b$ AND z

AND returns the first value (b$) if the second (z) is non-zero and a null string ("") if the second value (z) is 0.

Note that the BASIC assigns a value of 1 to a true condition and 0 to a false condition, and recognises any non-zero value as true and 0 as false. It does not evaluate combinations of numeric values in accordance with standard truth tables.

Examples

60 LET correct=(x=y+z) AND time<10
70 LET score=score+10*(1 AND correct)
80 LET a$=("Out Of Time Or Not " AND NOT correct)+"Correct"

If the two conditions in line 60 are true then the numeric variable correct is assigned a value of 1. The score is increased by 10 and a$ becomes "Correct". If either of the conditions is false, then correct has a value of 0; score is unchanged, and a$ becomes "Out Of Time Or Not Correct".

Format

  • cond AND cond
  • num-expr AND num-expr
  • string-expr AND num-expr

See also

Chapter 13.