Message Boards Message Boards

0
|
10857 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
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! 
POSTED BY: Ken Yong
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:
Do[
    code
,{i,1,3},{j,1,2}];
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:

http://www.wolfram.com/broadcast/video.php?channel=89&video=409

a
nd begin to get launched into learning things here:

http://reference.wolfram.com/mathematica/guide/CoreLanguageOverview.html
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract