473,835 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pre increment and post increment please help me out

19 New Member
hi everyone well i m little bit confused about the use of pre an post increment infact my thinking contradict with the logic of programe
lets consider an example
x=5;
y=x++;
z=x;
after the execution of these statments value of y will b 5 while value of z will be 6
WHY?
value of x++ and x=x+1is equal it first assign a value of x to y then increment in x. why?
expression on the right must be executed first then it must assign value to y
mean to saty value of y must be 6 just like pre increment.
if not then what about the given expression
y=x*3+2;
in this case why it is not assigning a value of x to y first and then solving expression?
although it first solve expression and then assign its value to y.
i m too much confuse about this please help me out
Nov 28 '06 #1
1 2268
horace1
1,510 Recognized Expert Top Contributor
the ++ and -- operators are prefix or postfix
(1) postfix (e.g. i++ or i--) the value of the operand is used in the expression then it is incremented or decremented.
(2) prefix (e.g. ++i or --i) the value of the operand is incremented or decremented and the new value used in the expression.

For example, assuming i = 10
Expand|Select|Wrap|Line Numbers
  1.    k = i++; after assignment k would = 10 and i = 11
  2.    k = ++i; after assignment k would = 11 and i = 11
  3.  
Now, assuming i = 10 and j = 2 consider the expressions (which may be part of a larger expression):
Expand|Select|Wrap|Line Numbers
  1.    i * j++  result = 20, j = 3 (incremented after evaluation)
  2.    i * ++j  result = 30, j = 3 (incremented before evaluation)
  3.    i * j--  result = 20, j = 1 (decremented after evaluation)
  4.    i * --j  result = 10, j = 1 (decremented before evaluation)
  5.  
does that help?

by the way don't do
Expand|Select|Wrap|Line Numbers
  1. i=i++;
  2.  
you don't know if the ++ is done before or after the assignment - it is undefined
Nov 28 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
3194
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a post-increment or post-decrement operator modifies the operand once the entire statement has been executed, not during execution of the statement, so this confused me. An examples given to illustrate the ambiguity is: a = i++; // may increment...
15
1843
by: Robert Swan | last post by:
I'd like to know why the following program outputs 1, and not 0. #include <iostream> class a { int v; public: a():v(0){} a& operator++(int) { v++;
13
6388
by: kailasam | last post by:
Hello, Iam having a doubt. Which is more efficient post increment or Pre increment? I have read that preincrement is efficient than Post increment. Iam not able to think how it is? For an independent statement i++ and ++i which is more efficient? Regards Kailasam
8
3192
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
11
3913
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
1
2783
by: Scott Gifford | last post by:
Hello, I'm working on an providing an iterator interface to a database. The basic thing I'm trying to accomplish is to have my iterator read rows from the database and return constructed objects. My goal here is to abstract away the database part, so that in the future these objects could be constructed from a data source on disk, across the network, etc. The interface I'm after is similar to the standard iterator interface for an...
13
9846
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are handled with the same overload, but I just want to make sure.
5
2868
by: simudream | last post by:
//hi maybe helpful others can look at this code and //tell me why the class code won't behave like //intrinsic types with post and pre increment //Version: 1.00 #include <iostream> using std::cout; using std::endl; using std::ostream;
8
2096
by: bintom | last post by:
Why doe the following C++ code behaves as it does? int i; i=1; cout << (++i)++; Output: 2 i=1; cout << ++(++i); Output: 3 i=1; cout << (i++)++; Output: Error. LValue required i=1; cout << ++(i++); Output: Error. LValue required
0
9810
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10562
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10236
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6968
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3997
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3092
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.