This line means?

Jan 12, 2014 at 5:21am
I wanted to know what this line meant?

for ( int loop = 1; loop <= numberOfMonths;++loop)

mainly what does the int loop = 1 do? Also for the loop++? what does that do?
Last edited on Jan 12, 2014 at 5:22am
Jan 12, 2014 at 5:27am
Do you know about for loops?

You can read this:
http://www.cplusplus.com/doc/tutorial/control/

(scroll down about halfway to where it says "The for loop")
Jan 12, 2014 at 6:56am
closed account (Dy7SLyTq)
also the ++ operator is the same as loop = loop + 1. well depending on if you use it ++loop or loop++ there will be some differences but you can google that
Jan 12, 2014 at 9:31am
also the ++ operator is the same as loop = loop + 1. well depending on if you use it ++loop or loop++ there will be some differences but you can google that

AFAIK, in this situation it makes absolutely no difference, not even to performance. However if iterators were being used then ++iterator would be more efficient than iterator++
Jan 12, 2014 at 1:30pm
AFAIK, in this situation it makes absolutely no difference, not even to performance.

Right, that makes no difference in the for-loop, the only difference I've learnt is that ++loop increase the variable before using it, while loop++ increase the variable after using it.

However if iterators were being used then ++iterator would be more efficient than iterator++

Could be elaborate more on this, is ++iterator significantlymore efficient than iterator++?
Jan 12, 2014 at 7:06pm
closed account (Dy7SLyTq)
in this situation yes it doesnt make a difference.
@yueeng: i dont believe so. because it turns into the command inc [Loop] either way, the compiler just determines where it is put
Jan 13, 2014 at 3:22am
@DTSCode
Do you mean the ++iterator or the ++loop? It really matters for integers, I've tested it.
Jan 13, 2014 at 3:34am
Unless your compiler is too stupid to use even the most basic optimizations, it won't make a difference for primitive types. But the main issue is the habit you form - it does make a difference with iterators with some compilers and some optimization levels, so you should get into the habit of using pre-increment and pre-decrement when possible. Otherwise you will be inconsistent.
Last edited on Jan 13, 2014 at 3:34am
Jan 13, 2014 at 5:44am
Yes. Just remember that something++ actually creates a copy of the object, increments the original and then returns the copy. That's two copies per call.
Jan 13, 2014 at 5:55am
In C++11 it is only one copy thanks to move constructors, and in C++03 it might only be one copy with return value optimization.
Topic archived. No new replies allowed.