In C, we use loops to repeat blocks of instructions. In these loops, there is often an index which counts the number of times the loop has been executed. Each time the loop is executed, this index is incremented, i.e. 1 is added to it. We could write an instruction like :
i = i + 1;
or:
i += 1;
This operation is so frequent, that the designers of Ccreated operators specially devoted to it:
Operator | name | Example | Equivalent |
---|---|---|---|
i++ |
post-increment | a = i++; |
a=i; i=i+1; |
i-- |
post-decrement | a = i--; |
a=i; i=i-1; |
++i |
pre-increment | a = ++i; |
i=i+1; a=i; |
--i |
pre-decrement | a = --i; |
i=i-1; a=i; |
In the case of post-increment, the increment is performed after the instruction. In the case of a pre-increment, the increment is performed first. This difference is essential, because it changes the result of the statements.
Consider the following example of a post-increment:
int a = 5, x;
x = 2 * a++;
In the case of post-increment (a++
), the increment will be done at the end,
after the multiplication (*
) and the assignment (=
). Here are the equivalent instructions:
// Multiplication
x = 2 * a;
// Post-increment
a = a + 1;
After the execution of the instructions, x
is 10 and a
is 6:
Let's go back to the previous example, modifying it with a pre-increment:
int a = 5, x;
x = 2 * ++a;
In the case of pre-increment, (++a
) the increment is done first,
before multiplication (*
) and assignment (=
). Here are the equivalent instructions:
// Pre-increment
a = a + 1;
// Multiplication
x = 2 * a;
After the execution of the instructions, x
is 12 and a
is 6:
In the following code, merge the two operations on a single line with an increment operator:
short x, a=-6;
// MERGE THE TWO FOLLOWING LINES
// WITH AN INCREMENT OPERATOR
a = a + 1;
x = 16 + a;
printf ("x = %d\n", x);
printf ("a = %d\n", a);
In the following code, merge the two operations on a single line with an increment operator:
short x, a=-6;
// MERGE THE TWO FOLLOWING LINES
// WITH AN INCREMENT OPERATOR
x = 16 + a;
a = a + 1;
printf ("x = %d\n", x);
printf ("a = %d\n", a);
In the following code, merge the three operations on a single line with increment operators:
short x, a=0, b=2;
// MERGE THE THREE FOLLOWING LINES
// WITH AN INCREMENT OPERATOR
a = a + 1;
x = a + b;
b = b - 1;
printf ("a = %d\n", a);
printf ("b = %d\n", b);
printf ("x = %d\n", x);
Which are the incrementing operators?
Which is the pre-decrement operator?
--i
.
Try again...
What is the equivalent statement to x = x + 1;
?
x++
already assign the result to variable x
, no need to write x = x++;
Try again...
Merge these two instructions into one:
a = a + 1;
x = a % b;
a
is executed first, this is a pre-increment.
Try again...
Merge these two instructions into one:
x = a * b;
a = a - 1;
a
is at the end, this is a post-decrement.
Try again...
Merge these two instructions into one:
x = a * 2;
a = a + 2;
Merge these two instructions into one:
x1 = x1 - 1;
x = x1 + x2;
x2 = x2 + 1;
x = x + x3;
x3 = x3 - 1;