What is the difference between the conditional AND and OR operators and their logical counterparts? How can incorrect use of the logical operators lead to unintended consequences in your program?

What will be an ideal response?


The Boolean logical AND operator (&) and Boolean logical inclusive OR operator (|) work just like their && and || conditional counterparts, except they do not support short-circuit evaluation. That is, they always evaluate both sides of the expression, no matter what the first evaluation is. This can lead to a side effect, or unintended consequence. For example, in the following statement, the first half of the expression is false, so the second half of the Boolean expression is never evaluated and yearsOfService is not increased.
if (salesAmountForYear >= 10000 && ++yearsOfService > 10)
bonus = 200;
On the other hand, when a single & is used and salesAmountForYear is not at least 10,000, then even though the first half of the expression is false, the second half is still evaluated and yearsOfService is increased.

Computer Science & Information Technology

You might also like to view...

Which of the following is a negative of binary search?

a. It requires significantly more memory than linear search. b. It is slower than linear search. c. The data must be in sorted order. d. None of the above.

Computer Science & Information Technology

Which of the following is NOT a primary detection methodology?

A. signature detection B. baseline detection C. anomaly detection D. stateful protocol analysis

Computer Science & Information Technology

What Linux command is used to create a new directory?

A. md B. mkdir C. make D. dir

Computer Science & Information Technology

Create a class Rational that represents a rational number. It should have private attributes for

• The numerator (an integer) • The denominator (an integer) and the following methods: • Rational(numerator, denominator)—a constructor for a rational number. • Accessor methods getNumerator and getDenominator and mutator methods setNumerator and setDenominator for the numerator and the denominator. You should use an exception to guarantee that the denominator is never zero.

Computer Science & Information Technology