Groups Home | Help | Sign in
Go to Google Groups Home   
Web    Images    Groups    News    Froogle    Local    more ยป
  Advanced Groups Search
  Preferences
comp.lang.perl.misc > When does (pre|post)increment happen?
Fixed font - Proportional font
Peter J. Acklam
Jan 13 2003, 8:51 am   show options
Newsgroups: comp.lang.perl.misc
From: pjack...@online.no (Peter J. Acklam) - Find messages by this author
Date: 13 Jan 2003 00:51:28 -0800
Local: Mon, Jan 13 2003 8:51 am
Subject: When does (pre|post)increment happen?
Reply to Author | Forward | Print | View Thread | Show original | Report Abuse

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

Google Home - Terms of Use - Privacy Policy - Jobs, Press, & Help

©2005 Google