Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Pre and Post Increament

In C, the increment operator ‘++’ is used to increase the value of a variable by 1. There are two forms of the increment operator:

  1. Pre-increment
  2. Post-increment.

1. Pre-increment (++x):

  • Pre-increment increments the value of the variable before it is used in an expression.
  • The syntax is ++x.
  • The value of x is incremented by 1, and the updated value is used in the expression.

Example:

#include <stdio.h>

int main() {
    int x = 5;
    
    // Pre-increment: increments the value of x before using it
    int y = ++x;
    
    printf("x: %d\n", x); // Output: x: 6
    printf("y: %d\n", y); // Output: y: 6
    
    return 0;
}

In this program, we have a variable x initialized to 5. Using the pre-increment operator ++x, the value of x is incremented by 1 before it is assigned to the variable y. Therefore, both x and y will have a value of 6.

The output of this program will be:

x: 6
y: 6

2. Post-increment (x++):

  • Post-increment increments the value of the variable after it is used in an expression.
  • The syntax is x++.
  • The current value of x is used in the expression, and then the value of x is incremented by 1.

Example:

#include <stdio.h>

int main() {
    int x = 5;
    
    // Post-increment: increments the value of x after using it
    int y = x++;
    
    printf("x: %d\n", x); // Output: x: 6
    printf("y: %d\n", y); // Output: y: 5
    
    return 0;
}

Here, the post-increment operator x++ is used. It assigns the current value of x to y and then increments the value of x by 1. As a result, x becomes 6 while y retains the original value of 5.

The output of this program will be:

x: 6
y: 5

Difference between Pre and Post Increament

Pre-increment (++x)Post-increment (x++)
1Increments the value of x before useIncrements the value of x after use
2Uses the updated value of xUses the current value of x
3Returns the updated value of xReturns the current value of x
4Example:Example:
“`c“`c
int x = 5;int x = 5;
int y = ++x; // y = 6, x = 6int y = x++; // y = 5, x = 6

Pre and Post Increament Practice Problems

1. What is the value of “x” after the following code snippet executes?

int x = 5;
x++;


Explanation: The initial value of “x” is 5. The post-increment operator (x++) is applied to “x”, which increments the value of “x” by 1. After the operation, the value of “x” becomes 6.


2. What is the value of “y” after the following code snippet executes?

int y = 3;
int x = y++;

Explanation: The initial value of “y” is 3. The post-increment operator (y++) is applied to “y” during the assignment to “x”. Therefore, the original value of “y” (3) is assigned to “x”, and then “y” is incremented by 1. After the operation, the value of “y” becomes 4.


3. What is the value of “x” after the following code snippet executes?

int x = 2;
int y = x++ + ++x;


Explanation: The initial value of “x” is 2. In the expression x++ + ++x, the post-increment operator (x++) is applied to the first occurrence of “x” and the pre-increment operator (++x) is applied to the second occurrence of “x”. The post-increment operator returns the original value of “x” (2) and then increments “x” by 1. The pre-increment operator increments “x” by 1 and returns the updated value (4). Therefore, the expression evaluates to 2 + 4, resulting in 6. After the operation, the value of “x” becomes 4.


4. What is the output of the following code snippet?

int x = 2;
System.out.println(x++);
System.out.println(x);


Explanation: The initial value of “x” is 2. In the first println statement, the post-increment operator (x++) is applied to “x”. The original value of “x” (2) is printed, and then “x” is incremented by 1. Therefore, the output is 2. In the second println statement, the updated value of “x” (3) is printed. Therefore, the output is 3.


5. What is the value of “y” after the following code snippet executes?

int y = 5;
int x = ++y + y++;


Explanation: The initial value of “y” is 5. In the expression ++y + y++, the pre-increment operator (++y) is applied to the first occurrence of “y” and the post-increment operator (y++) is applied to the second occurrence of “y”. The pre-increment operator increments “y” by 1 and returns the updated value (6). The post-increment operator returns the original value of “y” (6) and then increments “y” by 1. Therefore, the expression evaluates to 6 + 6, resulting in 12. After the operation, the value of “y” becomes 7.


6. What is the value of “x” after the following code snippet executes?

