English

Differentiate between the following: Post increment and Pre increment operator

Advertisements

Questions

Differentiate between the following:

Post increment and Pre increment operator

What is the difference between post-increment and pre-increment? If a = 4 give values of ++a and a++.

Distinguish Between
Advertisements

Solution

Post Increment Operator

Pre Increment Operator

1. The post increment operator increases the value of a variable by 1 after its current value is used in an expression.

1. The pre increment operator increases the value of a variable by 1 before its new value is used in an expression.

2. In post increment, the original value of the variable is used first, and then its value is increased by 1.

2. In pre increment, the value of the variable is increased by 1 first, and then the updated value is used.

3. The post increment operator is represented by placing the increment operator (++) after the variable.

3. The pre increment operator is represented by placing the increment operator (++) before the variable.

4. The syntax of the post increment operator is variable++.

4. The syntax of the pre increment operator is ++variable.

5. When used in an assignment expression, the post increment operator assigns the original value of the variable before incrementing it.

5. When used in an assignment expression, the pre increment operator increments the variable first and then assigns its updated value.

6. Example: If a = 10, then b = a++; assigns 10 to b and increases a to 11.

6. Example: If a = 10, then b = ++a; increases a to 11 and assigns 11 to b.

shaalaa.com
  Is there an error in this question or solution?
Chapter 6: Variables and Expressions - EXERCISE [Page 132]

APPEARS IN

Dheeraj Mehrotra Computer Science [English] Class 12
Chapter 6 Variables and Expressions
EXERCISE | Q 30. (d) | Page 132
Dheeraj Mehrotra Computer Science [English] Class 12
Chapter 6 Variables and Expressions
EXERCISE | Q 38. | Page 132
Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×