Monday, February 9, 2009

Additional Compare Concepts

The third group of compares will introduce some additional concepts, namely the logical "AND" and the logical "OR". We assign the value of 77 to the three integer variables simply to get started again with some defined values. The first compare of the third group contains the new control "&&", which is the logical "AND". The entire statement reads, if "x’ equals "y" AND if "x" equal 77 then the result is "true". Since this is true, the variable z is set equal to 33.
The next compare in this group introduces the "||" operator which is the "OR". The statement reads, if "x" is greater than "y" OR if "z" is greater than 12 then the result is true. Since "z" is greater than 12, it doesn’t matter if "x" is greater than "y" or not, because only one of the two conditions must be true for the result to be true. The result is true, so therefore "z" will be assigned the value of 22.

Logical Evaluation

When a compound expression is evaluated, the evaluation proceeds from left to right and as soon as the result of the outcome is assured, evaluation stops. Namely, in the case of an "AND" evaluation, when one of the terms evaluates to "false", evaluation is discontinued because additional true terms cannot make the result ever become "true". In the case of an "OR" evaluation, if any of the terms is found to be "true", evaluation stops because it will be impossible for additional terms to cause the result to be "false". In the case of additionally nested terms, the above rules will be applied to each of the nested levels.

Google Search

Custom Search