# Logical Operators in Java

\$\$/\$\$

Let us now take a look at how these operations are represented in Java.

\$\$/\$\$

## Video Transcript

So another use case for using logical operators with Booleans would be something like this. We can say that a person can get a loan only if his salary is greater than 50,000 and his credit score or his civil score is greater than 700 and that person is not blacklisted. So notice here I have used this small exclamation symbol. This exclamation symbol is what I called as the not operator in Java. So basically, whenever I say not of anything which is true, it gets converted into the opposite of whatever is the Boolean value. So I'll say that not of true is actually evaluated to false and not of false actually gets evaluated to true. So the not operator is useful in all those cases. Whenever I wish to reverse my any given Boolean value, not of true becomes false and not of false becomes true. So in this situation if I say not of blacklisted, that is I want that the person should not be blacklisted and only then the bank can give the loan to that person. Moreover, if the bank wants to ensure that all these three conditions should be true and only then the bank can give loan to that person, then we can use the and logical operator between these three conditions. So, when it comes to writing the code, the and operator is denoted by using the ampersand symbol twice like this. Similarly, here this and when written in the form of a code can be replaced by two ampersand symbols like this. So basically, my and operator can be denoted by using two ampersand symbols. Now imagine a case in which the bank would be willing to give loan to that person if any one of these conditions are true. That is if either the salary is greater than 50,000 or if the credit score is greater than 700, or if the person is not blacklisted. So, in situations wherein the bank needs to ensure that only one of the conditions being true is sufficient to give a loan, in that case we can use the or operator. So the or operator can be denoted by using two bars like this. You can find this symbol, the bar symbol near the enter key towards the right of your keyboard. So basically, when you wish to use the and operator you can input two ampersand symbols like this. And when you wish to use the or operator you can insert two bars like this. Moreover, the not symbol can simply be denoted as a single exclamation mark. Now imagine that I have three variables a, B and C. Suppose A is true and B is false. So what do you think will be the and operation between these two? So I say that the and between two different Booleans will be true only if both of them are true. Now in this case, since one of them is false, so the and of these two variables will actually evaluate to false similarly, if I wish to perform an or operation on these two variables, I say A or B. So the or operator gives true in all the cases except the case when both of them are false. If you want to remember it, simply just remember that the or of two different Boolean variables will become true if either one of them is true. In this case, a being true is a sufficient condition for A or B to evaluate to true. So the or between true and false is going to give me true. Imagine another case where C is equal to true and D is also equal to true. In this case, performing the or operation will give me what? Well, since both of them are true, and or just merely needs either one of them to be true. So the or operation between these two is going to give me true. And what will be the result of the and operation? We saw here? That and gives true only if all the conditions are met, that is, if all the boolean variables involved in the equation are true. So we say that the and between two true Boolean variables is going to give me true. So, just for your practice, can you tell me what will be the and and or of two Boolean variables in which both of them are false? So think about it and answer in the question that follows.

## Video Recap

Using logical operators with Booleans for loan eligibility

• Loan eligibility criteria: salary > 50,000, credit/civil score > 700, not blacklisted

• Exclamation symbol represents the not operator in Java

• And operator denoted by two ampersand symbols in code

• Or operator denoted by two bar symbols in code

• And operator evaluates to true if both Booleans are true

• Or operator evaluates to true if at least one Boolean is true

• Practice question: and and or of two false Boolean variables

You’re now aware of a few situations where the boolean data type and logical operators can be used. You learnt about three logical operators here:

1. AND (&&)

2. OR (||, generally near the Enter key on your keyboard)

3. NOT(!)

These operators help in condensing a condition into fewer lines; this simplifies your code.

Let us now see the different conditions in which these operations can be used and their results.

\$\$/\$\$

Now you are clear about how these operators work. But what if there are multiple “and” and “or” operations in one statement.  We will take a look at that in the next segment.