Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a simple function:

f[x0_,y0_]:= Module[{x=x0,y=y0},For[i=0,i<2,i++;Print[x],Print[x+y]];]

So when I call it:

I get:


Why not:


as expected? Also my indentation of - or + don't seem to format?

share|improve this question
Try f[x0_, y0_] := Module[{x = x0, y = y0},For[i = 0, i < 2,i++;Print[x]; Print[x + y]];]. This works as you expect it to work. – Öskå Nov 24 '12 at 12:08
Hi @Öskå Thanks, but is the semicolon not the standard way of suppressing an output, anyway it works but I can't figure out why comma reverses the order and semicolon works? – sebastian c. Nov 24 '12 at 12:16
The Print function will print whether you add a ; in the end or not. Although I have no clue why that comma changes the order. – Öskå Nov 24 '12 at 12:25
With the comma the first print is evaluated during incrementation, and not in the body. Hence the reversed order.… – ssch Nov 24 '12 at 12:30

For syntax is like this:


Your first Print is part of the incrementation step, due to ; combining it into one expression

For[i = 0, i <= 3, Print["incrementing from ",i]; i++, Print[i]]
(* Result:
incrementing from 0
incrementing from 1
incrementing from 2
incrementing from 3

You can put it all in the body like (note position of commas and semicolons):

For[i = 0, i < 2,i++,Print[x]; Print[x + y]]

I recommend you actively try to avoid For loops when coding in Mathematica, see for instance Functional Programming: Quick Start.

And check the other links in this popular answer under the section Basic advices for people new to Mathematica

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.