For loop and symantic manipulations

Status
Not open for further replies.

Thread Starter

Ryan$

Joined Dec 14, 2018
178
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 the pc modify the "i" by 5 and then after returning to for itself will start from the value that I added (by 5 ) + 1? for example here first i=0, so it will add it by 5 after entering the for loop, so i=5, then it will return to the for loop, my question will he returns by value i=5 or i=6? because he already does i++ in iteration i=0 so..?! thanks in advance!.


what's the difference between: "++i", "i++";
Code:
 for(int i=0;i<10;++i)
Code:
for (int i=0;i<10;i++)
thanks in advance !!
 
Last edited:

WBahn

Joined Mar 31, 2012
29,978
i++ is an expression that evaluates to a value and has a side effect.
++i is an expression that evaluates to a value and has a side effect.

The side effect in both cases is the same -- the value stored in the variable i is incremented by 1.

The value that i++ evaluates to is (i) while the value that ++i evaluates to is (i+1). The value that the expression evaluates to is independent of the side effect.

As for the for() loop semantics, that has nothing to do with i++.

In general, you can translate a for() loop into an equivalent while() loop (provided there are no 'continue' statements in the loop) as follows:

Code:
for (statement1; statement2; statement3)
{
   // loop code
}

to

statement1;
while (statement2)
{
   // loop code
   statement3;
}
The for() loop is what is known as "syntactic sugar" -- it isn't necessary, but it makes the code easier for humans to write, read, and maintain.

EDIT: Typed evaluation of i++ and ++i backwards. Fixed it. Thanks to @AlbertHall for pointing out the error.
 
Last edited:

MrChips

Joined Oct 2, 2009
30,708
The purpose of AAC forums is not here for its members to explain to you how every programming concept works.

We can do so much to explain one thing at a time. But there has to be a limit to this when you consider that all of this is explained in a proper text book, programming lesson, or web site.

You really need to find another source in order to learn about computers and programming languages.
 

MrChips

Joined Oct 2, 2009
30,708
FYI,

++i is called pre-increment
i++ is called post-increment

pre-increment means that the variable is incremented before being used in the operation.
post-increment means that the variable is used as is in the operation first and then it is incremented after the operation.

As for the question regarding i++ within the for( ) statement,
++i
or
i++

stand alone as a complete operation. Hence there is no difference in this example.
 
Status
Not open for further replies.
Top