Chapter 4- Relational and Logical Operators in Java




Relational Operators in Java

Relational operators are used to evaluate conditions (true or false) inside the if statements.


Some examples of relational operators are:

  • == (equals)
  • >= (greater than or equals to)
  • > (greater than)
  • < (less than)
  • <= (less than or equals to)
  • != (not equals)

Note: ‘=’ is used for assignment whereas ‘==’ is used for equality check.

The condition can be either true or false.


Logical Operators in Java

&&, || and ! are the most commonly used logical operators in Java.


These are read as:

  • && - AND
  • || - OR
  • ! – NOT

The above logical operators are used to provide logic to our Java programs.


  • AND Operator

Evaluates to true if both the conditions are true.

  • Y && Y = Y                
  • Y && N = N
  • N && Y = N
  • N && N = N

Convention: # Y – True and N - False


  • OR Operator

Evaluates to true when at least one of the conditions is true.

  • Y || Y = Y                   
  • Y || N = Y
  • N || Y = Y
  • N || N = N

Convention: # Y – True and N - False


  • NOT Operator

Negates the given logic (true becomes false and vice-versa)

  • !Y = N                         
  • !N = Y
  • else-if clause

Instead of using multiple if statements, we can also use else if along with if thus forming an if-else-if-else ladder.


Using such kind of logic reduces indents. Last else is executed only if all the conditions fail.


/* if (condition1) {

//Statements;

else if {

// Statements;

}

else {

//Statements

} */

Code as Described in the Video :

package com.company;

public class cwh_17_logical {
public static void main(String[] args) {
System.out.println("For Logical AND...");
boolean a = true;
boolean b = false;
// if (a && b){
// System.out.println("Y");
// }
// else{
// System.out.println("N");
// }

System.out.println("For Logical OR...");

// if (a || b){
// System.out.println("Y");
// }
// else{
// System.out.println("N");
// }

System.out.println("For Logical NOT");
System.out.print("Not(a) is ");
System.out.println(!a);
System.out.print("Not(b) is ");
System.out.println(!b);
}
}

Aryadeep

Welcome to CodeWithARYA. It's My Personal Reference Website. Where You Can Get Amazing Codes. Have a Look.

Post a Comment

Previous Post Next Post