Gossamer Forum
Home : General : Perl Programming :

Increment Test

Quote Reply
Increment Test
Without cheating can anyone tell me what each "print" statement will print and the value of $i after each print....

Code:
my $i = 0;

print ++$i + 1;
print $i++ + 1;
print $i + $i++;
print ++$i + ++$i;
print ++$i + $i++ + 1;

I'm fine up to the second line but then my brain explodes Laugh
Quote Reply
Re: [Paul] Increment Test In reply to
Oops, that was wrong. Let's try again:

my $i = 0;

print ++$i + 1; # 1
print $i++ + 1; # 2
print $i + $i++; # 3
print ++$i + ++$i; # 4
print ++$i + $i++ + 1; # 5

Well, the first one is simple, 2.

The second one, also simple, 2 again.

The third one is where things get funky. $i++ changes the value of $i, and ++ is evaluated before +, so it could be written as:

my $old = $i++; print $i + $old;

Thus, "5" is printed.

Number 4 is fun too, because both the increments return not the new value of $i, but $i itself. Thus, it is the equivelant of:

++$i; ++$i; print $i + $i;

And thus, 10.

Number 5 will evaluate the first ++, incrementing and then returning $i (not that this is different from the value of $i), which means this is equivelant:

++$i; print $i + $i++ + 1;

Now, the post increment happens before the rest of it, but returns the old value, so we can write it like this:

++$i; my $old = $i++; print $i + $old + 1;

Resulting in 14.

Thus, the answer should be:

2
2
5
10
14

Two important things to note here: 1) the "post" and "pre" parts of "post-increment/pre-increment" means post- or pre-evaluation, not post- and pre-statement. 2) ++$i does not return the new value of $i, but returns $i itself.

This is somewhat odd behaviour - generally you would expect that ++$i returns the new value of $i.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com

Last edited by:

Jagerman: Jun 10, 2002, 7:13 PM
Quote Reply
Re: [Jagerman] Increment Test In reply to
I got 4 as well for th third, but:

root@image:/usr/local/ghost# perl -le 'my $i = 2; print $i + $i++; '
5
root@image:/usr/local/ghost#

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Jagerman] Increment Test In reply to
Nice explanation but again, my brain exploded :)

I thought ++$i incremented the value of $i before anything happened and $i++ was the opposite, like in parts one and two, but then it seems things start getting weird in 3/4/5

I sat there for ages trying to work it out and just gave up lol

Code:
What it prints - Value of $i
2 1
2 2
5 3
10 5
14 7
Quote Reply
Re: [Paul] Increment Test In reply to
It comes down to this:

$i++ returns the old value of $i. So, for example, if $i = 2, then $i++ will always return 2.

++$i is where things get tricky. ++$i does _NOT_ return the new value of $i, but it returns $i itself. Thus, if $i changes elsewhere in the expression, this value will change as well.

Again assuming $i = 2:

print ++$i; # prints 3, and sets $i to 3

print ++$i + ++$i; # prints 10 - $i is incremented twice, and the new value of $i is added together twice.

Note that you can do this:

print +(0 + ++$i) + (0 + ++$i);

That goes back to the more expected way - if $i = 2, that will print 7.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com