Message Boards Message Boards

2 Replies
1 Total Likes
View groups...
Share this post:

Mathematica For Loop with multiple iterators?

Posted 11 years ago
I'm kinda new to using Mathematica, so any help is appreciated.
Is it possible in Mathematica to use multiple iterators in for loops?   Something like
For[i=1, j=1, i<1, j<3, i++, j++, ]
and so on?  I'm getting an error on this,  is there a different way to write this sort of syntax in Mathematica?  Thanks! 
2 Replies
What is it that you want to archive? For loops can usually (and should) be avoided, since there are better constructs.

In your example, it looks like i and j always have the same value, so one of them is redunant. If this is not the intention, but you want all pairs of combinations,
then you can use Do:
You can try something like this:
For[(i = 1; j = 1),
   i < 3 && j < 3,
   (i++; j++)]

I.e., use compound espressions and the And operator (the &&).  (Note that I changed your i<1 to i<3 so something would actually happen.) But, as many in this group will suggest, you really should take some time to learn about the different programming approaches in Mathematica so that you move away from the procedural approach of using things like For  and Do loops.  

You can get a taste here:

nd begin to get launched into learning things here:
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract