Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion When does (pre|post)increment happen?

Path: archiver1.google.com!postnews1.google.com!not-for-mail
From: pjack...@online.no (Peter J. Acklam)
Newsgroups: comp.lang.perl.misc
Subject: When does (pre|post)increment happen?
Date: 13 Jan 2003 00:51:28 -0800
Organization: http://groups.google.com/
Lines: 20
Message-ID: <fe4612a5.0301130051.7f5ee55d@posting.google.com>
NNTP-Posting-Host: 193.215.255.181
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1042447888 22953 127.0.0.1 (13 Jan 2003 08:51:28 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: 13 Jan 2003 08:51:28 GMT

The following results confuse me.  I expected
to get 0.5 and 1:

   $ perl -wle '$n = 1 ; print $n / ++$n'
   1

   $ perl -wle '$n = 1 ; print $n / $n++'
   2

In the first case, I expected to get 0.5 because
the preincrement would increment $n so the numerator
became 2, thus giving 1/2 = 0.5.

In the second case, I expected to get 1 because
the postincrement would increment $n *after* the
division was performed, thus giving 1/1 = 1.

I'd be thankful if anyone would explain...

Peter