zondag 17 juni 2007

Logical Expressions OR AND NOT

Often you test more than one condition. For example, for a character to be a lowercase letter, its value must be greater then or equal to 'a' and less then or equal to 'z'.
To meet this kind of need, C++ provides three logical operators to combine or modify excisting expressions.
The operators are logical OR written ; logical AND, written &&; and logical NOT, written !.

The logical OR operator:
In English, the word or can indicate when one or both of two conditions satisfy a requirement.
The OR operator combines two expressions into one. If either or both of the original expressions is true, or NONZERO, the resulting expression has the value TRUE. Otherwise the expression has the value fales. Here are some examples:

5 == 5 5 == 9 // true because first expression is true
5 > 3 5 > 10 // true because first expression is true
5 > 8 5 < 10 // true because the second expression is true
5 <> 2 // true because both expressions are true
5 > 8 5 < 2 // false because both expressions are false

Because the has a lower precedence than the relational operators, you don't need to use parentheses in these expressions.

C++ provides that the operator is a sequence point. That is any value changes indicated on the left side take place before the right side is evaluated.
For example:

i++ < 6 i == j

Suppose i originally has the value 10. By the time the comparison with j takes place, i has the value 11. Also, C++ won't bother evaluating the expression on the right if the expression on the left is true, for it only takes one true expression to make the whole logical expression true.

The logical AND Operator &&
The logical AND operator, writen &&, also combines two expressions into one.
The resulting expression has the value true only if both of the original expressions are true.
Here are some examples:

5 == 5 && 4 == 4 // true because both expressions are true
5 == 3 && 4 == 4 // false because first expression is false
5 > 3 && 5 > 10 // false because the second expression is false
5 > 8 && 5 < 10 // false because first expression is false
5 <> 2 // true because both expressions are true
5 > 8 && 5 < 2 // false because both expressions are false

Because the && has lower precedence than the relational operators, you don't need to use parentheses in these expressions. Like the operator, the && operator acts as sequence point, so the left side is evaluated, and any side effects are caried out before the right side is evaluated. If the left side is false, the whole logical expression must be false, so C++ doesn't bother evaluating the right side in that case.

With the && operator, you can also set up a series of if else if else statements, with each choice corresponding to a particular range of values!

if (age > 17 && age < 35)
index = 0;
else if ( age >= 35 && age < 50)
index = 1;
else if ( age >= 50 && age < 65)
index = 2;
else
index = 3;

RANGE TESTS
Note that each part of a range test should use the AND operator to join two complete relational expressions:
if (age > 17 && age < 35) // OK

DON'T BORROW FROM MATHEMATICS and use the following notation
if (17 < age < 35) // DON'T DO THIS!
This is valid code, so the compiler does NOT catch this, but the result of this expression is NOT what you want.

The value is:
(17 < age) < 35, 17 < age will result in a zero or one as result of the comparison, 1 or 0 are always < 35.

The Logical NOT operator: !
The ! operator negates, or reverses the truth value of, the expression that follows it. That is, if expression is true NONZERO, then !expression is FALSE zero. and if the expression is false zero, the value of !expression is TRUE.

Usually you can more clearly express a relationship without using the ! operator:

if (!(x > 5)) // if (x <= 5) is clearer

But the ! operator can be useful with functions that return true / false values or values that can be interpreted that way.
For example, strcmp(s1,s2) returns a nonzero (true) value if the two C-style strings s1 and s2 are different from eachother and a zero value if they are the same. This implies that !strcmp(s1, s2) is true if both the strings are equal.

Logical Operator Facts
As mentioned earlier, the C++ logical OR and logical AND operators have a lower precedence than relational operators. This means that an expression such as this

a > 5 && x < 10

is read this way:

(x > 5) && (x < 10)

The ! operator on the other hand, has a HIGHER precedence then any of the relational or arithmetic operators. Therefor to negate an expression, you should enclose the expression in parentheses, like this:

!(x > 5) // is it false that x is greater then 5
!x > 5 // is !x greater than 5

Incidentally, the second expression here is ALWAYS FALSE because !x can have only the values true or false, which get converted to 1 or 0.

The logical AND operator has higher precedence than the logical OR operator. Thus this expression:

age > 30 && age <> 300

means the folowing:

(age > 30 && age <> 300

That is, one condition is that age be in the range 31-44, and the second condition is that weight be greater than 300. The entire expression is true if one of or both of this conditions are true.

You can ofcourse use parentheses to tell the program the interpretation you want.
For example:

(age > 50 weight > 300) && donation > 1000
to get the combination that age be greater then 50 or weight is greater then 300 and donation is greater than 1000.

If you did not use the parentheses, the && operator would use the weight condition and donation condition value.

NOTE
Although the C++ operator precedence rules often make it possible to write compound camparisons without using parentheses, the simplest course of
action is to use parentheses to group the tests, wheter or not parentheses are needed!
It makes code easier to read, it doesn't force someone else to look up some of the less commonly used precedence rules, and it reduces the change of making errors because you don't remember the exact rule that applies.

C++ guarantees that when a program evaluates a LOGICAL EXPRESSION, it evaluates it from the LEFT TO THE RIGHT, and STOPS evaluation as soon as it knows what the ANSWER is.

Example:
x != 0 && 1.0 / x > 100.0

If the first condition is false, then the whole expression must be false. That is because for this expression to be true, each individual condition must be true.

Alternative Representations
Not all keyboards provide all the symbols used for the logical operators, so C++ Standard provides alternative representations.

&& = and
= or
! = not

These words are reserved words, but not keywords because they are alternative representations of existing language features. Incidentally, these are not reserved words in C, but a C program can use them as operators, provided that the program includes the iso646.h header file. C++ does not require using a header file.

Geen opmerkingen: