![]() |
| ||
| Re: i++ and ++i 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. |
| ||
| Re: i++ and ++i 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; } |
| ||
| Re: i++ and ++i Quote:
http://www.eskimo.com/~scs/C-faq/q3.2.html Quote:
|
| ||
| Re: i++ and ++i Quote:
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. |
| ||
| Re: i++ and ++i Quote:
and i++ first does the computation and then increments i. |
| ||
| Re: i++ and ++i 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; } |
| ||
| Re: i++ and ++i Quote:
++i means first increement and then process the instruction i++ means first process the instruction and then increment. :cool: |
| ||
| Re: i++ and ++i practical is the key to understand the concept in a better way. |
| ||
| Re: i++ and ++i 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. |
| ||
| Re: i++ and ++i Quote:
|
| All times are GMT -4. The time now is 5:21 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC