![]() |
| ||
| 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. |
| All times are GMT -4. The time now is 1:14 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC