| New Reply |
Potential applications of the icrement/decrement operators |
Share Thread | Thread Tools |
| Jun20-12, 12:15 AM | #1 |
|
|
Potential applications of the icrement/decrement operators
So I'm starting to learn how to program and I just learned how these operators work (difference between pre and post, etc.).
However, I have a question, what real application can these operators have in actual software development? I mean, if for instance I have a variable with an X value and I want to print X+1, wouldn't it be the same to use the + operator? Or is it something that has to do with economizing memmory? Thanks. |
| Jun20-12, 10:02 AM | #2 |
|
Mentor
|
Increment and decrement operators (++ and --) were introduced in C back in the 70s and are present in other languages that are descended from C, such as C++, Java, and C#. C was designed to be a mid-level language, somewhere between low-level assembly languages and high-level languages such as Fortran and COBOL. The intent by Brian Kernighan and Dennis Ritchie (the designers of C) was that C would be used to create operating systems. In the interest of performance, Kernighan and Ritchie incorporated a number of operators in C that had direct counterparts in assembly languages. These include the increment and decrement operators. If a number to be changed is already in a register, it can incremented or decremented in one clock cycle. In comparison, adding 1 by ordinary addition to a number that's in a register requires a load instruction for loading another register with 1, and then calling an add instruction to perform the addition. The same is true for decrementing versus subtracting 1 from a value in a register. By reducing the number of operations needed to increment or decrement a number in a register, Kernighan and Ritchie were able to reduce the amount of time needed to perform operations that are used very often in programs. |
| Jul19-12, 09:13 AM | #3 |
|
|
I agree that the compiler will optimize += 1 down to ++ in most cases.
However ++ is a lot quicker to type e.g. Code:
a[i] = 12 // eww i += 1 // this is bad a[i++] = 12 // i prefer this In C or C++, the order of operation of ++ on each side of an expression is not guaranteed by C++ standard therefore avoid stuff this Code:
a++ * ++a; // undefined compiler behaviour |
| Jul19-12, 06:29 PM | #4 |
Recognitions:
|
Potential applications of the icrement/decrement operators
Forgetting about optimising the code (because at this low level, that is the compiler's job, not yours!) there is an important feature of the increment and decrement operators. That is that the are operators, so you can use them inside expressions. "i = i + 1;" is a statement, not an expression, so you can't "embed" it in a bigger expression.
Splitting up "a[i++] = b," into "a[i] = b; i = i + 1;" is longwinded, and if you later realise the code should have said "a[j++] = b;" you have to remember to repalce i with j three times not once. You can also use the operators in ways that would be messy to write without them. For example Code:
while ( a[i++] > b[j++] )
{
/* do something */
}
Code:
while ( a[i] > b[j] )
{
i = i + 1;
j = j + 1;
/* do something */
}
|
| Jul19-12, 06:40 PM | #5 |
|
Recognitions:
|
|
| Jul19-12, 10:00 PM | #6 |
|
|
It is probably a good thing to also consider the nature of do-while and simply while statements in how they check for the termination condition to exit the loop.
The while statement does the check before the start of each iteration and the do-while loop checks it after each iteration. These kinds of things will help you put a lot of this pre-post stuff into context. |
| Jul20-12, 01:14 AM | #7 |
|
Recognitions:
|
Personally I find the second code block more transparent and easier to get right. To me it's far more obvious that the seconds loop terminates with i,j pointing to the first elements in their respective arrays for which the comparison fails (compared with the first one where the loop terminates with i,j both one past this point). |
| Jul20-12, 02:03 AM | #8 |
|
Recognitions:
|
My guess is the main reason that pre and post increment / decrement became part of the C language is that these operations were already included in machine language on some computers at the time the C language was being developed. It provided a simple way for programmers to take advantage of processors that included those operations. The C implementation of these operations just made them a bit more general purpose.
Getting back to the original question, these operations are probably more useful for indexes or pointers as opposed to general data variables. |
| Jul20-12, 08:01 AM | #9 |
|
|
Not wanting to start an argument but I regard readibility as more important than memory usage (in general, unless you're really pushed for resources). The problem is that in a software development environment, (1) programmers work in teams and don't have mind reading skills, (2) after the work is done, the software will live on for 30 years, being maintained by a less-skilled maintenance programmer. (A "code monkey"). I understand sci-programming may be different from commercial development, but there are parallels: one day you will ask your PHD student to make some adjustment to your code, or you will pay a code-shop to add some boring non-domain specific functionality like automatically push the results to the web. In the software engineering community, it is common practice amongst "reputable" shops that if you do anything unusual or which might confuse another guy lookng at your code, you should explain what you have done in a comment. Edit: I am not saying write dumb code. I am saying, in many cases, readability is important. |
| Jul20-12, 08:45 AM | #10 |
|
|
C/C++ = assigment is an operator (the = operator) and has value as an expession equal to the right hand side. Code:
a = i = i + 1; // valid, defined in the C standard |
| Jul20-12, 09:13 AM | #11 |
|
Mentor
|
Assignment expression: i = i + 1 Assignment statement: i = i + 1; The only difference here is the semicolon at the end. As rorix_bw already noted, the value of an assignment expression is the value that is stored in the variable on the left side of =. That's something that eludes beginning C/C++ programmers a lot, when they confuse assignment (=) with equality (==). Code:
x = 5;
if (x = 2)
{
printf("x is 2");
}
else
{
printf("x is %d", x);
}
|
| Jul22-12, 12:56 AM | #12 |
|
|
Yes the semantics of the natural language were unclear, but I thought the example was really bad and needed additional explanation, especially when = instead of == is such a common error.
|
| New Reply |
| Thread Tools | |
Similar Threads for: Potential applications of the icrement/decrement operators
|
||||
| Thread | Forum | Replies | ||
| decrement with J-K FF and gate/gates | Engineering, Comp Sci, & Technology Homework | 13 | ||
| Vector Potential applications in Aharonov-Bohm effect | Quantum Physics | 3 | ||
| logarithmic decrement | Calculus & Beyond Homework | 3 | ||
| 1d potential and switching between operators | Advanced Physics Homework | 2 | ||
| Potential Energy Based On Order Of Force Applications? | Classical Physics | 2 | ||