Logical operators can join many comparisons into one condition.
In the last lesson, you set up a series of if elsea final statements, notice that each condition only
has one comparison.
In this lesson, you're going to connect many comparisons into one condition.
The first thing I'll need you to do is create a new class by yourself inside the Section three project,
create a new file named Logical Operators Java, and make sure the class has a main method.
With a logical operator, we can compare many things at once.
And the most common logical operators are or and end.
We're going to start with the owner operator and this one returns true if either comparison is true.
And there are three parts to the owner operator.
There's the first comparison, and then there's the owner operator represented by two vertical lines
such that if any of the comparisons are true, the entire condition becomes true.
And then there's the second comparison.
So what are some use cases for the or operator?
Well, think about this, you're applying for a scholarship and to get the scholarship you need above
a seventy five in chemistry or in English.
So we're going to make two integer variables.
Chemistry grade set that equal to seventy eight.
An English grade return is set equal to sixty five.
Now we're going to make if statement where they condition and inside the condition, we're going to
check if chemistry great is greater than 75, did they get above a 75 in chemistry or we'll check if
English grade is bigger than 75.
And if either one of these comparisons is true, the entire condition is going to be true, in which
case we're going to print.
Congratulations.
You got the scholarship.
Otherwise, we're going to print.
You didn't get the scholarship.
All right, I think we're ready to run our coat.
And the condition is true, you did get the scholarship.
The or operator checks, if one of the comparisons is true, only one of them has to be true, and the
first comparison is to the entire condition is true and the code runs.
Now we're going to change the students chemistry grade to 67.
We're going to rerun the code.
And this time the code inside Elsa runs.
None of the comparisons are true, so the condition is false, so Jarvis skips over and runs the code
inside ELT's.
You can combine more than two comparisons.
You can combine three comparisons into one condition for comparisons, five comparisons, as many as
you want, as long as one of them is true, the entire condition is going to be true.
And so back in coat, let's say the school decided to be generous.
They're also going to offer you a scholarship if you know Java.
So we're going to make a string variable and set it equal to Java.
And now we're going to add another comparison to the condition, it's going to check if the variable
equals Java, does the student know the Java programming language?
Rewriting our code.
And we got our scholarship back.
The or operator checks if one of the comparisons is true.
First one is false.
The second one is false, but the last one is true to the entire condition is true and the code inside
runs.
Now we're going to talk about the end operator.
And there are three parts to it as well.
There is the first comparison, there's the end operator, which is a pair of ampersands and the second
comparison.
And with the end operation, the condition is only true if every comparison is true.
In this case, the first comparison is false.
So the entire condition becomes false.
So what are some use cases for the end operator?
Well, think about this.
To get your diploma, you need at least 40 credits and a minimum GPA of 2.0.
Now, credits are whole numbers.
So we're going to make it into variable credits and we're going to set that equal to fifty six.
And GPA is a decimal measure.
So we're going to make a double variable GPA and set it equal to three point two.
Now we're going to make another statement.
First, we're going to check if a student has at least 40 credits, credits greater than or equal to
40 and will check if they have a GPA of at least two point zero.
And if the student satisfies both requirements, if both of these comparisons are true, we're going
to print you earned your diploma.
We're sorry, you need at least 40 credits and a minimum GPA of 2.0.
All right, we're ready to run our code.
The condition is indeed true, they got their diploma, the end operator checks, if every comparison
is true, the student has about 40 credits and has a high enough GPA.
So the entire condition is true and the coda runs.
What if we change the students GPA to one point for.
Rewriting our code.
This time, they don't get their diploma.
The end operator needs both comparisons to be true.
The student has more than 40 credits, but there is too low, so the condition is false in the code
runs.
In this lesson, you connected many comparisons to one condition.
The owner operator returns true if either comparison is true now, a student had a seven eight in chemistry
and a sixty five in English, and you made a condition that checks if chemistry grade is greater than
75 or in English, and the first comparison is true.
And whether or not the second one is false or true, it doesn't matter.
The entire condition becomes true and the code inside runs.
Now, the end operator is different, it only returns true if every comparison, if every expression
is true.
Now, a student had 56 credits, but a GPA of one point four and you made a condition that checks if
a student scored above 40 credits and had a minimum GPA of 2.0.
The end operator needs both comparisons to be true, but one of them is false.
This means that the entire condition is false and Java skips over to the L statements.