C Programming Tutorial 34 - Increment and Decrement Operators

submitted by mkenny400 on 03/19/18 1

Let's say we have a variable and we want to add one to it.   int pizzasToEat = 123;   The hard way to do this is to go like this:   pizzasToEat = pizzasToEat + 1;   What the assignment operator does here is take the entire expression on the right and evaluate it to a value. So pizzasToEat + 1 = 124. Then, it assigns that value to the variable on the left. So now pizza will equal 124. We can output this data before and after to see this in action.   This is pretty great if you want to add a certain amount to a variable. For example, let's say the local pizza shop goes out of business and since I'm their biggest customer, they sell me 200 pizzas. We could do this:   pizzasToEat = pizzasToEat + 200;   If you are just adding one to a variable, there is actually a shortcut:   pizzasToEat++;   This operator is a unary operator because it only takes one operand, the variable. It is also unique in that it changes the value of the variable. pizzasToEat + 1 does not actually change the value of pizzasToEat unless you assign it back to the variable itself.   The ++ is called the Increment operator and it is one of the most popular operators in programming so please become familiar with it and practice using it.   Now, I'm going to introduce some trivia for you. Let's clean up a bit…int pizzasToEat = 100; is the only thing we have in our code.   What will this output:   int output = pizzasToEat++; printf("Pizzas to eat: %i \n", pizzasToEat);   5…4…3…2…1…   The answer is 100!   This is one of the trickiest things to get used to with the increment operator. We are actually saying we want the ++ to happen after the value is assigned. If we want to say increment the value before it is assigned, we put the ++ in front.   Now let's start with int pizzasToEat = 100 again. What will this output?   int output = ++pizzasToEat; printf("Pizzas to eat: %i\n", pizzasToEat);   5…4…3…2…1…   The answer is 101!   We can print out the value afterwards and see that I'm not lying.   There is also a decrement operator:   pizzasToEat--;   This is the same as doing:   pizzasToEat = pizzasToEat - 1;   These two operator are very foreign to beginners so it is often neglected. So make sure you understand what is going on in this video! If you don't understand, keep trying examples and watch this video a few more times. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Suppor me! www.patreon.com/calebcurry Subscribe to my newsletter: eepurl.com/-8qtH Donate!: bit.ly/DonateCTVM2. ~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~ More content: CalebCurry.com Facebook: www.facebook.com/CalebTheVideoMaker Google+: plus.google.com/+CalebTheVideoMaker2 Twitter: twitter.com/calebCurry Amazing Web Hosting - www.dreamhost.com/r.cgi?1487063 (The best web hosting for a cheap price!)

Leave a comment

Be the first to comment

Collections with this video
Email
Message
×
Embed video on a website or blog
Width
px
Height
px
×
Join Huzzaz
Start collecting all your favorite videos
×
Log in
Join Huzzaz

facebook login
×
Retrieve username and password
Name
Enter your email address to retrieve your username and password
(Check your spam folder if you don't find it in your inbox)

×