And condition in java example

Tech Tutorials

Tutorials and posts about Java, Spring, Hadoop and many more. Java code examples and interview questions. Spring code examples.

Tuesday, April 12, 2022

Conditional Operators in Java With Examples

The conditional operators in Java- Conditional-AND (&&) and Conditional-OR (||) perform operations on two boolean expressions. Result is also a boolean value of true or false based on whether condition is satisfied or not.

How does Java Conditional operator work

Conditional-OR— If any of the two boolean expressions is true then the result is true. For example in condition (A || B), if expression A evaluates to true then result is true even if expression B evaluates to false. Let’s make it clear with a table.

Java conditional operator example

public class ConditionalDemo < public static void main(String[] args) < int a = 7; int b = 8; int c = 5; // This condition evaluates to true if((a >c) && (b > c)) < System.out.println("a and b both are greater than c"); >// This condition evaluates to false if((a < c) && (b >c)) < System.out.println("a and b both are greater than c"); >// This condition evaluates to true if((a < c) || (b >c)) < System.out.println("OR Condition (a < c) OR (b >c) "); > // This condition evaluates to true if(((a > c) && (b > c)) || (c < 3))< System.out.println("Both AND and OR used - First expression (a >c) && (b > c) is true so OR condition is true "); > > >
a and b both are greater than c OR Condition (a < c) OR (b >c) Both AND and OR used - First expression (a > c) && (b > c) is true so OR condition is true

Short-circuiting behavior of Java conditional operators

These conditional operators (&&, ||) exhibit «short-circuiting» behavior, which means that the second operand is evaluated only if needed.

Читайте также:  Python new line windows

As explained above Conditional-AND evaluates to false if any of the two expressions is false. In that case if first expression evaluates to false there is no need to evaluate the second expression as result is going to be false anyway.

Same way for Conditional-OR if the first expression evaluates to true there is no need to evaluate the second expression because result is going to be true anyway.

Short-circuiting behavior Java example

Let us try to understand this short circuiting behavior with few examples.

In this example class there are two methods getCompValue1() and getCompValue2() and a conditional expression if(getCompValue1(7) && getCompValue2(5)) which is evaluated.

public class SCDemo < public static void main(String[] args) < if(getCompValue1(7) && getCompValue2(5))< System.out.println("Conditional expression evaluates to true"); >else < System.out.println("Conditional expression evaluates to false"); >> static boolean getCompValue1(int num) < System.out.println("In getCompValue1 with value " + num); return num >6; > static boolean getCompValue2(int num) < System.out.println("In getCompValue2 with value " + num); return num >6; > >
In getCompValue1 with value 7 In getCompValue2 with value 5 Conditional expression evaluates to false

Here note that return value of method getCompValue1() is true (7 > 6). That is why second expression (getCompValue2(5)) is also evaluated which evaluates to false (5 > 6). Thus the conditional expression evaluates to false.

Now if expression is changed in the getCompValue1 in such a way that this method returns false then notice what happens.

Class with getCompValue1() method changed

public class SCDemo < public static void main(String[] args) < if(getCompValue1(7) && getCompValue2(5))< System.out.println("Conditional expression evaluates to true"); >else < System.out.println("Conditional expression evaluates to false"); >> static boolean getCompValue1(int num) < System.out.println("In getCompValue1 with value " + num); return num >8; > static boolean getCompValue2(int num) < System.out.println("In getCompValue2 with value " + num); return num >6; > >
In getCompValue1 with value 7 Conditional expression evaluates to false

Now see the output, second expression is not even evaluated, where getCompValue2() method is called. Because first expression itself is false now (7 > 8). That’s what is «short-circuiting» behavior, where second operand is evaluated only if needed.

Another Short-circuiting example

Let us see another example, many times it happens that we check for null or zero and we go on to evaluate expression only if value is not zero or null.

Here first part of the condition verifies if val1’s value is zero or not. If val1’s value is zero expression val1 != 0 itself becomes false so the second expression is not evaluated. That saves you from a run-time exception in case val1 is zero.

