# How to use do, for, while loops?

Posted 9 years ago
18314 Views
|
4 Replies
|
8 Total Likes
|
 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]? ]]]
4 Replies
Sort By:
Posted 9 years ago
 And a good book to work your way though is the following:http://www.amazon.com/Programming-Mathematica-Introduction-Paul-Wellin/dp/1107009464/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 9 years ago
 Here are several examples which should get you started:With Do: a = 1; Do[ a = a k, {k, 1, 10} ] a 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++ ] a 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 Or Fold: Fold[Times, 1, Range] Or recursive programming: fact = 1; fact[n_] := fact[n - 1] n fact 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 9 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,David In:= (* by recursion *) fact1 = 1; In:= fact1[n_] := n fact1[n - 1]; In:= fact1 Out= 6 In:= (* by Apply *) fact2[n_] := Times @@ Range[1, n] In:= fact2 Out= 6 In:= (* and of course by built in *) Factorial Out= 6 In:= (* or *) 3! Out= 6 
Posted 9 years ago
 (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.