# Using a 'Do' loop to redefine my 'For' loop function?

**URL:** <https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423>\
**Category:** Questions\
**Created:** [October 13, 2016, 1:49am UTC](https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423 "2016-10-13T01:49:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdavis8594](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bdavis8594](https://community.wolfram.com/u/bdavis8594)\
**Post date:** [October 13, 2016, 1:49am UTC](https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423/1 "2016-10-13T01:49:35Z")

</div>

I am trying to produce this same output using a Do loop.  
Here is the code I have for my For loop.

```
primelist[m_] := 
 (Clear[ourlist]; 
  ourlist = {}; 
  For[
   k = 1, (* Initial k value, this is where we start*)
   k <= m, (* For loop will stop at the last prime number less than or equal to whatever we specify for "m", this is what were testing *)
   k = k + 1,(* value we are testing to determine if prime or not prime, this is the increments *)
   If[PrimeQ[k], AppendTo[ourlist, k]] (* appends k to our empty list defined as ourlist = {}, this is where the prime numbers will be stored in list form, this is the body *)
   ];
  ourlist)
primelist[25]

```

here is the output as expected:

```
{2, 3, 5, 7, 11, 13, 17, 19, 23}

```

I know For loops are most common, but I also know you can use a For, Do and While loop to basically do the same thing if you wanted. I’m trying to find a way using the Do loop in my case.

Thanks for any help,

B

---

<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:** [October 13, 2016, 3:34am UTC](https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423/2 "2016-10-13T03:34:04Z")

</div>

Is there a reason why you are using procedural programming to do this? The Wolfram Language is much more powerful than this and typically things like `For` and `Do` loops are rarely needed. For example

```
Select[Range[25], PrimeQ]

```

---

<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:** [October 13, 2016, 3:36am UTC](https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423/3 "2016-10-13T03:36:52Z")

</div>

But if you insist on using a `Do` loop here is an example:

```
primes = {};
Do[
  If[PrimeQ[i], AppendTo[primes, i]],
  {i, 25}];
primes

```

---

<div class="post-metadata">

**Author:** ![bdavis8594](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bdavis8594](https://community.wolfram.com/u/bdavis8594)\
**Post date:** [October 13, 2016, 4:21am UTC](https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423/4 "2016-10-13T04:21:33Z")

</div>

Using a ‘Do’ loop to redefine my ‘For’ loop function.

---

<div class="post-metadata">

**Author:** ![bdavis8594](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bdavis8594](https://community.wolfram.com/u/bdavis8594)\
**Post date:** [October 13, 2016, 5:01am UTC](https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423/5 "2016-10-13T05:01:57Z")

</div>

Hey David,

I use ‘For’ loops frequently in R, but I am trying to get a feel for loops in different programs, for this example, in Mathematica. I’m at a beginners level in Mathematica but the logic is very similar to other programs. I didn’t even know it could be done the first way. I appreciate both methods. I just know how useful and widely used loops are that I am trying to practice through various methods, such as For, While and Do loops. Thanks again!

Is using “While” similar to the “Do” code you created? I tried now using While but it seems a bit harder. Well, I know all 3 loops have different syntax, etc.

Brandon

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [October 13, 2016, 11:19am UTC](https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423/6 "2016-10-13T11:19:49Z")

</div>

The solution by David is very good. But an even more efficient way is actually directly asking for primes: so no Select or For or Do necessary:

```
Prime[Range[PrimePi[25]]]

```

PrimePi[25] will give show that the lowest prima equal or below 25 is the 9th prime. Then the numbers 1 through 9 are created {1,2,3,4,5,6,7,8,9}, and then for each, a certain prime is asked.

---

<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:** [October 13, 2016, 3:27pm UTC](https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423/7 "2016-10-13T15:27:46Z")

</div>

True! But to implement your last sentence would involve a `Select` or a `Cases` line of code. So, it depends on what one wants the function to return.

---

<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:** [October 13, 2016, 3:32pm UTC](https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423/8 "2016-10-13T15:32:16Z")

</div>

The best thing to do for leaning Mathematica is not to try to implement the coding paradigms of other languages. Although they exist in Mathematica for the sake of completeness (and, yes, sometimes they are the right choice for something very particular), the paradigms in Mathematica are in Funcitonal, pattern matching, and rule-based prorramming–and the language for these (I may have left something out), or at least how they are combined into one language is unique to Mathematica. The best thing for a beginner to do to learn the language is to use one of the excellent books out there or to use Stephen Wolfram’s recent book which is on-line at

> **[An Elementary Introduction to the Wolfram Language by Stephen Wolfram](https://www.wolfram.com/language/elementary-introduction/3rd-ed/)**
>
> Start using the Wolfram Language. Stephen Wolfram's book introduces the basics with a series of easy-to-follow examples, exercises, QA, tech notes.

Start there and go through it step by step to get the sense of how to write code in Mathematica

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [October 13, 2016, 3:32pm UTC](https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423/9 "2016-10-13T15:32:23Z")

</div>

No it does not, look at the code. No need for Cases, DeleteCases, Select, If or so… It doesn’t eliminate elements, Basically PrimePi will figure out the number of primes, and then those are asked.

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [October 13, 2016, 3:34pm UTC](https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423/10 "2016-10-13T15:34:11Z")

</div>

Agreed, ‘translating’ languages to Mathematica almost always leaves you with slow, verbose, low-level code… There are exceptions of course…

---

<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:** [October 13, 2016, 3:34pm UTC](https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423/11 "2016-10-13T15:34:21Z")

</div>

Oh, right, my bad 😉

---

<div class="post-metadata">

**Author:** ![bdavis8594](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bdavis8594](https://community.wolfram.com/u/bdavis8594)\
**Post date:** [October 13, 2016, 5:12pm UTC](https://community.wolfram.com/t/using-a-do-loop-to-redefine-my-for-loop-function/9423/12 "2016-10-13T17:12:47Z")

</div>

Very informative Thanks to the both of you,

B
