32bitfloat
32bitfloat

Reputation: 771

why does the for-loop not care about the way of increment?

This is propably a farcical "problem" but I just don't see the reason for this behaviour.


Facts:

$i++;

returns the current value and then increments $i by one.

++$i;

increments $i by one and then returns $i.


Situations:

for($i = 0; $i < 10; ++$i){
    echo $i."\n";
}

gives

0
1
2
3
4
5
6
7
8
9

2nd:

for($i = 0; $i < 10; $i++){
    echo $i."\n";   
}

gives also

0
1
2
3
4
5
6
7
8
9

If I'd take the documentation of the increment literally, I would explain the loops as follows:

  1. at the end of each iteration, $i is incremented by one and then returned, so we've got at first a 0 because $i started as 0, then a 1 etc.
  2. at the end of each iteration, $i is returned and THEN incremented, which would, exactly, mean that there were two iterations where $i = 0.


It's a fact that this is not true. Could somebody please explain, why?

Upvotes: 0

Views: 184

Answers (5)

Joseph Silber
Joseph Silber

Reputation: 219920

$i is not being returned, it is being used (by you). Big difference.

If you were to rewrite your if statements to instead use a while loop, you'd have these:

Post-increment:

$i = 0;

while ( $i < 10 ) {
    echo $i."\n";
    $i++;
}

Pre-increment:

$i = 0;

while ( $i < 10 ) {
    echo $i."\n";
    ++$i;
}

As you can see, there's no difference between the two.


Here's what the 3 statements you supply to the for loop are for:

  1. Run before the first step
  2. Run before every step (use the returned value to determine whether to continue with the loop)
  3. Run after every step

at no point is the returned value of the 3rd statement being utilized in any way.


P.S. As mentioned, the third statement is run after every loop including the last one. This is evident by accessing the $i variable after the loop has completed:

for ($i = 0; $i < 10; $i++) {
    echo $i."\n";
}
echo $i;

which would list all the numbers up to and including 10.

See it here in action: http://viper-7.com/Y6N2jU

Upvotes: 6

Palladium
Palladium

Reputation: 3763

As I understand it, the for loop checks the condition in the second parameter before executing the third parameter. This means that, in both cases:

  • $i is initialized with value 0 (this step is obviously skipped for every subsequent iteration);
  • $i is compared with 10 (it's smaller); and then
  • $i is incremented.

In that last step, whether the value of $i is returned before or after the increment shouldn't matter, because the "returned" value of $i isn't actually used in any comparison. The actual value of $i as stored in memory has still been incremented by one each iteration, after the comparison has been made.

Upvotes: 2

Kalpesh
Kalpesh

Reputation: 5685

Post-incrementing or Pre-incrementing a variable as the third expression in the loop doesn't have an effect on the behavior of it

Because, $i++ and ++$i would both execute at the end of each iteration of the loop.

Upvotes: 0

Razvan
Razvan

Reputation: 10093

A for loop has 3 components:

  1. An initialization component (the code before the first ';')
  2. A termination condition (the code between the first ';' and the second ';')
  3. An increment (after the second ';')

The order of execution of these 3 pieces of code is:

  • (1) executed only once at the beginning of the for loop

  • (2) verified before each iteration

  • (3) executed after each iteration

Consequently it's not important if you use the prefix or postfix form of the increment operation; after each iteration the increment (3) will be executed and you will get a new value of the incremented variable

This:

for($i = 0; $i < 10; $a = $i++){
    echo $i."\n";
}

as compared to this

for($i = 0; $i < 10; $a = ++$i){
    echo $i."\n";
}

would make a difference in the value of $a after each iteration, but not in the value of $i

Upvotes: 1

Calvin Jia
Calvin Jia

Reputation: 856

If you write the for loop out as a sequence of if statements, I think it would make more sense as to why $i increments each time.

i = 0
if i > 10
    return
else
    print i
    i++ // or ++i
if i > 10
    return
else
    print i
    i++ // or ++i

and so on.

Upvotes: 0

Related Questions