Mathematica For Loop Problem in Mathematica

  • Thread starter Thread starter EngWiPy
  • Start date Start date
  • Tags Tags
    Loop Mathematica
Click For Summary
To execute multiline code within a for loop in Mathematica, use semicolons (;) to separate each line of code instead of commas (,). This approach allows for the execution of multiple statements for each iteration of the loop. The discussion highlights the importance of using the correct syntax to avoid errors, emphasizing that this method can streamline coding efforts in Mathematica.
EngWiPy
Messages
1,361
Reaction score
61
Hello,
I want to execute a multiline code for each value of the for loop. How can I do that?
Regards
 
Physics news on Phys.org
For[
i = 0, i < n, i++,
line1;
line2;
line3
]
 
DaleSpam said:
For[
i = 0, i < n, i++,
line1;
line2;
line3
]

Thanks DaleSpam. I used ',' instead of ';', which was not accepted by Mathematica. This saves a lot of effort.
Best regards
 
No problem. You can always use ; to make a compound statement, so it is a general trick that helps elsewhere too.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K