MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: For loops with mathematica....

  • To: mathgroup at
  • Subject: [mg101126] Re: [mg101069] For loops with mathematica....
  • From: Murray Eisenberg <murray at>
  • Date: Wed, 24 Jun 2009 06:36:13 -0400 (EDT)
  • Organization: Mathematics & Statistics, Univ. of Mass./Amherst
  • References: <>
  • Reply-to: murray at

The form (i, end} for an iterator is essentially an abbreviation for {i, 
1, end}. And that is a special case of {i, start, end}.  That, in turn, 
is a special case of {i, start, end, increment}.

But you're asking about a more complicated set of values for i than any 
single iterator of that form will provide. Fortunately, in the current 
version of Mathematica, there is a more general iterator construction 
yet, {i, valueList}, which iterates over the values given in the list 
valueList.  For example,


will evaluate expr at each of i = 3, 8, 12, 291.

So your problem now reduces to constructing the list of values you want, 
and that's straightforward:

   Join[Range[1,20], Range[300,400]]

You are correct that if you want to "do" several things -- that is, 
evaluate several individual expressions during each iteration, then you 
separate them by semicolons, in other words, form from them a compound 
expression.  For example:

   Do[Print[i^2];Print[i^3], {i,{3,8,12,291}}]

You say that separating expressions with a semicolon in the first 
argument of Do "doesn't seem to work", but you didn't explain in what 
way it doesn't work.

Finally, you might do some more reading of Mathematica documentation to 
learn more of the basics.  And also to see whether you really need a Do 
loop at all, or any other kind of explicitly constructed iterative 
construct.  You may find that Nest can nicely automate the entire 
process for you.  Or that you can use whole-array processing, functional 
programming, or pattern-matching instead of an explicitly coded loop. 
And you might find that such an alternative procedure is much simpler to 
code and read and even more efficient than an explicitly coded loop.

Julien wrote:
> Hey guys, 
> I am totally new to mathematica.
> I want to use a simple loop for but with many arguments in the loop...
> the help says to do
> Do[ thingstodo, {i,8}]
> i will go from 1 to 8 to do thingstodo.
> First question: what if I want i to go from 20 then 200 then 300 then 400, (can we give a sequence as argument?)
> Second question: I have a lot of line to do in things to do. should I separate everything with a ; (doesn t seem to work, or use a procedure or something?)
> THanks a lot for your help!!!
> Julien

Murray Eisenberg                     murray at
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305

  • Prev by Date: Re: Re: Putting an If in my function
  • Next by Date: Re: Green's Function for Fresnel Reflection?
  • Previous by thread: Re: For loops with mathematica....
  • Next by thread: Re: For loops with mathematica....