[Date Index]
[Thread Index]
[Author Index]
Re: Assignment problem
*To*: mathgroup at smc.vnet.net
*Subject*: [mg86345] Re: Assignment problem
*From*: Albert Retey <awnl at arcor.net>
*Date*: Sun, 9 Mar 2008 05:00:33 -0500 (EST)
*References*: <fqo8o1$suo$1@smc.vnet.net> <fqqs3b$kf5$1@smc.vnet.net> <fqtqin$d3j$1@smc.vnet.net>
Hi,
>> Note that usually using a Do-Loop is the better choice in Mathematica:
>
> (as compared with a For loop).
>
> This is the second post I've seen today that says that Do loops are
> better than For loops in Mathematica. What is better about them (aside
> from slightly simpler syntax)?
1) Isn't that enough? I think it is a big plus if you can do the same
thing with simpler syntax. It also fits much better in the syntax of
Table and other Mathematica-constructs with the need of iterators, so
code becomes more uniform which makes it also easier to understand. I
also have never struggled over index mismatches with Do loops, but that
was the most common error I made when writing C-code -- with for loops
(admittedly I was switching between languages at that time, which also
was an importand reason for this).
Of course there are things that you can do only with a For loop, but the
price is that you always have to check what really happens in a For loop
when you try to understand what it does, and at least for me that is
usually more difficult. When I see a Do-loop, I see (or guess, probably
not always correctly) that something simple is going on...
2) Do-loops do a scoping of the indices, which might not always be what
you want, but I think in most cases is appropriate.
3) I have not really investigated this, since I never had the need for a
For-loop in mathematica, but I guess that Do loops are (or at least
should be) also more efficient, since no mathematica code needs to be
evaluated when determining the indices and they can be determined in
advance. But this might only be relevant in special situations...
just my 2 cents
albert
PS: when playing around with For and Do loops I just discovered that the
following loops behave differently, and the somewhat surprising result
of the Do-loop indicates that the indices are really determined in advance:
Do[Print[++i], {i, 0, 3, 3}]
For[i = 0, i <= 5, i += 3, Print[++i]]
on the other hand, that means that the two constructs are not as
interchangable as one might think...
Prev by Date:
**Re: Apply function to parts of a list**
Next by Date:
**Re: Re: changing style of vertices for ShowGraph with Combinatorica**
Previous by thread:
**Re: Assignment problem**
Next by thread:
**Re: Assignment problem**
| |