A for loop is composed of three sections; each separated by a ';'.
First of all, they are all optional. You can omit one or all sections.
Code:
for(;;); // This is valid. A loop that runs forever.
In the first section you can define or initialize a variable. Usually the variable
that controls the loop, but it can be any variable.
Code:
//declaration and definition
for (unsigned int i = 7;;)
{
//loop body...
}
//initialization
unsigned int i;
for (i = 42;;)
{
//loop body...
}
The second section evaluates an expression at the beginning of each iteration.
If true, the loop body will execute, if false, control jumps to the next instruction
after the loop.
Code:
for (;2 + 2 == 4;)
printf("Seems like two plus two is four!");
The last section executes an instruction. Simple as that.
Code:
for(;;printf("Printing hello world forever!\n"));
Although commonly used to control the expression that is evaluated to determine
wether the loop executes or not, it doesn't have to be that, as you can clearly
see on the example above.
Now, I'm showing you this to help you understand the mechanics of the "for" construct,
and that there is nothing mystic about it. This doesn't mean however that you should
use the "for loop" however you want. You could, but you shouldn't.
It will be more clear for anyone reading your code if you stick to the common conventions
of defining the exit condition, evaluating the exit condition, and modifying the exit condition:
Code:
for (unsigned int a = 10; a > 0; --a)
{
printf("Countdown to new year: %u!\n", a);
}
printf("Happy new year!\n");
[/code]
++ and -- are the increment and decrement operators. As their name suggest they
either increment or decrement an object or expression.
Code:
a = a + 1;
//is the same as
a++;
//or
a += 1;
These operators can be used either before or after the expression they modify, i.e
in prefix or postfix form.
When used before, their modification is given priority and occurs before most other operations.
In case of simple expressions where there are no more operations, like in the example above,
the expressions are equivalent.
Code:
++a;
//is the same as
a++;
In cases like:
Code:
unsigned int a = 7;
unsigned int b = 0;
b = a++;
There is a difference. Here b will equal 7 because the ++ operator is used in its postfix form;
the assignment operation is performed first, b = 7, then the increment operation takes place, a++ or a += 1,
which results in a being equal to 8. We end up with a = 8, b = 7.
If we use the prefix operator instead we get:
Code:
unsigned int a = 7;
unsigned int b = 0;
b = ++a;
In this case the increment operation takes precedence and 'a' is incremented before the assignment;
a += 1 equals 8, then b = 8, resulting in both a and b being equal to 8.
Increment and decrement operators - Wikipedia
You do need to be careful when using these in certain expressions like:
Code:
a[++i]=a[i];
a[i++]=a[i];
This is undefined, and you should get a warning, due to your modification of the element used to access the array.
This has to do with how the expression is evaluated at a lower level, I don't quite understand the details myself.
I do know some compilers will correctly deduce your intent and work as you'd except while others will crap on you.