Onwards, the part two in the first video we went through, the first four operators and we saw a very
cool pattern in operation between whole numbers will return a whole number and an operation between
decimals always returns a decimal.
Now you're going to see the very same pattern as we go through the remaining five operators.
And the first one is the modulus operator, which is expressed by the percent sign and what the modulus
does is it returns the remainder of a division.
When you divide two numbers like ten divided by two, what remains is the modulus, in this case, the
modulus from dividing ten by two is zero.
And in Java, the modulus operation involves the dividend and the deviser, this operation will divide
ten by two, but it won't return the result of the division, it returns the remainder.
But why do we care about the remainder?
It's very useful if you want to identify odd or even numbers when you divide an even number by two like
10.
The remainder is zero.
When you divide an odd number by two, the remainder is not zero, which makes five an odd number.
So we can prove this in our code.
We're going to grab the last two strings from the article.
Comment them out and we're going to put them in print statements.
System, dot, dot, dot, print line.
System out print line.
And we'll just put in both string's.
OK, so at the end of the first string, we're going to connect the modulus from dividing 10 by two.
And in the second string will connect the modulus from dividing five by two.
OK, compile the code and run it, and indeed the modulus from the Vining, ten by two is zero, which
makes ten even and the modulus from dividing five by one is not zero, which makes five an odd number.
All right, let's talk about the ad one operator whose symbol is represented by two plus signs and what
this operator does is it increases the value by one.
And this operation is very common for integer variables because suppose you had a counter variable of
type event which starts at zero.
And when you place plus plus next to the variable, it increases the counter value by one doing so three
times.
The counter value ends up being three.
This is pretty straightforward and let's make it official by doing it in code.
So I'll right int counter.
Is equal to zero and will increase the value by one three times countered plus plus.
Counter plus plus.
Encounter plus plus.
Then I'm going to print system.
Dart out front line.
And we'll connect the counter value.
Compile your code.
And sure enough, the counter equals three.
Conversely, the subtract one operator is represented by two minus signs, and it's a role that's a
decrease value by one.
This operation is also common for integer variables because suppose you had a counter variable that
starts at three and when you place minus minus next to the variable, it decreases the value by one.
Do this three times in the counter ends up being zero.
OK, we can try this in Java.
We're going to write counter minus minus.
Counter minus, minus and one more time, counter minus, minus.
We're going to copy and paste the first print statements.
Recompile the code.
Subtracting one three times, as expected, return zero, because the counter starts at three in each
decrement, removes one from the counter value.
All right, let's talk about the ad buy operator, whose symbol is Express's plus equals the ad buy
operator.
What it does is it adds the value on the right and we already use it in the updating variables video.
You saw an example where a bus started with zero passengers.
The plus equals operator was used to update the value by adding nine the value on the right.
And conversely, we also have a tracked by operator, which you've already seen as well, and this one
subtracts the value on the rights.
We also use this in the updating variables video.
So from the same example, the bus dropped off five passengers, then four.
And in both cases, the minus equals operator subtracts the value on the right.
All right, let's recap, we just used every math operator in Java in every math operation follows a
very nice pattern to study.
Math operation between two whole numbers will always return a whole number and then operation between
decimals will always preserve the decimal.
And now I'm going to recap all the operations we looked at so far, and they are the plus operator to
add values, subtraction, to subtract values, multiplication and division, to multiply and divide
values.
The modulus operator returns the remainder of a division.
The add one operator increases the value by one e subtract one operator decreases a value by one, add
by increases in value by adding the number on the right and subtract by decreases a value by subtracting
the number on the rights.
That is all.