I wish that the Mathematica documentation for the If command showed examples that have more than one statement for the t and f responses.
All three of these work:
No parentheses or braces:
Module[{x, y, z},
If[3 === 4,
x = 1;
y = 2;
z = x + y,
x = 5;
y = 9;
z = x + y];
Print[z]]
Use parentheses:
Module[{x, y, z},
If[3 === 4,
(x = 1;
y = 2;
z = x + y),
(x = 5;
y = 9;
z = x + y)];
Print[z]]
Use braces:
Module[{x, y, z},
If[3 === 4,
{x = 1;
y = 2;
z = x + y},
{x = 5;
y = 9;
z = x + y}];
Print[z]]
All give output 14.
Is it OK to use each of these three? If so, what do users prefer?
{z}(Also note that you generally don't need thePrintstatement at the end. A plainzwill do if you're working interactively.). All three work by making aCompoundExpression. (a;b;creally is justCompoundExpression[a, b, c]). You only need parens to resolve ambiguity. The first is therefore probably the cleanest. The third is definitely not what you want. (unless you are trying to build a list). – b3m2a1 May 27 '17 at 04:37The third is different in that it returns the list {z}. I think you meant to sayThe third is different in that it returns the list {z} if you don't use the Print command.Still, I really appreciate this answer. – David May 27 '17 at 05:02Ifreturns that. ThePrintcommand is really pretty much meaningless here. (By which I mean leaving it out will not change what you see in any way -- except that you'll have and"Output"cell instead of"Print"cell). – b3m2a1 May 27 '17 at 05:04