|
You are in Section 1 of 9, Article 1.7 of 1.7
Logical Expressions
Logical expressions, sometimes called boolean expressions, are expressions that result in being true or false. A value of 1 (or more generally any nonzero value) is always associated with a true expression; a value of 0 is always associated with a false expression.
There will be many times when a programmer will have to write code to handle certain boolean (true/false) situations. If an expression is true, then the programmer will issue some command or execute a sequence of instructions. If an expression is false, then the programmer will issue some other command or instruction.
A relational expression is a special type of logical expression in which some type of comparison is performed. The relational operators are >, <, <=, >=, = =, and !=.
logical expression - an expression that returns a value of either true ( 1 ) or false ( 0 ).
relational expression - a special type of logical expression in which some type of comparison is performed.
Relational Operators
One of the biggest problems many beginning programmers have is distinguishing between ( = ) and ( == ). Experienced programmers can even run into problems with these two operators because it is very easy to mistype them when programming.
( = ) is an operator used for assignment purposes; it does not imply equality.
( == ) is a relational operator used to compare two values to see if they are equal; "is equal to".
Be careful not to use ( = ) in relational expressions when ( == ) is needed.
Examples of relational expressions (also logical expressions):
A relational expression is always a logical expression. Logical expressions are either relational expressions or a combination of multiple relational expressions joined together by the logical operators: &&, ||, and !.
Logical Operators
Example of a logical expression (not a relational expression):
(a < b) || ( b < c)
If a = 5, b = 3, and c = 10, the result of this expression is 1 (true).
A quick way to tell if an expression is logical but not relational is if one of the logical operators is being used.
Common Errors:
1) Using = (assignment operator) when == is needed.
2) Using an expression such as:
=> needs to be changed to:
3) Using an expression such as:
=> needs to be changed to:
4) Using && when || is needed or vice versa:
Suppose a variable x is being used and x = -1 and x = -99 have special meaning. We want to test a variable x to make sure it doesn't equal a value having special meaning:
=> is wrong; needs to be changed to:
In general, the operators have precedence (highest to lowest):
- A - arithmetic
- R - relational
- L - logical
NOTE: The only exception is logical not ( ! ), which is evaluated before arithmetic operators.
Specifically, the overall precedence for operators in this tutorial is:
Practical Use Example:
Suppose an insurance company offers a discount on automobile insurance for students who have no prior accidents and a GPA of 3.0 or above. What expression could be used to ask this?
Most compilers also have a capability called short-circuit evaluation when evaluating logical expressions. This makes the evaluation of logical operations much faster and increases program efficiency.
If a compound logical expression involves one expression that is false and an AND ( && ) operation is used, the entire expression must be false. If one expression is true and OR ( || ) is involved, the entire expression must be true.
For an example of how logical expressions can be used in a program, consider the following program:
With logical and relational expressions covered, it's time to learn how program flow can be controlled in C++. Read on for more about the different control structures...
Next: Introduction To Control Structures
You are in Section 1 of 9, Article 1.7 of 1.7
[Back to Top]
|