That’s all for this topic Conditional Operators in Java With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Источник

Java Operator – &, && (AND) || (OR) Logical Operators

Ihechikara Vincent Abba

Ihechikara Vincent Abba

Java Operator – &, && (AND) || (OR) Logical Operators

We use operators in most programming languages to perform operations on variables.

They are divided into various categories like arithmetic operators, assignment operators, comparison operators, logical operators, and so on.

In this article, we will be talking about the bitwise AND operator, and the AND ( && ) and OR ( || ) logical operators.

How to use the bitwise AND operator

The symbol & denotes the bitwise AND operator. It evaluates the binary value of given numbers. The binary result of these numbers will be returned to us in base 10.

When the & operator starts its operation, it will evaluate the value of characters in both numbers starting from the left.

Let’s look at an example to help you understand better:

System.out.println(10 & 12); // returns 8

The binary value of 10 is 1010

The binary value of 12 is 1100

Here is something you should have in mind before we start the operation:

So let’s carry out the operation.

The first character for 10 is 1 and the first character for 12 is also 1 so:

We move on to the second characters – 0 for 10 and 1 for 12:

For the third characters – 1 for 10 and 0 for 12:

For the fourth characters – 0 for 10 and 0 for 12:

Now let’s combine all the returned characters. We would have 1000.

The binary value 1000 in base 10 is 8 and that is why our operation returned 8.

How to use the logical AND operator

Note that we use logical operators to evaluate conditions. They return either true or false based on the conditions given.

The symbol && denotes the AND operator. It evaluates two statements/conditions and returns true only when both statements/conditions are true.

Here is what the syntax looks like:

statment1/condition1 && statemnt2/condition2

As you can see above, there are two statements/conditions separated by the operator. The operator evaluates the value of both statements/conditions and gives us a result – true or false.

System.out.println((10 > 2) && (8 > 4)); //true

The operation will return true because both conditions are true – 10 is greater than 2 and 8 is greater than 4. If either one of the conditions had an untrue logic then we would get false .

To better understand the && operator, you should know that both conditions must be true to get a value of true .

Here is another example that returns false :

System.out.println((2 > 10) && (8 > 4)); // false

Here, 2 is not greater than 10 but 8 is greater than 4 – so we get a false returned to us. This is because one of the conditions is not true.

  • If both conditions are true => true
  • If one of the two conditions is false => false
  • If both conditions are false => false

How to use the logical OR operator

We use the symbol || to denote the OR operator. This operator will only return false when both conditions are false. This means that if both conditions are true, we would get true returned, and if one of both conditions is true, we would also get a value of true returned to us.

statment1/condition1 || statemnt2/condition2

Let’s go over a few examples.

System.out.println((6 < 1) || (4 >2)); // true

This returns true because one of conditions is true.

  • If both conditions are true => true
  • If one of the conditions is true => true
  • If both conditions are false => false

Conclusion

In this article, we learned how to use the bitwise & operator in Java and how the operation is carried out to give us a result.

We also learned how to use the && and || logical operators in Java. We learned what value each operation returns based on the conditions involved in the operation.

Источник

Logical Operators in Java

Logical operators can be defined as a type of operators that help us to combine multiple conditional statements. There are three types of logical operators in Java: AND, OR and NOT operators.

  • AND operator returns true when both conditions under evaluation are true , otherwise it returns false.
  • OR operator returns true if any one of the given conditions is true . OR operator returns false if and only if both conditions under evaluation are false .
  • NOT operator accepts a single value as an input and returns the inverse of the same. This is a unary operator unlike the AND and OR operators.

Introduction

You may remember AND, OR and NOT gates from your electronic and tinkering labs.

  • AND gate in electrical circuit combined two signals such that the output is on if both signals are on.
  • OR gate, on the other hand, combined signals such that the output becomes on if any of the input signals are on.
  • NOT gate reverses the input signal. (High input to low output and vice-versa)

