Difference between post increment and pre increment in hindi
Answers
Answered by
0
On their own, both expressions will have the effect of incrementing the value of variable I. If you use these two expressions as statements by themselves, as in:
I++;
or
++I;
you won’t observe any difference.
The difference between pre-increment (++I) and the post-increment (I++) shows up only when you use the value of the expression. In both of the above statements, the increment of I occurs, but the value of the expression is thrown away, and is not stored or printed or passed anywhere. But if you capture and use the value of the expression, you will observe the difference in behavior. Consider the following code:
int I = 42;int J; J = I++; // post-increment, J will contain 42
When you use the post-increment operator, the value of the expression I++ will be the value of I before it is incremented. Thus, in this example, after the code has executed, I will contain 43, but J will contain 42. The post-increment of I occurs after the expression’s value has been captured.
Now, consider the following code:
int I = 42;int J; J = ++I; // pre-increment, J will contain 43
When you use the pre-increment operator, the value of the expression ++I will be the value of I after it is incremented. Thus, in this example, after the code has executed, I will contain 43 and J will also contain 43. The pre-increment of I occurs before the expression’s value has been captured.
You’ll see the same difference in behavior no matter how you use the expression’s value: as part of a larger expression, as an argument to be printed out, as an argument passed into any other function, etc. And you’ll see a similar behavioral difference when you use the post-decrement (I—) or pre-decrement (—I) operators.
An easy way to remember how these operators behave involves these two rules:
If the operator appears before the operand, the operation will occur beforethe expression has been evaluated.If the operator appears after the operand, the operation will occur afterthe expression has already been evaluated.
While the actual sequence of operations at the instruction level can vary, this set of rules describes the effective behavior from a C
I++;
or
++I;
you won’t observe any difference.
The difference between pre-increment (++I) and the post-increment (I++) shows up only when you use the value of the expression. In both of the above statements, the increment of I occurs, but the value of the expression is thrown away, and is not stored or printed or passed anywhere. But if you capture and use the value of the expression, you will observe the difference in behavior. Consider the following code:
int I = 42;int J; J = I++; // post-increment, J will contain 42
When you use the post-increment operator, the value of the expression I++ will be the value of I before it is incremented. Thus, in this example, after the code has executed, I will contain 43, but J will contain 42. The post-increment of I occurs after the expression’s value has been captured.
Now, consider the following code:
int I = 42;int J; J = ++I; // pre-increment, J will contain 43
When you use the pre-increment operator, the value of the expression ++I will be the value of I after it is incremented. Thus, in this example, after the code has executed, I will contain 43 and J will also contain 43. The pre-increment of I occurs before the expression’s value has been captured.
You’ll see the same difference in behavior no matter how you use the expression’s value: as part of a larger expression, as an argument to be printed out, as an argument passed into any other function, etc. And you’ll see a similar behavioral difference when you use the post-decrement (I—) or pre-decrement (—I) operators.
An easy way to remember how these operators behave involves these two rules:
If the operator appears before the operand, the operation will occur beforethe expression has been evaluated.If the operator appears after the operand, the operation will occur afterthe expression has already been evaluated.
While the actual sequence of operations at the instruction level can vary, this set of rules describes the effective behavior from a C
Similar questions
Math,
7 months ago
English,
7 months ago
Social Sciences,
7 months ago
Business Studies,
1 year ago
Social Sciences,
1 year ago
Science,
1 year ago
Chemistry,
1 year ago