int x = 3;
x = ++x + x++;


Explanation: The initial value of “x” is 3. In the expression ++x + x++, the pre-increment operator (++x) is applied to the first occurrence of “x” and the post-increment operator (x++) is applied to the second occurrence of “x”. The pre-increment operator increments “x” by 1 and returns the updated value (4). The post-increment operator returns the original value of “x” (4) and then increments “x” by 1. Therefore, the expression evaluates to 4 + 4, resulting in 8. After the operation, the value of “x” becomes 4.


7. What is the output of the following code snippet?

int x = 2;
System.out.println(++x + x++);


Explanation: The initial value of “x” is 2. In the println statement, the pre-increment operator (++x) is applied to the first occurrence of “x” and the post-increment operator (x++) is applied to the second occurrence of “x”. The pre-increment operator increments “x” by 1 and returns the updated value (3). The post-increment operator returns the original value of “x” (3) and then increments “x” by 1. Therefore, the expression evaluates to 3 + 3, resulting in 6. The output will be 6.


8. What is the value of “y” after the following code snippet executes?

int x = 4;
int y = x++ + ++x;


Explanation: The initial value of “x” is 4. In the expression x++ + ++x, the post-increment operator (x++) is applied to the first occurrence of “x” and the pre-increment operator (++x) is applied to the second occurrence of “x”. The post-increment operator returns the original value of “x” (4) and then increments “x” by 1. The pre-increment operator increments “x” by 1 and returns the updated value (6). Therefore, the expression evaluates to 4 + 6, resulting in 10. After the operation, the value of “y” becomes 10.


9. What is the value of “x” after the following code snippet executes?

int x = 10;
int y = x-- - --x;


Explanation: The initial value of “x” is 10. In the expression x– – –x, the post-decrement operator (x–) is applied to the first occurrence of “x” and the pre-decrement operator (–x) is applied to the second occurrence of “x”. The post-decrement operator returns the original value of “x” (10) and then decrements “x” by 1. The pre-decrement operator decrements “x” by 1 and returns the updated value (8). Therefore, the expression evaluates to 10 – 8, resulting in 2. After the operation, the value of “x” becomes 8.


10. What is the output of the following code snippet?

int x = 5;
System.out.println(x--);
System.out.println(x);


Explanation: The initial value of “x” is 5. In the first println statement, the post-decrement operator (x–) is applied to “x”. The original value of “x” (5) is printed, and then “x” is decremented by 1. Therefore, the output is 5. In the second println statement, the updated value of “x” (4) is printed. Therefore, the output is 4.


11. What is the result of the expression

x = 5; y = ++x;

a) x = 6, y = 6
b) x = 6, y = 5
c) x = 5, y = 6
d) x = 5, y = 5

12. What is the value of “x” after the following code snippet executes ?

int x = 3;
int y = x++;

a) 3
b) 4
c) 5
d) 6


13. What is the value of “y” after the following code snippet executes?

int x = 2;
int y = x++ + ++x;

a) 4
b) 5
c) 6
d) 7


14. What is the output of the following code snippet?

int x = 5;
System.out.println(x++);
System.out.println(x);

a) 5, 6
b) 6, 6
c) 6, 5
d) 5, 5


15. What is the value of “y” after the following code snippet executes?

int x = 2;
int y = x++ * ++x;

a) 4
b) 6
c) 8
d) 10


16. What is the value of “x” after the following code snippet executes?

int x = 4;
x = x++ + ++x;

a) 7
b) 8
c) 9
d) 10

17. What is the output of the following code snippet?

int x = 1;
System.out.println(++x + x++);

a) 4
b) 5
c) 6
d) 7

18. What is the value of “y” after the following code snippet executes?

int x = 3;
int y = ++x + x++;

a) 7
b) 8
c) 9
d) 10

19. What is the value of “x” after the following code snippet executes?

int x = 10;
int y = x-- - --x;

a) 8
b) 9
c) 10
d) 11

20. What is the output of the following code snippet?

int x = 5;
System.out.println(x--);
System.out.println(x);

a) 5, 4
b) 5, 5
c) 4, 4
d) 4, 5