Developer community
0 votes
77 views
asked by Sr Member (5k points) 7 9 12 | 77 views

1 Answer

0 votes
 
The difference between these two forms appears when the increment and/or decrement operators are part of a larger expression. In the prefix form, the operand is incremented or decremented before the value is used in the expression. In postfix form, the value is used in the expression, and then the operand is modified.
 
The following table summarizes the difference between Pre-and Post- Increment and Decrement Operations:
Initial Value of x     Expression         Final Value of y Final      Value of x
  5                                    y = x++                    5                                       6
 5                                     y = ++x                      6                                     6
 5                                     y = x--                     5                                       4
 5                                    y = --x                        4                                     4
 
For example:
 
 
x = 42; 
y = ++x;
 
y is set to 43, because the increment occurs before x is assigned to y. Thus, the line
 
 
y = ++x;
 
is the equivalent of these two statements:
 
 
x = x + 1; 
y = x;
 
However, when written like this,
x = 42; 
y = x++;
 
the value of x is obtained before the increment operator is executed, so the value of y is 42.
In both cases x is set to 43. The line
y = x++;
is the equivalent of these two statements:
y = x; 
x = x + 1;
 
answered by Sr Member (4.8k points) 10 25 31

Related questions

0 votes
1 answer
asked by sunny Expert (11.4k points) 14 23 30 | 13 views
0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
asked by rajesh Guru (39.1k points) 10 22 36 | 1.4k views
0 votes
1 answer
asked by om Sr Member (1.3k points) 8 17 31 | 54 views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users