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.

  1. /*
  2.   ReferenceDesigner.com Tutorial for beginners
  3.  Condtional AND example
  4.  */
  5. class and{
  6.  
  7. public static void main (String args[]) {
  8. int bodyweight = 250; // Body Weight in pounds
  9. int fastingglucose = 140; // Fasting Glucose level
  10.  
  11. if ( (bodyweight > 200) && (fastingglucose > 120) )
  12. System.out.println("You are overweight and have high blodd glucose. You must see doc ");
  13. }
  14.  
  15. }


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.

  1. /*
  2.   ReferenceDesigner.com Tutorial for beginners
  3.  Condtional OR example
  4.  */
  5. class or{
  6.  
  7. public static void main (String args[]) {
  8. int bloodpressure = 160; // Systolic
  9. int fastingglucose = 85; // Fasting Glucose level
  10.  
  11. if ( (bloodpressure > 140) ||(fastingglucose > 120) )
  12. System.out.println("You should see a doctor ");
  13. }
  14.  
  15. }


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).

  1. /*
  2.   ReferenceDesigner.com Tutorial for beginners
  3.  boolean NOT example
  4.  */
  5. class not{
  6.  
  7. public static void main (String args[]) {
  8. int bloodpressure = 160; // Systolic
  9. int fastingglucose = 85; // Fasting Glucose level
  10. boolean healthy;
  11. healthy = !((bloodpressure > 140) ||(fastingglucose > 120) );
  12. System.out.println("Health status is " + healthy);
  13. }
  14.  
  15. }


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