# Thread: For loop and semantic manipulations

1. ## For loop and semantic manipulations

Hi !
can someone please explain to me exactly how the for loop work?! I mean when exactly the "i++" modified? when entering the loop or once returning from the loop it gets modified? also is it first gets modified and then check the condition of for loop or first checking the condition and then adding the "i"?

if after loop itself I added the operand "i" will be it added again once returning to the loop itself? I mean like this:
Code:
``` for (int i=0;i<10;i++)
{
i+=5;
}```
then will I be added again by 1 once returning to for itself?

what's the difference between: "++i", "i++";

also what's the difference between those:
for(int i=0; i<10;i++);
for(int i=0;i<10,++i);
a[i]=a[i++];
a[++i]=a[i];
a[i++]=a[i]; 2. Have you read any book on C ? If you read several top C books, many of such questions are answered. Generally I would say try to do one job in every line as far as you can. Also take a look at this link about C operator precedence. 3. Originally Posted by RyanC also what's the difference between those:
for(int i=0; i<10;i++);
for(int i=0;i<10,++i);
a[i]=a[i++];
a[++i]=a[i];
a[i++]=a[i];
There's no difference between the first two.

The last three have undefined behavior--don't do them! 4. 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. 5. The goof-ball has also infested another forum now.
For Loop - C And C++ | Dream.In.Code 6. Originally Posted by Dren ... book elided ...
Stop feeding the troll.
Just let it die. 7. No more food. Popular pages Recent additions condition, difference, forint, loop, modified 