Originally posted by: Regs
I never quite understood the purpose of a for loop/ for while loop ect..
Just one of those things I want to figure out before even reading a java book.
In english, a for loop says
given x variable, while a comparison of x is true, do whats in the brackets and then do this line
For(Int variable = 1; variable != 2; variable++)
{
cout << "See, I'm not so hard to understand" << endl;
}
The program hits the for, and does the first part of the paren (Int variable = 1

in this case, it defines an integer variable and sets it equal to 1.
Then it checks if the second is true. variable != 2. variable == 1 and 1 < 2 so this is true. So the program executes what's in the squigglies (cout << "See, I'm not so hard to understand." << endl

and then the last item in the parens (variable++).
The program then checks the second part again. It's false this time, so the loop terminates.
It really makes more sense if you play with it in a debugger. You can watch exactly how the language steps through each part, and why.