On Jun 1, [EMAIL PROTECTED] said:

>$i = 1;
>$power = 1;
>
>print "Input a number:\n";
>chomp ( $x = <STDIN> );
>
>print "Input the value you want the number raised to\n";
>chomp ( $y = <STDIN>) ;
>       
>while ( $i<= $y ) {
>       $power *= $x;
>       ++ $i;
>}

This loop is basically a for-loop in extended form.

  $power = $x ** $y;

is the simplest way of raising $x to the $y power.  But you can do it the
long way, like:

  $power = 1;
  for ($i = 0; $i < $y; $i++) { $power *= $x }

or

  $power = 1;
  for ($i = 1; $i <= $y; $i++) { $power *= $x }

or, more Perl-like

  $power = 1;
  for (1 .. $y) { $power *= $x }

You could even use a while loop depending solely on $y!

  $power = 1;
  while ($y--) { $power *= $x }

The point is, the while loop in the program is starting $i at 1, and
multiplying $power by $x once each iteration, until $i is greater than $y
-- at that point, we've multiplied $y times, so $power holds $x**$y.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
**      Manning Publications, Co, is publishing my Perl Regex book      **

Reply via email to