![]() |
| ||
| i++ and ++i 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 :) |
| ||
| Re: i++ and ++i Quote:
int i = 4; //Now say their is a function nammed goFetch(int), and we wanted to pass an increment of i to it //i = 4 before this code gets touched, the value 4 would be passed, and then i would become 5 goFetch(i++); //i = 4 before this code gets touched, i gets incrimented before anything else (and becomes 5), and the value 5 would be passed goFetch(++i); So, ++i means to incriment first then give the incrimented value, and i++ means to give the original value, then incriment. |
| ||
| Re: i++ and ++i You asked about loops and if's. Here's some: LOOPS for (int i = 0; i < 10; i++) - vs - for (int i = 0; i < 10; ++i) no effective difference. for (int i = 0; ++i < 10; ) - vs - for (int i = 0; i++ < 10; ) Here, the first one will loop 9 times (1..9), the second one 10 (0..9). In both, the value of i in the body of the loop will be 1-based rather than 0 based. IF/WHILE if (i++ < 10) while (i++ < 10) - vs - if (++i < 10) while (++i < 10) The second set will execute one less than the first set, same as the for (). This is 'undefined' and tends to work differently on different compilers, and differently between optimized and non-optimized builds: int i = j++ + j++; int i = ++j + j++; and the like. And though this might sound silly, consider this: #define MAX(x,y) ((x > y) ? x : y) That's a big advantage of small inlined functions vs #defines; the lack of those side effects: inline int MAX(int x, int y) { return ((x > y) ? x : y); } |
| ||
| Re: i++ and ++i It`s simple :) int i; |
| ||
| Re: i++ and ++i if you still dont get it just cout << and it will all become clear |
| ||
| Re: i++ and ++i hi all. . for ( int i =0; i >10; i++; )here i will be 0 in the first time code run .. thats mean the code will be done 10 times for ( int i =0; i >10; ++1; )here i will be before the program done the first time .. thats mean the code will be done 9 times |
| ||
| Re: i++ and ++i huh? First just a nit: it should be i < 10, not i > 10, or else the loop won't execute at all. Second, the i++/++i (another nit: you said ++1) is done at the BOTTOM of the loop, after the code in the braces, so ++i and i++ won't affect the loop either way. Or am I sniffing too many whiteboard pens again? |
| ||
| Re: i++ and ++i yaeh i am sorry for this mistake i really i was tired when i wrote this reply but i am soorry again .. :( #include <iostream> // This line of code is necessary to include all the C++ standard functions\classes definitions read this tutorial carefully u will get it .. |
| ||
| Re: i++ and ++i Please stop using void main. |
| ||
| Re: i++ and ++i Quote:
++i: increment the i's current value by 1 before doing the calculation or doing the comparison. Hence, using in for loop, we cannot see clearly the differences. However, using in if statement, they are clearly different. For instance, we have i = 4. if (i++ == 4) // the result is true since the current value of i is 4. After doing the comparison, then the i's value now becomes 5. Whereas, if (++i == 4) // the result is false since the value of i is increment first, now the value of i becomes 5, then it does the comparison. I hope it will help you. |
| ||
| 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:
|
| ||
| Re: i++ and ++i He's referring to the way you write them when you wish to overload the operators. http://www.parashift.com/c++-faq-lit...html#faq-13.14 However, 1. He's 3 years too late with the information. 2. It's off topic as the thread was about how the prefix and postfix forms affect integers, and not about how to write overload versions. |
| ||
| Re: i++ and ++i Quote:
Hi, As i++ is used in a statement, then the valude of i in the statement, remains same ie unincremented. But after the statement, this is incremented. When ++i, is used then it is incremented with in the statement it self. bye |
| ||
| Re: i++ and ++i hi when inside a loop for example for(i=0;i<10;i++); here the loop starts from 0, and in the case of ++i; the loop starts from 1, in the former the assignment is before increment in the later the assignment is after i gets incremented |
| ||
| Re: i++ and ++i >>and in the case of ++i; >>the loop starts from 1, No it doesn't -- the loop still starts with 0 because the i is not incremented until after the iteration of that loop finishes. |
| ||
| Re: i++ and ++i i++ first takes the value of i and then gives the increment----(POST INCREMENT OPERATOR) whereas ++i first gives the increment to the assigned i value---(PRE INCREMENT OPERATOR) example::: i=6 i++ takes i and then gives increment i.e.,i++=5 but the compiler stores the value as 6 which is not the displayed output ++i gives increment first i.e., ++i=6... void main()input: enter an int:5 output:5 7 HERE THE VALUE OF ++i IS 7 COZ THE COMPILER STORES THE VALUE OF i++ AS 6... |
| ||
| Re: i++ and ++i WTF.. All she asked was the difference between ++i and i++, not such weird details. Can't you just say that, ++i will first increment the value of i and then assign it., while i++ will first assign the value and then increments (so the variable assigned before gets to keep the old value of i). |
| ||
| Re: i++ and ++i Quote:
|
| ||
| Re: i++ and ++i xDD Totally, kekeke.. Wonder what they'll throw at us if we ask the difference between a long int and a short int. kekeke |
| ||
| Re: i++ and ++i Don't mean to hijack this thread, but yes there may be a difference, but then again there might not be one also. depends on the compiler, which I'm sure you already know but other readers may not. |
| ||
| Re: i++ and ++i Hehe, xD |
| ||
| Re: i++ and ++i void main() { int i=3; printf("%d,%d,%d",i,i++,++i); getch(); } o/p: 5,4,4 if we r using ++i it will increment and it will asign to i so ans is 4 if we r using i++ it will increment but it assign to i so ans is 4 b'coz of i++ i value is 5.for clarifications execute the program |
| ||
| Re: i++ and ++i Not only is your example horribly broken, it's not even a valid example of the point you're trying to make. >void main() Implementation-defined behavior. main is required to return int. >printf("%d,%d,%d",i,i++,++i); Undefined behavior. The commas in an argument list do not constitute sequence points, and ++ modifies its operand. Undefined behavior. You didn't include stdio.h, and functions that take variable arguments require a prototype in scope. >getch(); Implementation-defined behavior. You didn't include a header that declares getch, and getch isn't a standard function. |
| ||
| Re: i++ and ++i Haahaa, good one Narue, But hell yeah that example will only give out errors. Btw, balu116, please use the code tags. Posting source code like that is rude. |
| ||
| Re: i++ and ++i When did this become the noob gets it wrong thread? It seems a magnet for 1-post people to chip in with their "interpretation" only to be corrected for getting some aspect of "side effect" or "sequence point" wrong. |
| ||
| Re: i++ and ++i xD but it can't be helped but corrected. Since others who are new here might get it wrong (which happens a lot). A good code writing practice is always appreciated. |
| All times are GMT -4. The time now is 2:07 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC