FAQ: Operators: Lesson - Increment and Decrement

This community-built FAQ covers the “Increment and Decrement” exercise from the lesson “Operators: Lesson”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C

FAQs on the exercise Increment and Decrement

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Order of operations

According to the order of operations, addition should come first (incrementation (++) is a fancy way of addition.

Now, let’s consider the following code from: Course Front end engineer, Module 11 (Javascript syntax part II), Learn javascript syntax: Loops, exercise: Do… While Statements.

// Write your code below

var cupsOfSugarNeeded = 2;

var cupsAdded = 0;

do {

cupsAdded++;

console.log(cupsAdded);

} while(cupsAdded < cupsOfSugarNeeded) {

cupsAdded++;

console.log(cupsAdded);

}

If I would do this: console.log(cupsAdded++);

The variable only seems to be incremented after the method has been executed.

Though this doesn’t seem to comply with the order of operarion?

What would be the answer?

Thomas Hoyle, from The Netherlands.

This is a bit tricky because they showed these operators in the order of operations table but didn’t bother to explain how they behave when used in an expression.

In most languages that have inherited the C-style syntax (C/C++, Java, C#, yes even JS), there are actually two different version of the increment and decrement operators - prefix and postfix (or pre-increment and post-increment):

++a; // pre increment
a++; // post increment
--a; // pre decrement
a--; // post decrement

The difference between them is when the increment happens - in the pre-increment form, the variable is reassigned before the expression, however in the post-increment form it’s reassigned after.

Truth be told most people use these as standalone operators to increment or decrement a single value, their use inside of more complex expressions is kind of a weird thing to do. People are generally more familiar with the post-increment form (a++), but in practice if you’re trying to do math with these you probably want the pre-increment form.

The best advice would be just don’t do stuff like this at all, it leads to harder to understand code for no real benefit :slight_smile:

int a = 3;
// The next example can be tricky to reason about - the expression evaluates *before* the increment
float b = a++ / 2.0;  // now a = 4, but b = 1.5

// Lets reset 
a = 3;
// This behaves more as you might expect, the variable is incremented before the expression is evaluated
int c = ++a / 2;  // now a = 4, c = 2

Here’s a reference on this: