This line means?

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
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")
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
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++
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++?
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
@DTSCode
Do you mean the ++iterator or the ++loop? It really matters for integers, I've tested it.
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
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.
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.