postfix and prefix...???
just a quick question i hope....
What is the difference between theses two "for" loops....
for(int i = 0; i < 10; ++i)
for(int i = 0; i < 10; i++)
of course i am speaking of the last parameter....the postfix and prefix operators in this particular case only...
>What is the difference between theses two "for" loops....
Programmer style. The increment is guaranteed to happen before the next iteration of the loop begins and after the previous iteration finishes, so prefix and postfix are exactly the same in this case.
Well, there aren't differences in those loops per se.
++i and i++ differ in the return value. ++i returns the new value, i++ returns the old.
If your increment/decrement is the only statement on a line, then it does not matter, but if you try and assign it to something, then it does.
In general, use ++i unless you have some reason not to. Most of the time it does not matter.
thanx.....i was just wondering b/c i've seen different syntaxes among different applications...
i know what it does in terms of assigment .. anyway, thank you....
Prelude ----> why do u always quote other people's questions and statements... ??
>why do u always quote other people's questions and statements... ??
It's a habit I've gotten into because most threads ask more than one question. It's easier to understand what I'm talking about if I reiterate the question and then answer it. This becomes even more useful in very long threads with several conversations going on at once.
i read somewhere that since ++i always increments and i++ onlt increments if needed that the former s more eficient.
I think the former is very slightly more efficient b/c it does not involve the use of a temporary.
++i resolves to:
i++ resolves to:
int temp = i;
Or, if i is equal to 2, then
p = ++i;
p and i equal three
p = i++;
p equals two, i equals three.
i stand corrected.
thanks machine gun dude.