understanding adding numeric accumulator

understanding adding numeric accumulator

am 09.07.2011 01:33:07 von phillyj101

Hi all,
I'm teaching myself perl. Right now, I am stuck with this script. I
don't understand how it works. I know what it does and how to do it by
hand.

$n = 1;
while ($n < 10) {
$sum += $n;
$n += 2;
}
print "The sum is $sum.\n"

I know $sum is initially 0 (undef). I see that $sum becomes 1, then $n
becomes 3. The loop goes back and then I don't understand. Now $sum is
1+3?

Thanks,
JJ

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: understanding adding numeric accumulator

am 09.07.2011 01:44:51 von Jim Gibson

On 7/8/11 Fri Jul 8, 2011 4:33 PM, "J. S. John"
scribbled:

> Hi all,
> I'm teaching myself perl. Right now, I am stuck with this script. I
> don't understand how it works. I know what it does and how to do it by
> hand.
>
> $n = 1;
> while ($n < 10) {
> $sum += $n;
> $n += 2;
> }
> print "The sum is $sum.\n"
>
> I know $sum is initially 0 (undef). I see that $sum becomes 1, then $n
> becomes 3. The loop goes back and then I don't understand. Now $sum is
> 1+3?

Yes.

The while loop will repeat as long as its conditional expression is true. In
this case, it will loop as long as $n is less than 10. Since $n is 1 at the
beginning of the loop and incremented by 2 during each iteration of the
loop, $n will have the successive values 1, 3, 5, 7, and 9 during 5 loop
iterations. At the beginning of the 6th iteration, $n has the value 11.
Since the logical expression '$n < 10' is now false, the loop body is
skipped, and execution resumes at the first line after the loop body (the
print statement).

At the end of the loop, $sum should have the value 1 + 3 + 5 + 7 + 9, or 25.



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: understanding adding numeric accumulator

am 09.07.2011 01:48:39 von Rob Coops

--000e0cd71958362aeb04a7977a40
Content-Type: text/plain; charset=UTF-8

On Sat, Jul 9, 2011 at 1:33 AM, J. S. John wrote:

> Hi all,
> I'm teaching myself perl. Right now, I am stuck with this script. I
> don't understand how it works. I know what it does and how to do it by
> hand.
>
> $n = 1;
> while ($n < 10) {
> $sum += $n;
> $n += 2;
> }
> print "The sum is $sum.\n"
>
> I know $sum is initially 0 (undef). I see that $sum becomes 1, then $n
> becomes 3. The loop goes back and then I don't understand. Now $sum is
> 1+3?
>
> Thanks,
> JJ
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
First of all it is a really bad thing to not use warnings and strict in your
scripts just start your script with:

use warnings;
use strict;

and this script will end up screaming that you have to declare your
variables.

So your script would end up looking like this:

use warnings;
use strict;

my $n = 1;
my $sum; # Should actually be my $sum = 0; even if it is only for
readability but we shall let that slide for now ;-)
while ($n < 10) {
$sum += $n;
$n += 2;
}
print "The sum is $sum.\n"

So what does the script do?

Well literally it says while $n is smaller then 10 execute what is in side
the { } brackets. Inside you say $sum = $sum + $n; and then $n = $n + 2;
So on the second loop you look at the value of $n which now is 1 + 2 = 3. So
then we take $sum which is 1 and add $n to that so 1 + 3 = 4 and we increase
$n by 2 ending up at 5.
The next loop (5 is smaller then 10 after all) $sum becomes 4 + 5 = 9 and $n
becomes 5 + 2 = 7
The next loop (7 is smaller then 10 after all) $sum becomes 9 + 7 = 16 and
$n becomes 7 + 2 = 9
The next loop (9 is smaller then 10 after all) $sum becomes 16 + 9 = 25 and
$n becomes 9 + 2 = 11
Then 11 is not smaller then 10 anymore an the loop ends the next thing you
do is print the value of $sum which should by now be 25.

To see if I am right why not modify the script a little so it will print the
inner values:

use warnings;
use strict;

my $n = 1;
my $sum = 0;
while ($n < 10) {
print '$sum = ' . $sum . ' $n = ' . $n;
print '$sum += $n';
$sum += $n;
print '$sum = ' . $sum;
$n += 2;
print '$n + 2 = ' . $n;
}
print "The sum is $sum.\n"

Now it should show you exactly what it is doing inside the loop :-)

Regards,

Rob

--000e0cd71958362aeb04a7977a40--

Re: understanding adding numeric accumulator

am 09.07.2011 02:05:14 von Chris Charley

""J. S. John"" wrote in message
news:CAAhF0RKhYip680xK9Kz+3q4Uvw79S2dH8deNuLOb_2gXLyfshw@mai l.gmail.com...

Hi all,
I'm teaching myself perl. Right now, I am stuck with this script. I
don't understand how it works. I know what it does and how to do it by
hand.

$n = 1;
while ($n < 10) {
$sum += $n;
$n += 2;
}
print "The sum is $sum.\n"

I know $sum is initially 0 (undef). I see that $sum becomes 1, then $n
becomes 3. The loop goes back and then I don't understand. Now $sum is
1+3?

Thanks,
JJ

Hello JJ,

It is sometimes helpful to put print statements in a routine to follow the
flow of action. However, if you don't have Perl installed that would be a
problem :-)

$sum += $n;

This statement is shorthand for $sum = $sum + $n;
Whatever value (0 the first time through the loop) is in $sum is added to
the value of $n, (which starts at 1 and increases by 2 for every time though
the loop: $n = $n + 2), and stored in $sum.

In effect, this loop is calculating the sum of odd integers from 1 to 9.

Here it is with some print (say) statements:

#!/usr/bin/perl
use strict;
use warnings;
use 5.014;

my $n = 1;
my $sum;
while ($n < 10) {
say "n: ", $n;
say "sum: ", $sum += $n;
$n += 2;
}

And it prints:
n: 1
sum: 1
n: 3
sum: 4
n: 5
sum: 9
n: 7
sum: 16
n: 9
sum: 25

The loop ends because $n gets incremented to 11 and fails the loop test ($n
< 10).

Hope this helps explain it for you.

Chris


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: understanding adding numeric accumulator

am 10.07.2011 23:56:11 von Chris Nehren

Chris, your quoting mechanism is extremely unclear. It's difficult to
discern what you wrote and what the people you're quoting wrote.

On Fri, Jul 08, 2011 at 20:05:14 -0400 , Chris Charley wrote:
> Hello JJ,
>
> It is sometimes helpful to put print statements in a routine to
> follow the flow of action. However, if you don't have Perl installed
> that would be a problem :-)

If you've not got perl installed, installing it is pretty trivial, even
on Windows. There are portable Strawberry releases available at
http://strawberryperl.com/releases.html (portable meaning one doesn't
need to be an admin to install perl), and those on anything sane with a
compiler toolchain can use perlbrew (http://cpan.me/App::perlbrew) to
get a recent perl installed in their home directory. If you've not got a
compiler toolchain installed, you're extremely limited in the CPAN
modules you can choose, and may as well not use Perl at that point.

> Here it is with some print (say) statements:

To be clear, say is a builtin for perls version 5.10.1 (5.10.0 had
several bugs and is not recommended for production or development use)
and above. It's roughly equivalent to print with a \n added to the end.
If you don't have 5.10.1 or above installed, read the docs for say here:
http://p3rl.org/say .

--
Chris Nehren | Coder, Sysadmin, Masochist
Shadowcat Systems Ltd. | http://shadowcat.co.uk/

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/