>>Let's continue our study of logical operators by looking at an "or" statement. English and
Java are very similar. Obviously, English usage inspired Java, and so the words can
mean very similar things. An example of this is the and operator and the not operator.
Now Java's a little more precise than English is, but basically they mean the same thing.
But English and Java can be very different. An example of this is the word "static".
Now we've seen the word static all over Java programs. You know, public static void main,
but we really haven't discussed what it means. What I can tell you is it doesn't mean anything
like the English word static means. Another example of a word that's different in Java
than it is in English is or. So let's talk about "and" first. When we say: "I'll go to
the movies and I'll go to the store," it's only true if you do both things. Now that's
true in English, and it's also true in Java. So and works exactly like English in Java.
"Or" is a little bit more complicated. If I say: "I'll go to the movies or I'll buy
milk," the English usage is actually not completely clear. People figure out what it means from
the context. Now that kind of behavior is okay in human language, but it would never
be able to work in a computer. So most of the or's in English are what we call exclusive
or's. And what that means is for the statement to be true, one or the other has to be true,
but not both.
The thing is though-- English is inconsistent. So suppose a retailer asks you the following
question on a web survey. And by the way, I took this example from a real web survey
that I filled out last week. They ask you: "Where do you shop for our brand? In the store
or through the catalog, online or through the catalog, online or in the store?" Well
in this case they clearly mean that to be an inclusive or-- that is, if you shop both
through the store and in the catalog, that first choice would actually be true. So this
is probably an inclusive or. But once again these things are hard to figure out in English
and English isn't very precise.
Now when you're dealing with programming with the Java or, programming languages have to
be consistent. And the Java or is an inclusive or. So what that means is if we take the statement:
x percent 2 equals equals equals 0, remember that's a fancy way of saying x is even? Or
x percent 3 equals equals 0, which is the fancy way of saying x is divisible by 3. It's
true for numbers that are divisible by 2, otherwise known as the even numbers. It's
true for number's divisible by 3, and it's also true for numbers that are divisible by
both 2 and 3, otherwise known as numbers that are divisible by 6. And of course if you have
a number that's not divisible but 2 and not divisible by 3, the statement would be false.
So that's an inclusive or. Let's talk about exclusive or's in Java. Suppose we want to
find if an int value was outside of the ranger from one to a hundred, inclusive. Now this
is actually an exclusive or, because there is no single value that's less than 1 and
bigger than 100 at the same time. The thing is inclusive or works just fine here. We can
say value less than 1 or value greater than 100. The fact that these two statements can't
be true at the same time really doesn't matter at all. It's immaterial.
Now very rarely will we need an exclusive or operator, although there are some rare
examples. Let's think about cryptography. In cryptography you need to sometimes find
numbers that are not a multiple of two other numbers. And so in that case we might want
to know if something is a multiple of 2 or 3, but not a multiple of 6. Now this is kind
of a hard explanation to give, 'cause we don't know that much about cryptography yet. But
trust me - this is a real problem. Just the numbers aren't 2 and 3, they're bigger numbers
than that.
Now you could use a conditional statement to look for the case when both of these statements
are true. And that's actually the most efficient way to do it. Java does in fact have an exclusive
or operator. It's symbol is a carrot, a carrot looks like a little hat, there's one in the
PowerPoint for you-- but it works slightly differently. And the slight difference is
really beyond the scope of this class. The sort of colloquial name for this Xor, for
an exclusive or, which means that one or the other is true, but not both. Let's take a
look at the code for the cryptography example. We have public static Boolean divisible by
2, Xor 3 int value. So what this is supposed to do is take the parameter value and determine
whether it is or is not divisible by 2, exclusive or, whether value is divisible by 3.
So first we'll create a variable divisible by 2 and we'll give it the value of value
percent 2 equals equals zero. In other words, divisible by 2 will be true when value is
even. Then we create another Boolean called Boolean divisible by 3. And it's going to
get the value of value percent 3 equals equals zero. In other words it will be true when
value if divisible by 3. Now we're going to have to do 2 separate tests. One to see if
they're both true, and in that case we would return false. So that's that first conditional
statement there. If divisible by 2 and divisible by 3, we return false. Now once we've done
that we can use our regular exclusive or. So we'll say if divisible by 2 or divisible
by 3, return true. Now something that's very important here is to realize that the order
of these if statements is absolutely essential. You have to check for both of them first.
You have to use the "and" first and then the "or". Because if you use divisible by 2 or
divisible by 3 with that inclusive or and you do it first, you'll return true when you
should be returning false. So that would be a serious problem. So keep programming.