-
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)
blah blah;
and
for(int i = 0; i < 10; i++)
blah blah;
of course i am speaking of the last parameter....the postfix and prefix operators in this particular case only...
thanx
-
>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.
-Prelude
-
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....
matheo917
-
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.
-Prelude
-
howdy,
i read somewhere that since ++i always increments and i++ onlt increments if needed that the former s more eficient.
M.R.
-
I think the former is very slightly more efficient b/c it does not involve the use of a temporary.
++i resolves to:
{
i+=1;
return i;
}
i++ resolves to:
{
int temp = i;
i+=1;
return temp;
}
Or, if i is equal to 2, then
p = ++i;
p and i equal three
p = i++;
p equals two, i equals three.
-
howdy,
i stand corrected.
thanks machine gun dude.
M.R.