Is preIncrement (++ x) and postIncrement (x ++) required

I have never seen usecase for pre-increment and post-increment in real code. The only place I see them most often is puzzles.
My opinion is that it introduces more confusion rather than usefulness.

  • Is there a real use scenario for this
  • Unable to do this using + =

    y = x++

    y = x
    x += 1

+3
increment programming-languages post-increment pre-increment
source share
3 answers

This is just a shorter way to write the same thing, and it only confuses those who do not deeply understand C (a) . The same argument can be made to replace:

 for (i = 0; i < 10; i++) printf ("%d\n", i); 

from:

 i = 0; while (i < 10) { printf ("%d\n", i); i = i + 1; } 

since any for can also be done using while or:

 i = 0; loop: if (i < 10) { printf ("%d\n", i); i = i + 1; goto loop; } 

since any loop construction can be built from conditions and goto . But (I hope) you would not do that, would you?


(a) I sometimes like to explain this to my students as simple statements and side effects, which allows the C-code to be shorter, usually unimportant, or with minimal loss of readability.

For approval:

 y = x++; 

the operator assigns x - y side effect, which then increases x . ++x is the same thing, it's just that a side effect happens in advance.

Similarly, the side effect of assignment is that it is evaluated as an assigned value, that is, you can do things like:

 while ((c = getchar()) != -1) count++; 

and does things like:

 42; 

valid but useless C. instructions

+7
source share

The pre-and post-increment statements make more sense if you consider them in the light of history and when they were conceived.

In the days when C was basically a high-level assembler for PDP-11 machines </flamebait> , and long before we had good optimization compilers, we used regular idioms that used post-increment operators ideally for. Such things:

 char* strcpy(char* src, char* dest) { /* highly simplified version and likely not compileable as-is */ while (*dest++ = *src++); return dest; } 

In the code that was called code PDP-11 (or another) machine language, which heavily used the basic addressing modes (such as relative direct and relative indirect), which included these types of operations before and after the increment and decrement.

So, to answer your question: are languages ​​needed these days? No, of course not. This proves that you need very little in terms of instructions for calculating things. The question is more interesting if you ask: "Are these features desirable?" To this I would answer a qualified "yes."

Using your examples:

 y = x; x += 1; 

against.

 y = x++; 

I see two benefits right from the head.

  • The code is shorter. All I need to know in order to understand what you are doing is in one place (as long as I know the language, naturally!) Instead of spreading. Spreading across two lines seems complicated, but if you make thousands of them, it can make a big difference in the end.
  • It is more likely that the code created even by the crappy compiler will be atomic in the second case. In the first case, this will most likely not be if you do not have a good compiler. (Not all platforms have good, powerful optimizing compilers.)

Also, I find that I say very much that you are talking about += when this in itself is an “unnecessary” way of saying x = x + 1; .... In the end, there is no use case that I can think of for += that cannot be undone with _ = _ + _ .

+6
source share

You accidentally raise a much more serious problem here, and it will become more and more known to you when the years (decades) pass.

Languages ​​often make the mistake of providing “abilities” when they do not. IMO, ++ should be a standalone expression only and absolutely not an expression operator.

Try to keep the following to heart: The goal is not to create code for a competent engineer to read. The goal is to create a code for a competent engineer to read when it is exhausted at 3 a.m. and jump on caffeine.

If an engineer tells you, “All code constructors can cause you trouble. You just need to know what you are doing,” and then leave, laughing, because he just revealed himself as part of the problem.

In other words, never indicate anything like this:

 a[aIndex++] = b[++bIndex]; 

Here you can find an interesting discussion about this: Why avoid increments ("++") and shorten ("-") operators in JavaScript?

0
source share

All Articles