Ad Code

Responsive Advertisement

RELATIONAL OPERATORS

 

Relational equal to and not equal to operators



Relational operators include = = and ! = operators which possess precedence value of 8. 

Precedence

Operator

Type

Associativity

8

= =

Relational is equal to

 

Left to Right

! =

Relational is not equal to


The operator ‘= =’ is used to check whether the Left hand side variable is equal to its right hand side variable. If the condition is satisfied, True is printed in the output else False is printed. So these operators prints out Boolean outputs. If you want to learn more about precedence of operators, CHECK HERE.

         In case of ‘! =’ operator, the output is false when the inputs are same and the output is true when the inputs are different.

Simple Java program to understand Relational equal to and not equal to operators

        class operator{
            public static void main(String args[]){
            //Case 1:
            // Assigning 2 same inputs
            byte num_1=11, num_2=11;
            //Case 2:
            // Assigning different inputs
            byte num_3=11, num_4=29;
            //Comparison of the input values using Relational equal to operator
            System.out.println("num_1 is equal to num_2 " + (num_1 ==                         num_2));
            System.out.println("num_3 is equal to num_ 4 " + (num_3 ==                     num_4));
            //Comparison of the input values using Relational not equal to                     operator
            System.out.println("num_1 is equal to num_2 " + (num_1 !=                         num_2));
            System.out.println("num_3 is equal to num_ 4 " + (num_3 !=                         num_4));
            // '+' means concatenation operation for printing both the statement             and output
                }
            }

 Output

Relational Operators with precedence 9


Similar to the earlier relational operators, these precedence 9 operators are used to check the condition and execute Boolean outputs. This can be validated by a simple Java program.

Java program to understand Relational operators with precedence 9

        class operator{
            public static void main(String args[]){
            // Assigning 2 numbers
            short num_1=111, num_2=211;
            //Comparison of the input values using precedence 9 relational operators
            System.out.println("num_1 is lesser than num_2 - " + (num_1 < num_2));
            System.out.println("num_3 is lesser than or equal to num_ 4 - " + (num_1 <= num_2));
            //Comparison of the input values using Relational not equal to operator
            System.out.println("num_1 is greater than num_2 - " + (num_1 > num_2));
            System.out.println("num_3 is greater than or equal to num_ 4 - " + (num_1 >= num_2));
            // '+' means concatenation operation for printing both the statement and output
            }
        }

 Output

Java code using Logical, relational and Ternary Conditional Operators

If you want to know more about Ternary conditional operator, you can CHECK HERE.

Scenario

Consider Muthu will get a course based on her final marks (out of 500). If her mark is greater than or equal to 470, she will get MBBS seat and if her mark is within 430 to 470, she will get Engineering seat and if it’s lesser than or equal to 430, she will get Diploma seat. Write a simple Java Program to predict the seat which she will get.

        class operator{
            public static void main(String args[]){
            // Assigning Muthu's marks
            short marks=433;
            //Comparison of Muthu's marks using relational operators
            System.out.println("Muthu is "+ ((marks>=470)?"eligible for Doctor seat": "not eligible for Doctor seat"));
            System.out.println("Muthu is "+ ((430<marks && marks <470)?"eligible for Engineering seat": "not eligible for Engineering seat"));
            System.out.println("Muthu is "+ ((marks<=430)?"eligible for Diploma seat": "not eligible for Diploma seat"));
            // '+' means concatenation operation for printing both the statement and output
                }
            }

Output






                                                                                                                                        NEXT

Post a Comment

0 Comments