[Tutor] for loops ... (fwd)
Karl Pflästerer
sigurd at 12move.de
Tue Nov 18 14:01:06 EST 2003
On 18 Nov 2003, Danny Yoo <- dyoo at hkn.eecs.berkeley.edu wrote:
> ---------- Forwarded message ----------
> Date: Tue, 18 Nov 2003 06:49:25 +0100
> From: Tadey <tayiper at volja.net>
> To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
> Subject: for loops ...
(I send a Cc to you as I'm not sure if you read the list).
[...]
>>>> j = 1
>>>> while j <= 12:
> ... print "%d x 12 = %d" % (j, j*12)
> ... j = j + 1
> ... the part, where it says j = j + 1 ... looked at first a bit illogical,
If you think like a mathematician you're perfectly right. Computer
scientists sadly thought such a syntax would be no problem (not all; cf
eg. Pascal).
> like 1 = 1 + 1, so 1 = 2 come after,so this equation seems illogical (the
> value on both sides of = sign should be identical - that is the deffinition
> of equation).
Right. But that is no equation but an assignment. In some languages
you'd have to write assignment like that: k := k + 1 to make it clear
that there is no equation meant.
You have to read k = k + 1 as
new value = old value + 1
The left hand side (lhs) gets assigned the value which is computed at
the right hand side (rhs).
> So I tryed to imagine it not as equation, but rather as some "instructon"
> how number (set by variable should "change/increase" through "looping"
> cicles.
Right.
> So I tryed to type j = 1 then j = j + 1, and of course after print j it says
> j = 2, so in case above (if j = 1) means variable j changes from 1 to 2, and
> then from 2 to 3, every loop +1, and so on to 11 ... is that it ??
Yes.
> So please, give me some background releted to that kind of
> equations/instructions (j = j + 1), how they "control" loops, which has
> higher "priority" - for better imaging !!
See above. First the rhs is computed; its value is ssigned to the lhs.
[...]
> I don't get it, how it print that high values as a result.
As you seem to know mathematics the formula computes factorial numbers
(alomost). They are only defined for positive natural numbers including
zero.
> I thought, that it first "takes" dog value 2, defined in tadey(2), then
> "calculates" current value for this loop, so dog = dog - 1, that gives 1,
> than multiplicate with result, which is set 1, which gives the real result
> ... but as you see (if you calculate in my way), I didn't get the right
> values.
>>>> def tadey(glg):
> result = 1
> while glg > 0:
> result = result * glg
> glg = glg - 1
> return result
Let's do it manually. If you call that function with 3 what happens?
(I write assignment as `a <- b' which means: assign the value of b to a)
tadey(3)
Step Operation Result
1 result =1 result <- 1
********** Entering the loop **********
2 glg > 0 True (glg is 3)
3 result = ... result <- 1 * 3
4 glg = ... glg <- 3 - 1
********** check loop condition **********
5 glg > 0 True (glg is 2)
6 result = ... result <- 3 * 2 (see step 3 for the new value of
result and step 4 for the new
value of glg)
7 glg = ... glg <- 2 - 1
********** check loop condition **********
8 glg > 0 True
9 result = ... result <- 6 * 1 (see step 6 and 7)
10 glg = ... glg <- 1 - 1
********** check loop condition **********
11 glg > 0 False (see step 10)
********** leaving loop **********
12 return result 6 (see step 9)
Karl
--
Please do *not* send copies of replies to me.
I read the list
More information about the Tutor
mailing list