degibson
Golden Member
- Mar 21, 2008
- 1,389
- 0
- 0
Originally posted by: Crusty
Originally posted by: tuteja1986
// thingy loops![]()
#include <iostream>
using namespace std;
int main() {
for (i = 0; i <= 3; i++) {
code goes here
}
return 0;
}
Read your book :! read them :! memories every single thing from
int to for to while to array to if statements to functions to classes to fstream :!
Programming not that hardits only people look at it in a way "OMG I am confused" ... and i am like , that thing is less confusing then chicks or in some case , you find it much easier to learn the basic of c++ then to try to sleep with a AAA chick
![]()
![]()
Originally posted by: deveraux
Originally posted by: slugg
Originally posted by: JACKDRUID
Originally posted by: slugg
^^ ++i is faster than i++, although most compilers will find this an optimize it for you. Careful - they're NOT the same! In this case, it's fine. Notice how the conditional is now i < 3.
umm.. i thought the only difference between ++i and i++ is the order of operation... ++i would be faster because its doing 1 less loop...
i++ copies i to a new memory space, increments it, puts it on the stack, then later assigns it back to i once other operations have been popped off the stack.
++i increments i in its already existing memory space.
These two methods are the reasons behind WHY it makes a difference in the order of operation, so you're kind of right.
The satements i++ and ++i, by themselves, are logically equivalent. However, if you combine a pre/post-increment operation with an other operation, then it would make a difference due to the order the stack is pushed/popped. Example:
int i = 0, j = 0, x, y;
x = i++;
y = ++j;
^^ After running that, your results are:
x == 0
y == 1
i == 1
j == 1
So as you can see, if you're using a counter in a for loop, ++i or i++ doesn't make a logical difference, but ++i is technically faster. Not something you really have to worry about, since most compilers will optimize it for you, but just an interesting tidbit. There is no practical reason to know this, I'm just a nerd, lol. If you really want to understand how it works, look up how to implement a for loop in assembly. You'll see that the "best" way is to do the analogous equivalent to ++i.
Thanks for the explanation. Been coding for years and I never really stopped to think about i++ or ++i other than logical uses of it. But what you said makes sense and is definitely useful to know!
Originally posted by: IHateMyJob2004
Originally posted by: deveraux
Originally posted by: slugg
Originally posted by: JACKDRUID
Originally posted by: slugg
^^ ++i is faster than i++, although most compilers will find this an optimize it for you. Careful - they're NOT the same! In this case, it's fine. Notice how the conditional is now i < 3.
umm.. i thought the only difference between ++i and i++ is the order of operation... ++i would be faster because its doing 1 less loop...
i++ copies i to a new memory space, increments it, puts it on the stack, then later assigns it back to i once other operations have been popped off the stack.
++i increments i in its already existing memory space.
These two methods are the reasons behind WHY it makes a difference in the order of operation, so you're kind of right.
The satements i++ and ++i, by themselves, are logically equivalent. However, if you combine a pre/post-increment operation with an other operation, then it would make a difference due to the order the stack is pushed/popped. Example:
int i = 0, j = 0, x, y;
x = i++;
y = ++j;
^^ After running that, your results are:
x == 0
y == 1
i == 1
j == 1
So as you can see, if you're using a counter in a for loop, ++i or i++ doesn't make a logical difference, but ++i is technically faster. Not something you really have to worry about, since most compilers will optimize it for you, but just an interesting tidbit. There is no practical reason to know this, I'm just a nerd, lol. If you really want to understand how it works, look up how to implement a for loop in assembly. You'll see that the "best" way is to do the analogous equivalent to ++i.
Thanks for the explanation. Been coding for years and I never really stopped to think about i++ or ++i other than logical uses of it. But what you said makes sense and is definitely useful to know!
I love people that freak out over hte difference. If this micro-optimization matters in the real world, tell me how so. I still have yet to find a reason to care. Use i++ at all times. The simple reason is that 90% of real world programmers will only get confused by ++i. There is no difference and ++i can only waste time due to people scratching their head.
<== 8+ years in Java and C++ in everything from VxWorks to Windows.
Originally posted by: IHateMyJob2004
Originally posted by: deveraux
Originally posted by: slugg
Originally posted by: JACKDRUID
Originally posted by: slugg
^^ ++i is faster than i++, although most compilers will find this an optimize it for you. Careful - they're NOT the same! In this case, it's fine. Notice how the conditional is now i < 3.
umm.. i thought the only difference between ++i and i++ is the order of operation... ++i would be faster because its doing 1 less loop...
i++ copies i to a new memory space, increments it, puts it on the stack, then later assigns it back to i once other operations have been popped off the stack.
++i increments i in its already existing memory space.
These two methods are the reasons behind WHY it makes a difference in the order of operation, so you're kind of right.
The satements i++ and ++i, by themselves, are logically equivalent. However, if you combine a pre/post-increment operation with an other operation, then it would make a difference due to the order the stack is pushed/popped. Example:
int i = 0, j = 0, x, y;
x = i++;
y = ++j;
^^ After running that, your results are:
x == 0
y == 1
i == 1
j == 1
So as you can see, if you're using a counter in a for loop, ++i or i++ doesn't make a logical difference, but ++i is technically faster. Not something you really have to worry about, since most compilers will optimize it for you, but just an interesting tidbit. There is no practical reason to know this, I'm just a nerd, lol. If you really want to understand how it works, look up how to implement a for loop in assembly. You'll see that the "best" way is to do the analogous equivalent to ++i.
Thanks for the explanation. Been coding for years and I never really stopped to think about i++ or ++i other than logical uses of it. But what you said makes sense and is definitely useful to know!
I love people that freak out over hte difference. If this micro-optimization matters in the real world, tell me how so. I still have yet to find a reason to care. Use i++ at all times. The simple reason is that 90% of real world programmers will only get confused by ++i. There is no difference and ++i can only waste time due to people scratching their head.
<== 8+ years in Java and C++ in everything from VxWorks to Windows.
Originally posted by: Markbnj
Originally posted by: IHateMyJob2004
Originally posted by: deveraux
Originally posted by: slugg
Originally posted by: JACKDRUID
Originally posted by: slugg
^^ ++i is faster than i++, although most compilers will find this an optimize it for you. Careful - they're NOT the same! In this case, it's fine. Notice how the conditional is now i < 3.
umm.. i thought the only difference between ++i and i++ is the order of operation... ++i would be faster because its doing 1 less loop...
i++ copies i to a new memory space, increments it, puts it on the stack, then later assigns it back to i once other operations have been popped off the stack.
++i increments i in its already existing memory space.
These two methods are the reasons behind WHY it makes a difference in the order of operation, so you're kind of right.
The satements i++ and ++i, by themselves, are logically equivalent. However, if you combine a pre/post-increment operation with an other operation, then it would make a difference due to the order the stack is pushed/popped. Example:
int i = 0, j = 0, x, y;
x = i++;
y = ++j;
^^ After running that, your results are:
x == 0
y == 1
i == 1
j == 1
So as you can see, if you're using a counter in a for loop, ++i or i++ doesn't make a logical difference, but ++i is technically faster. Not something you really have to worry about, since most compilers will optimize it for you, but just an interesting tidbit. There is no practical reason to know this, I'm just a nerd, lol. If you really want to understand how it works, look up how to implement a for loop in assembly. You'll see that the "best" way is to do the analogous equivalent to ++i.
Thanks for the explanation. Been coding for years and I never really stopped to think about i++ or ++i other than logical uses of it. But what you said makes sense and is definitely useful to know!
I love people that freak out over hte difference. If this micro-optimization matters in the real world, tell me how so. I still have yet to find a reason to care. Use i++ at all times. The simple reason is that 90% of real world programmers will only get confused by ++i. There is no difference and ++i can only waste time due to people scratching their head.
<== 8+ years in Java and C++ in everything from VxWorks to Windows.
Anyone who can't get their head around the prefix increment operator should not be allowed near a C++ compiler. Seriously.
Not that I disagree with your main point: people spend too much time thinking about performance too early in the process. The old saw used to run: make it work, make it work right, then make it work fast. However making it a matter of habit to use the prefix operators is a simple thing.
As for sleeping with AAA chicks... it will be a lot easier for most of us to remember to use the prefix increment operator.