# How to use do, for, while loops?

**URL:** <https://community.wolfram.com/t/how-to-use-do-for-while-loops/4201>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [December 2, 2014, 3:34pm UTC](https://community.wolfram.com/t/how-to-use-do-for-while-loops/4201 "2014-12-02T15:34:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![theundescribable](https://avatars.discourse-cdn.com/v4/letter/t/8edcca/32.png) [@theundescribable](https://community.wolfram.com/u/theundescribable)\
**Post date:** [December 2, 2014, 3:34pm UTC](https://community.wolfram.com/t/how-to-use-do-for-while-loops/4201/1 "2014-12-02T15:34:50Z")

</div>

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]?  
]]]

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [December 2, 2014, 4:00pm UTC](https://community.wolfram.com/t/how-to-use-do-for-while-loops/4201/2 "2014-12-02T16:00:49Z")

</div>

(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`.

---

<div class="post-metadata">

**Author:** ![dlkeith](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dlkeith/32/31494_2.png) [@dlkeith](https://community.wolfram.com/u/dlkeith)\
**Post date:** [December 2, 2014, 5:12pm UTC](https://community.wolfram.com/t/how-to-use-do-for-while-loops/4201/3 "2014-12-02T17:12:17Z")

</div>

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[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 *)
Factorial[3]

Out[6]= 6

In[7]:= (* or *)
3!

Out[7]= 6

```

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [December 2, 2014, 6:18pm UTC](https://community.wolfram.com/t/how-to-use-do-for-while-loops/4201/4 "2014-12-02T18:18:08Z")

</div>

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[10]

```

Or `Fold`:

```
Fold[Times, 1, Range[10]]

```

Or recursive programming:

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

```

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:

- [Programming Paradigms via Mathematica (A First Course) -- from Wolfram Library Archive](http://library.wolfram.com/infocenter/MathSource/1847/)

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).

---

<div class="post-metadata">

**Author:** ![dreiss](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dreiss/32/29094_2.png) [@dreiss](https://community.wolfram.com/u/dreiss)\
**Post date:** [December 2, 2014, 6:27pm UTC](https://community.wolfram.com/t/how-to-use-do-for-while-loops/4201/5 "2014-12-02T18:27:41Z")

</div>

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

> **[Programming with Mathematica®: An Introduction: Wellin, Paul: 9781107009462:...](https://www.amazon.com/Programming-Mathematica%C2%AE-Introduction-Paul-Wellin/dp/1107009464)**
>
> Programming with Mathematica®: An Introduction \[Wellin, Paul\] on Amazon.com. \*FREE\* shipping on qualifying offers. Programming with Mathematica®: An Introduction

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!
