hello everyone alex here from
wornoffkeys.com
and in this quick video i'm going to
show you how you can increment and
decrement numbers
using java so in the previous videos i
showed you how to
increase numbers using the addition
operator and also how to subtract
multiply and divide
within java in this video we're going to
be going over how to increment and
decrement
which is just a fancy term for
increasing and decreasing by exactly one
so real quick before we start if you
didn't help with anything
feel free to leave a comment down below
or join the worn off keys discord server
and i'm sure someone will be able to
help you there so here i'm inside of
this empty function
i'm going to start off by creating an
integer we'll just call this number
this will be equal to 50. now if we
wanted to increase or decrease
by one we can use the addition operator
or the subtraction operator that i
showed you in the last video
we could say number equals number plus
one
now of course at this point number would
equal 51
and if we were to decrease this with
minus then it would be 49.
however there's an easier way to do this
that is by saying
number plus plus this is the same exact
thing
as saying number equals number plus 1.
i can demonstrate this by removing this
line and s-o-u-t
with the tab i'll generate this
statement and intellij i can then print
out number
and we are going to run this
so here we see 51 because we are
increasing number right here
and we can also decrease that by using
two minus signs
we can then run this here and we see 49.
now there's certain ways you can do this
we can add the plus signs and the minus
signs on the end
or we can add the plus signs and the
minus signs at the start
now this exact use case there's not
going to be a difference this will still
be 51.
we can confirm this by running the
program one more time and we say 51.
of course if these were minus signs we
will see 49 still
now the reason why you might want to add
them before or after
is because it's going to interact with
that number differently
let me go ahead and get rid of this so
we just have our console log right here
so let's say that i said number plus
plus at the end
if i go to run this we're going to see
50.
the reason for that is because number
equals 50 as declared on line 5
and the plus plus after the name of the
variable means it will
use the variable and then increase the
number
where a plus plus beforehand means it
will increase the number
and then use the variable so in this
case we are going to get 51 instead of
50
because we are saying to increment this
number and then use the variable that is
the result
so if i run this we will see 51. now to
clarify this even further if i add the
pluses back to the end
and then i print out number again now in
the second
print line statement i am not increasing
number whatsoever i'm just printing out
whatever the value is
so this first print line statement will
say 50 and the second one will say 51
because at this point number equals 50
and
then we increase it after using it then
once line 8 comes around
it's already been increased because of
this so this will then say 51.
we can confirm this by running the code
here we see 50
for line 7 and 51 for line 8.
the same exact concept applies to
subtraction
so we can do minus minus afterwards this
will be 50 and then this will be 49
so i can then run the code and we see 50
and 49.
now if i were to move the minuses to the
left hand
side of the number name then we can run
this again and we are going to see
49 and then 49. the reason for this is
we are going to first
decrease or decrement the number
variable and then
use the value so that will then be 49
because we're declaring it as 50.
and then here we're just simply printing
out whatever is inside of number which
is 49
again right here and so this is how
you're going to increment
and decrement using java and this will
become much more clear as to why this is
useful
in future videos in the next video i'm
going to show you something called
compound assignment operators which is a
shorter way to do a lot of the
mathematical operators that we saw in
the last video
thanks for watching this java tutorial
if you need help
leave a comment down below or ask in the
worn off keys discord which will be
linked in the video description
if you want access to the source code
for this video a link to the github
repository can be found in the video
description as well
if you found this video helpful please
consider liking
and subscribing with the notifications
on so you get notified whenever i
release a future java tutorials
