"How to Make a Website" was retired on March 24, 2020. You are now viewing the recommended replacement.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Basics!
You have completed Java Basics!
Preview
In this video, we will learn how to check multiple conditions in one statement using a logical OR statement.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now that we know how
to block a single specific word,
0:01
let's expand our reach and block
more than just one offensive word.
0:03
One useful tool we have
is the logical OR expression.
0:07
This lets us check multiple conditions
in a single if statement.
0:11
For example,
we can say if the noun is dork
0:15
or if the noun is jerk,
then exit the program.
0:18
You can chain as many OR statements
as you want.
0:23
Let's add this logical
or to exit our program if the user enters
0:25
dork or jerk.
0:29
So blocking
jerk as a noun is going to be very easy.
0:32
In fact, the only thing I think I need
to show you is the syntax for or in Java.
0:36
So let's get into our
if statement for dork here.
0:42
And after this method call,
but still inside of the if parentheses,
0:45
we enter a double pipe,
these vertical bars.
0:49
It's right above the Enter or Return key
on most keyboard layouts.
0:52
Now, unfortunately, we can't just
plug in the string jerk on this side.
0:56
We need to be explicit
for both conditions.
0:59
So let copy this dork one and paste it
on the right side of these OR pipes
1:02
Then we just change dork to jerk
1:08
Now this line is getting a bit long
1:12
and if we added another
or it would likely go off the screen.
1:14
So we can actually split it across
multiple lines inside the parentheses
1:17
for readability.
1:22
Much better.
1:26
Now you might be wondering, can't
I just write two separate if statements?
1:28
Well, you could,
but then you'd be repeating
1:32
this code block twice,
which is bad practice.
1:34
It's better to combine conditions in one
if statement to avoid repetition.
1:38
Let's try this out by saving, compiling,
1:43
and running the program.
1:45
I'll enter age, 29, name, Travis,
1:51
big, and noun, jerk.
1:56
The program blocks it right away, great!
2:00
So let's walk that real quick.
So we entered jerk so noun was jerk.
2:03
It checked first if nout
equals dork and that was false.
2:08
Then it moved on and checked
or if it's equal to jerk and that was true.
2:13
So the code block ran.
So the jerk line was true
2:18
so this entire condition was true
because of the or.
2:21
It's just speaking to somebody.
2:25
If you say, I'll give you a ride
if you give me $20, or you ask politely.
2:27
As long as they do one of those,
and as long as you're not a liar,
2:32
you would give them the ride.
2:36
So the condition was true,
and the code below was run.
2:38
After using this,
I think it would be nicer to give the user
2:41
a second chance to enter a polite word
instead of just exiting immediately.
2:44
We'll fix that in the next lesson.
2:49
One more tip.
2:52
You can keep adding more words
by chaining with OR.
2:53
And that is how OR works.
2:59
Great.
3:04
We just learned one way that we can check
multiple conditions in one if statement.
3:05
By not making two identical code blocks,
we avoided repeating ourselves.
3:11
A goal of programming is to make sure
that you don't repeat yourself
3:15
or often referred to as DRY.
As you advance through your learnings
3:19
you'll hear this phrase often and I'm sure
it will be hammered into your thinking
3:23
as you begin coding bigger applications.
While we here thinking about logic,
3:26
I'd like to introduce you to OR's
counterpart, which is AND.
3:31
While OR returns true
if any expression is true,
3:36
AND will only return true
if all expressions are true.
3:39
Logical ANDs
use the double ampersand symbol.
3:43
As this logic is such an important concept
and used all over
3:47
the place, let's go back and review
our dating site analogy.
3:49
So an example of an
3:54
or statement is must love cats or dogs.
3:55
A cat lover would be tested and she'd say,
do you love cats?
3:59
And she'd say,
oh gee, yes, yes, yes, yes, yes.
4:02
There's no need to ask about dogs as this
person definitely met the requirement.
4:06
Now let's look at another example.
4:10
The request is younger than 40
and has no children and loves art.
4:12
This elderly gentleman has prompted
how old he is, and there's no need to
4:18
go on asking any further questions,
as he doesn't meet all the requirements.
4:22
Okay,
so let's take a really quick exercise
4:27
around these statements before we move on.
4:30
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up