Message Boards Message Boards

4 Replies
8 Total Likes
View groups...
Share this post:

How to use do, for, while loops?

Posted 10 years ago

I'm new to mathematica and I'm trying to teach myself a few things before taking a class in it. I am currently trying to write a program that calculates 10! I know that I need a do and while loop. But I'm stuck. This is what I've got so far (it is probably nonsense so sorry in advance). The question marks are what I need help on. Thanks again!

Do[ For[i = 1, i < 21, i++, ? While[i < 21, print[n*i]? ]]]

POSTED BY: Alice Bishop
4 Replies

And a good book to work your way though is the following:

I think that Szabolcs would agree with me that the next important step for you would be to spend time learning (by working through a course like this book or the on-line course that he pointed you to) how to program in Mathematica--and essential to this is to work many examples. Good luck!

POSTED BY: David Reiss

Here are several examples which should get you started:

With Do:

a = 1;
 a = a k,
 {k, 1, 10}

With For:

a = 1;
For[k = 1, k <= 10, k++,
 a = a k

With While:

a = 1;
k = 1;
While[k <= 10,
 a = a k++

These are called procedural constructs and they are not what we usually use in Mathematica (though they're definitely necessary and advantageous in some situations)

You could also use Apply:

Times @@ Range[10]

Or Fold:

Fold[Times, 1, Range[10]]

Or recursive programming:

fact[1] = 1;
fact[n_] := fact[n - 1] n

If you are a beginner in Mathematica I recommend that you avoid For for now in favour of Do or While. Use it only if:

  • You're facing one of the very rare cases when it really provides a simpler solution than the alternatives

  • You are translating code from a language which has an analogous for construct (C, C++, Java, etc.) and you want to minimize the risk of messing up something, at least during the first pass of translation

  • You are learning programming for the first time using Mathematica and you want to get familiar with this construct, which is very common in C-like languages. In this case it's good to get familiar with it, but I still don't recommend using it except when your explicit goal is to learn how to use For.

Finally, based on your question it sounds like you might find this course very useful:

It is very old, but most of it still applies to current versions of Mathematica and gives a good foundation (especially if you don't have a lot of experience with other programming languages either).

POSTED BY: Szabolcs Horvát
Posted 10 years ago

Hi Alice,

Daniel is of course correct, and perhaps has better sense than I do in that he gave you a direct answer to your question. But I would also like to point out that Mathematica offers a significantly different paradigm in programming compared to mainly procedural languages like C. It does of course have Do, For, and While -- and these are still useful in some calculations, especially where results are built up in an iteration, like you are doing calculating N factorial.

But I usually call Mathematica a 10th generation language. (I started calling it that somewhere around V4, so maybe I should increment that!) As an example, below are a few more ways to calculate N Factorial.

Kind regards and enjoy Mathematica,


In[1]:= (* by recursion *)
fact1[1] = 1;

In[2]:= fact1[n_] := n fact1[n - 1];

In[3]:= fact1[3]

Out[3]= 6

In[4]:= (* by Apply *)
fact2[n_] := Times @@ Range[1, n]

In[5]:= fact2[3]

Out[5]= 6

In[6]:= (* and of course by built in *)

Out[6]= 6

In[7]:= (* or *)

Out[7]= 6
POSTED BY: David Keith

(1) The inner While will run forever. Nothing inside it changes the condition to False.

(2) Mathematica is case sensitive. So you'd want Print where you have print.

(3) You are working too hard (I don't mean just from a Mathematica perspective, but from that of programming in general). Calculating 10! in this way should require a single loop in any language, not two (let alone three, which is what you have). You could for example start with n=1 and, at each step, change it via n=n*I.

POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract