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++.
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 ( |
3. The pre increment operator is represented by placing the increment operator ( |
|
4. The syntax of the post increment operator is |
4. The syntax of the pre increment operator is |
|
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 |
6. Example: If |
