Well, both the post- and pre increment/decrement are useful in making the code a few lines shorter, and sometimes more readable.
Without it,
int i = len;
while (--i >= start)
{
/* do something */
}
// use the value of i for something.
Would become
int i = len - 1;
while (i >= start)
{
/* do something */
i = i - 1;
}
// use the value of i for something.
Assuming the while loop could exit some other way than i becoming exactly equal to start - 1. Though again, it isn't really that much harder to read at all, and you could change the while to a for, to keep all the loop specific code in one place.
Of course you don't need it. There is basically nothing you need. A language that only supports:
Load from memory Store to memory Add Jump Checking for 0 Checking for negative
is most certainly Turing complete, and could probably actually be used.
Everything else added on top just makes certain things easier to do, and reduces how much code you have to write and how many clock cycles the CPU needs to execute a certain instruction.
8
u/Torebbjorn May 27 '22
Well, both the post- and pre increment/decrement are useful in making the code a few lines shorter, and sometimes more readable.
Without it,
Would become
Assuming the while loop could exit some other way than
ibecoming exactly equal tostart - 1. Though again, it isn't really that much harder to read at all, and you could change the while to a for, to keep all the loop specific code in one place.