i++ and ++i

Reply

Join Date: Aug 2004
Posts: 17
Reputation: let us c is an unknown quantity at this point 
Solved Threads: 1
let us c's Avatar
let us c let us c is offline Offline
Newbie Poster

Re: i++ and ++i

 
0
  #11
Aug 26th, 2004
that is simple
++i means pre increment.
i++ means post increment.
consider the programme
..
main()
{
int a=7;
printf("%d\t%d\t%d\t%d\t",++a,a++,++a,a++);
printf("%d\n"a);
getch();
}
the ot put is
10 10 8 8
11
a++ here increments the value and shows it but ++a increments the value and gives it for next operation.ie ++a increments but not show the value.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 7
Reputation: kalinga is an unknown quantity at this point 
Solved Threads: 0
kalinga kalinga is offline Offline
Newbie Poster

Re: i++ and ++i

 
0
  #12
Aug 26th, 2004
if you are using void main()function at the beginning do not use return 0 at the end.

if you use int main()function at the startup use return 0 at the the end..

#include<iostream.h>
int main()
{
int x=5;
cout<<x<<endl; //print 5
cout<<x++<<endl;// the 5 is print first .then the 1 is added
cout<<x<<endl;//the x get 6 becoz 1 is been added
}
----------------------------------
2 program-
#include<iostream.h>
int main()
{
int x=5;
cout<<x<<endl; //print 5
cout<<++x<<endl; //first add 1 and add 5 to it.so the answer is 6
cout<<x<<endl; //the variable now is 6
return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,341
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: i++ and ++i

 
0
  #13
Aug 26th, 2004
Originally Posted by let us c
that is simple
++i means pre increment.
i++ means post increment.
consider the programme
..
main()
{
int a=7;
printf("%d\t%d\t%d\t%d\t",++a,a++,++a,a++);
printf("%d\n"a);
getch();
}
the ot put is
10 10 8 8
11
a++ here increments the value and shows it but ++a increments the value and gives it for next operation.ie ++a increments but not show the value.
You picked the worst example you could -- it is purely undefined behavior.
http://www.eskimo.com/~scs/C-faq/q3.2.html

Originally Posted by kalinga
if you are using void main()function at the beginning do not use return 0 at the end.
If you are using void main(), and are on a hosted implementation, you are incorrect. (It seems to me I may have mentioned this already?)
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: i++ and ++i

 
0
  #14
Aug 26th, 2004
Originally Posted by cscgal
Can someone please explain to me the difference between ++i and i++ when written as part of an expression (i.e. within loops and if statements) ? Thanks
Could I suggest a book?: C++ Primer 3rd Edition

Covers tiny stuff like performance details and the new C++ standard very well,bit advanced but very good if you want details.

Postfix(p++) and Perfix(++p).

Rules
p++ - First use value then increment.
++p - First increment then use value.

If you still dint understand after all the stuff the other guys gave.Then check this out.

You are mad at a guy, he shouts at you,making you madder.With p++ you shout at the guy then slap him and with ++p you slap him first, then shout at him.

The end result is that on the first case he would have got a piece of your mind before getting the physical part for what ever he did, and the other way around with the second.
Which method you want to use depends on the situation and your choice.

(Hint: p++, if you plan on running)

,no offense anyone.

Also,val++ (real c++ here ok) is what is commonly used by many programmers.

As far as loops go it depends on which type of loop you re going to use like
for(), while() , and do while() ,where it's declared and where the expression is placed.Just apply the rules and you will have the result.
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 1,749
Reputation: nanosani is an unknown quantity at this point 
Solved Threads: 55
Team Colleague
nanosani's Avatar
nanosani nanosani is offline Offline
Unauthenticated Liar

Re: i++ and ++i

 
0
  #15
Aug 27th, 2004
Originally Posted by cscgal
Can someone please explain to me the difference between ++i and i++ when written as part of an expression (i.e. within loops and if statements) ? Thanks
Simply ++i increments i first and then does any computation in the loop ...
and i++ first does the computation and then increments i.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 24
Reputation: XianBin is an unknown quantity at this point 
Solved Threads: 0
XianBin XianBin is offline Offline
Newbie Poster

Re: i++ and ++i

 
0
  #16
Aug 27th, 2004
try this:

//---------------------------------
// try_i.cpp
#include <iostream.h>

void main()
{
int i = 1;
int last_value;

last_value = i++;
cout << "i++ = " << i << endl;

last_value = ++i;
cout << "++i = " << i << endl;

cout << "Again:" << endl;
cout << "i++ = " << i++ << endl;
cout << "++i = " << ++i << endl;
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 2
Reputation: varun_sonkerr is an unknown quantity at this point 
Solved Threads: 0
varun_sonkerr varun_sonkerr is offline Offline
Newbie Poster

Re: i++ and ++i

 
0
  #17
Aug 29th, 2004
Originally Posted by cscgal
Can someone please explain to me the difference between ++i and i++ when written as part of an expression (i.e. within loops and if statements) ? Thanks
The functionality of both the syntexes are same but difference lies in preference of operators.
++i means first increement and then process the instruction
i++ means first process the instruction and then increment.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 2
Reputation: varun_sonkerr is an unknown quantity at this point 
Solved Threads: 0
varun_sonkerr varun_sonkerr is offline Offline
Newbie Poster

Re: i++ and ++i

 
0
  #18
Aug 29th, 2004
practical is the key to understand the concept in a better way.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1
Reputation: Wonder is an unknown quantity at this point 
Solved Threads: 0
Wonder Wonder is offline Offline
Newbie Poster

Re: i++ and ++i

 
0
  #19
Sep 30th, 2007
One thing that is good to remember is that ++i is returned by reference while i++ is returned by value.

You should use ++i to increment where the old value of i is not needed.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: i++ and ++i

 
0
  #20
Sep 30th, 2007
Originally Posted by Wonder View Post
One thing that is good to remember is that ++i is returned by reference while i++ is returned by value.
Never heard that one before.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC