Ad Code

Responsive Advertisement

LOGICAL OPERATORS

Operators with Precedence 3 and 4

Precedence

Operator

Type

Associativity

3

||

Logical OR

Left to Right

4

&&

Logical AND

Left to Right


Precedence refers to the priority given to the operator. The more the precedence, the better is the priority. If both logical OR and logical AND is present in a single statement, logical AND will be executed first since it has greater precedence. To understand the operators with precedence 1 and 2, CLICK HERE.

Logical OR in Java program       

While using conditional statements, logical OR can be used to perform either or operation. This can be explained clearly by using a simple code.

Eg) Consider a student Kalai, who will be permitted to XII standard if she passes either in Computer or in Biology exam. Since it is either or operation, logical OR can be used. Consider pass mark is 40.

        class operator{
            public static void main(String args[]){
            //Assigning marks to Kalai
            byte biology=65;
            byte computer=30;
            //Output is false only when both marks are lesser than 40
            System.out.println(biology>40 || computer>40);
            }
       }

Output

Logical AND in Java program

Consider a scenerio where Kalai is permitted to XII standard only if she passes in English and Chemistry. In this case, Logical AND can be used since it prints true only if all the conditions are satisfied.

        class operator{
            public static void main(String args[]){
            //Assigning marks to Kalai
            byte English=87;
            byte Maths=97;
            byte Biology=58;
            byte Physics=96;
            byte Chemistry=27;
            //Output is false even if one subject mark is lesser than 40
            System.out.println(English>40 && Maths>40 && Biology>40 && Physics>40 && Chemistry>40 );
            }
        }

 Output

Even though, Kalai has passed in all the subjects except Chemistry, the output is false since Logical AND is used.

Combining Logical and Ternary conditional operation

If you want to learn the functioning of Ternary conditional operator, you can CHECK HERE.

Eg) Consider a scenario where Kalai and Ashu will be eligible to write an exam only if they have hall ticket and their Internal mark is greater than 400/500.

        class operator{
            public static void main(String args[]){
            //Assigning marks to Kalai
            short internal_1=479;
            //Storing the presence of hall ticket in Boolean data type
            boolean hallticket_1=true;
            //Assigning marks and hall ticket presence to Ashu
            short internal_2=433;
            boolean hallticket_2=false;
            //Priniting whether Kalai is elligible for the exam
            System.out.print("Kalai ");
            System.out.println(internal_1>400 && hallticket_1==true ? "is elligible for the exam" : "is not elligible for the exam" );
            //Priniting whether Ashu is elligible for the exam
            System.out.print("Ashu ");
            System.out.println(internal_2>400 && hallticket_2==true ? "is elligible for the exam" : "is not elligible for the exam" );
                }
            }

Output


Though Ashu had passed in the internal, since she did not have a hall ticket, she is not eligible to take the exam. Instead of ‘&&’, ‘||’ can be used for the applications that require either or condition.

 

                                                                                                    NEXT


To learn more about Java operators, you can check these useful links:

ASSIGNMENT OPERATORS

LOGICAL OPERATORS

BITWISE OPERATORS

RELATIONAL OPERATORS

SHIFT OPERATORS

  ARITHMETIC OPERATORS

UNARY OPERATORS




Post a Comment

1 Comments