DEV Community

Cover image for Java Programming intelliJ Basics (AND , OR operator)
Deeksha V
Deeksha V

Posted on

Java Programming intelliJ Basics (AND , OR operator)

AND operator:

The && (AND) operator returns true, only if both the expressions are true, and returns false otherwise.
Example code: True condition

public class SecondClass {

    public static void main(String[] args) 
    {
    int TopScore = 100;score = 90;

        if ((score == 90) && TopScore == 100)
        {
            System.out.println("the score is 90 and 100");
        } else {
            System.out.println("The score is not 90 and 100");
        }
Enter fullscreen mode Exit fullscreen mode

In the above code, the variable name TopScore is assigned to 100 and the variable name Score is assigned to 90, in which both the conditions are true, so it returns the output as the score is 90 and 100. Suppose if the any one of the condition doesn't match, it prints the statements which are written in else block, which is the score is not 90 and 100.

Example code: False condition

public class SecondClass {

    public static void main(String[] args) {

    int score = 90;

    int TopScore = 100;

        if ((score != 90) && TopScore == 100)
        { 
            System.out.println("the score is 90 and 100");
        } else {
            System.out.println("The score is not 90 and 100");
        }
Enter fullscreen mode Exit fullscreen mode

OR operator:

The || (OR) operator will result in true, if any one of the condition is true.

Example code

public class SecondClass {

    public static void main(String[] args) {

    int TopScore = 100;

    int HighScore = 150;

        if ((TopScore != 100) || (HighScore == 150))
        {
            System.out.println("the score is 150");
        } else {
            System.out.println("the score is 100");
        }
Enter fullscreen mode Exit fullscreen mode

In the above code displayed, the variable TopScore is set to 100 and the variable HighScore is set to 150. Here the first condition ,TopScore is not equal (!=) to 100. which is false, so it produces the output as the score is 150.or if the second condition, which is HighScore is equal to 150 is matched, then it results in the score is 150. In simple words, it checks whether any one of the condition is true and produces the output.

Top comments (0)