and so on. Similar functionalities are performed by the logical operators in Java.

  • We can easily use logical operators to combine multiple conditions. Also whenever we tend to apply these operators on two conditions, a logical output is expected.
  • We implement them to achieve a boolean output and control the flow of execution in a program.

Let’s now have a quick look at the features of these logical operators in Java.

Features of Logical Operators In Java

  • Logical operators help us control the flow of execution of our program.
  • We have boolean operators which return only true or false .
  • Logical operators can be easily implemented onto single or multiple boolean operands.
  • There are three logical operators in Java:
    • && (Logical AND)
    • || (Logical OR)
    • ! (Logical NOT)

    Now we have a brief idea about operators, let’s see the detailed explanation and implementation of each logical operator one by one.

    AND Operator

    Syntax — cond1 && cond2

    This operator is named as the Logical AND operator. This operator returns true when both conditions under evaluation are true , otherwise it returns false.

    Short-Circuiting Effect: If the first condition cond1 evaluates to false , it doesn’t carry out a check on the second condition. It returns false without evaluating the second condition.

    We are going to understand the above-mentioned definition with the help of a table that shows us the results of the conditions in which this operator has been used. The table represents the using of AND operator.

    From the table above, we can see that the AND operator returns true only if both conditions (under consideration) are true . Even if one of the conditions is false , the AND operator returns false .

    Explanation:

    • In the code above, we are finding the maximum number out of p , q , r (taken as inputs).
    • If p is maximum among p , q , and r variables then both condition p > q and p > r should be satisfied. Thus we have combined these two conditions using the AND (&&) operator.
    • Similar logic is employed for q . If p and q are not maximum, the maximum is obviously r .

    OR Operator

    Syntax — cond1 || cond2

    This operator is named as the Logical OR operator. This operator returns true if any one of the given conditions is true. OR operator returns false if and only if both conditions under evaluation are false .

    Short-Circuiting Effect: This operator doesn’t check the second condition if the first one is true . The second condition is checked only and only if the first condition is false.

    Let’s see a table for this blog too.

    It is clear from the table above that OR operator returns false if and only if both conditions are false . Otherwise, it returns true .

    Explanation:

    • The code above takes three numbers representing the sides of a triangle as input and finds out if the triangle is valid triangle or not.
    • To solve this, we have used the property: «Sum of two sides of a triangle is always greater than the third side.»
    • Hence, if sum of any two sides of the input three sides is less than or equal to the third side, the triangle cannot exist.
    • Thus, we have used the OR operator as it returns true if any of the conditions are true .

    NOT Operator

    Syntax — !

    This operator is called as Logical NOT operator. This can be used using an exclamation ( ! ) mark. It accepts a single value as an input and returns the inverse of the same. This is a unary operator unlike the AND and OR operators.

    We can make a similar truth table for this operator itself and let’s see how it would look like.

    Here, if the condition is true then the operator returns false i.e. the opposite of true and vice versa.

    Explanation:

    • In the code above, we are simply printing the output of NOT operator on the two conditions: (p < q) and (p >q) .

    XOR Operator

    Syntax — ^

    This is a bitwise operator and stands for «exclusive or» . It performs logical operations as well if the operands are boolean variables.

    From the table above, it is clear that when both the inputs are identical, false is returned. However, if XOR is performed on opposite operands, true is returned.

    Logical Operators Example

    Logical Operators Table

    Operator Example Description
    Conditional AND cond1 && cond2 Returns true only if both cond1 and cond2 are true
    Conditional OR cond1 || cond2 Returns true if atleast one of cond1 and cond2 is true
    Logical NOT !cond Returns the opposite of input argument cond
    Logical XOR cond1 ^ cond2 Returns true only if cond1 and cond2 are different

    Summary

    • Logical operators can be defined as a type of operators that help us to combine multiple conditional statements.
    • There are three types of logical operators in Java:
      • AND Operator (&&)
      • OR Operator (||)
      • NOT Operator (!)

      Also Read

      Источник

Оцените статью