Logical Operations: Difference between revisions

From BR Wiki
Jump to navigation Jump to search
(edit)
(edit)
Line 5: Line 5:
* OR
* OR


===NOT===
===~ (same effect as NOT)===


{{:NOT}}
{{:~}}


===AND===
===AND===

Revision as of 09:11, 11 January 2012

BR programmers rely heavily on logical expressions and operators for decision-making in their programs. The three logical operators used in BR are:

  • ~ (same effect as NOT)
  • AND
  • OR

~ (same effect as NOT)

The logical unary negation operator ~ reverses the meaning of its operand. The operand must be numeric. Note that in BR, 1 means true and 0 means false.

The ~ operator works as follows:

  • The result is true if the operand is false
  • The result is false if the operand is true.

The effect of the ~ operator is equivalent to the NOT operator, with one difference: ~ works everywhere whereas NOT will only work in If and PRINT statements.

~ is normally used in conjunction with an IF Statement.

The following examples demonstrates the use of ~:

00010 let x =  1 ! 1 is same as true
00020 let y = ~x ! not true evaluates to false, which is 0 in BR


00010 let result = ~( 2 > 5 ) ! 2 > 5 evaluates to false, so ~( 2 > 5 ) evaluates to true, which is 1 in BR
00020 print result            ! since result is 1, that's what will print on the screen


AND

And is a logical operator which evaluates a combination of two logical conditions.

Suppose that we wish to ensure at some point in an application that two conditions are both true before we choose a certain path of execution. In this case, we can use the AND operator, as follows:

00010 if sex == "M" AND age >= 65 then seniorMales += 1;

This if statement contains two simple conditions. The condition sex == "M" determines whether a person is male. The condition age >= 65 might be evaluated to determine whether a person is a senior citizen. The if statement considers the combined condition

sex == "M" AND age >= 65

which is true if and only if both simple conditions are true. If the combined condition is true, the if statement’s body increments seniorMales by 1. If either or both of the simple conditions are false, the application skips the increment. Some programmers find that the preceding combined condition is more readable when redundant parentheses are added, as in:

( sex == "M" ) AND ( age >= 65 )

The table below summarizes the AND operator. The table shows all four possible combinations of false and true values for expression1 and expression2. Such tables are called truth tables.

Operator AND has a higher precedence than operator OR. Both operators associate from left to right.


OR

Or is a logical operator which evaluates a combination of two logical conditions.

Suppose we wish to ensure that either or both of two conditions are true before we choose a certain path of execution. In this case, we use the OR operator, as in the following application segment:

00010 if semesterAverage >= 90 OR finalExam >= 90 then print "Student grade is A"

This statement contains two simple conditions. The condition semesterAverage >= 90 is evaluated to determine whether the student deserves an A in the course because of a solid performance throughout the semester. The condition finalExam >= 90 is evaluated to determine whether the student deserves an A in the course because of an outstanding performance on the final exam. The if statement then considers the combined condition

semesterAverage >= 90 OR finalExam >= 90

and awards the student an A if either or both of the simple conditions are true. The only time the message "Student grade is A" is not displayed is when both of the simple conditions are false.

Below is a truth table for operator OR.


Operator AND has a higher precedence than operator OR. Both operators associate from left to right.