Java Conditional AND and OR
The && operator performs the conditinal AND operation. It compbines two boolean values and the result is true if both the operands are true. The conditional OR || operator is returns true if either of the two operands are true.
Consider a practical example , where we check a patient's seriousness by checking his weight and blood sugar level. An excessive body weigh and high blood sugar level triggers serious health condition and he must see a doctor.
|
If you compile and run this program, you will get the output as follows
C:\Program Files\Java\jdk1.7.0_17\bin>java and You are overweight and have high blodd glucose. You must see doc |
Second Expression is not evaluated if first is false
In the condional AND operator x && y is x is false , the result of the and operation is false irrespective of the value of y. Java in that case does not evaluate y. This speeds up code execution.
The || operator performs the conditinal OR operation. It evaluates two boolean values and the result is true if either one or both the operands are true. Consider a practical example , where we check a patient's Systoloc blood pressure and fasting blood sugar level. We trigger an alarm and ask the patient to see a doctor if either of the two is excessive.
|
If you compile and run this program, you will get the output as follows
C:\Program Files\Java\jdk1.7.0_17\bin>java or You should see a doctor |
The NOT operator is a unary operator and take a single boolean arguments and inverts it. See the folllowing example where we have tried to print true if the patient is healthy ( depending upon both his blood pressure and blood glucose being under control).
|
If you compile and run this program, you will get the output as follows
C:\Program Files\Java\jdk1.7.0_17\bin>java or You should see a doctor |