2
$\begingroup$

The following code works perfectly

For[i=1,i<4,i++,Print[i]]

However, when I break this into different lines, it stops working. Moreover, the braces appear to be pink or red

For
[
i=1,i<4,i++,
Print[i]
]

Why is this happening?

$\endgroup$
  • $\begingroup$ It's possible that your example could have worked under some very old versions of Mathematica, where line breaks tended to be ignored. $\endgroup$ – mikado Sep 19 '19 at 18:39
6
$\begingroup$

This happens because the expression

For

is complete at the location of the newline. Mathematica will not attempt to read further. Instead, it will try to interpret the next line as the start of a new expression.

Compare this with

For[

which is incomplete at the newline. Mathematica will read further to try to complete it.

Thus, use

For[
  i=1, i<4, i++,
  Print[i]
]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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