Post-Increment and Pre-Increment Within a 'For' Loop Produce Same Output

Post-increment and pre-increment within a 'for' loop produce same output

After evaluating i++ or ++i, the new value of i will be the same in both cases. The difference between pre- and post-increment is in the result of evaluating the expression itself.

++i increments i and evaluates to the new value of i.

i++ evaluates to the old value of i, and increments i.

The reason this doesn't matter in a for loop is that the flow of control works roughly like this:

  1. test the condition
  2. if it is false, terminate
  3. if it is true, execute the body
  4. execute the incrementation step

Because (1) and (4) are decoupled, either pre- or post-increment can be used.

Difference between pre-increment and post-increment in a loop?

a++ is known as postfix.

add 1 to a, returns the old value.

++a is known as prefix.

add 1 to a, returns the new value.

C#:

string[] items = {"a","b","c","d"};
int i = 0;
foreach (string item in items)
{
Console.WriteLine(++i);
}
Console.WriteLine("");

i = 0;
foreach (string item in items)
{
Console.WriteLine(i++);
}

Output:

1
2
3
4

0
1
2
3

foreach and while loops depend on which increment type you use. With for loops like below it makes no difference as you're not using the return value of i:

for (int i = 0; i < 5; i++) { Console.Write(i);}
Console.WriteLine("");
for (int i = 0; i < 5; ++i) { Console.Write(i); }

0 1 2 3 4

0 1 2 3 4

If the value as evaluated is used then the type of increment becomes significant:

int n = 0;
for (int i = 0; n < 5; n = i++) { }

In a for loop, is there a difference between pre/post-incrementing a loop control variable in terms of the total quantity of iterations?

There is no difference. In older compilers, ++counter was faster because it did not create a temporary variable but all modern compilers can optimize that out. For heavy objects with custom (non-inlined) increment operators, ++counter can still be more efficient.

As for when evaluation takes place:

for (initialization; condition; increment/decrement)
code;

is evaluated as

{
initialization;
while (condition)
{
code;
increment/decrement;
}
}

Why have for-loops with pre-increment the same behaviour as those with post-increment?

The last part of the for loop only happens at the end of each loop.

So ++i and i++ do basically the same thing in that case.


Order of operations:

var i = 0;

while (i < 10) {
// do stuff
console.log(i);
// increment i in your own way
++i; // or i++;
}

Why doesn't changing the pre to the post increment at the iteration part of a for loop make a difference?

The loop is equivalent to:

int x = 2;
{
int y = 2;
while (y > 0)
{
System.out.println(x + " "+ y + " ");
x++;
y--; // or --y;
}
}

As you can see from reading that code, it doesn't matter whether you use the post or pre decrement operator in the third section of the for loop.

More generally, any for loop of the form:

for (ForInit ; Expression ; ForUpdate)
forLoopBody();

is exactly equivalent to the while loop:

{
ForInit;
while (Expression) {
forLoopBody();
ForUpdate;
}
}

The for loop is more compact, and thus easier to parse for such a common idiom.



Related Topics



Leave a reply



Submit