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 () 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 () below!
You can also find further discussion and get answers to your questions over in #get-help.
Agree with a comment or answer? Like () to up-vote the contribution!
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
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