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

This code works as expected:

isDefined = 2;
x = 1;
For[i = 1, i <= 3, i++,
    If[x + i > isDefined, x = 100];
    Print[StringJoin["i = ", ToString[i], ", x = ", ToString[x]]];

It produces output:

i = 1, x = 1
i = 2, x = 100
i = 3, x = 100

This code does not work as I would expect it to:

    Print[StringJoin["i = ",ToString[i],", x = ",ToString[x]]];

It produces output:

i = 1, x = 1
i = 2, x = 1
i = 3, x = 1

In both, it starts with x = 1, so when i = 2, x + i = 3 > 2. Since 2 is the value assigned to the variable isDefined, x is set equal to 100. The second example is the same code, except instead of comparing x + i with the variable isDefined, it compares x + i with the variable notDefined, a variable I never defined. Instead of giving an error message and stopping, or somehow indicating that I told it to compare the value of x + i with something that has not been assigned a value, it keeps right on going and never gives any indication that anything is wrong.

In the real code on which this example is based, I have lots of input data and I create output data based on calculations and conditions using the input data. I changed a variable name but accidentally left the old name in an if statement. The result was output that looked like the condition had never been met, when actually Mathematica just did what it did in the example above. Some programming languages won't compile/run if you try to do something like this, and if they don't do this by default, there's an option to specify that they shouldn't compile/run. I've looked through the preferences and online and can't find anything about this. I'd like to avoid the incorrect output that results from this problem. Does anyone have any ideas (other than the obvious solution of not making mistakes in the first place)?

share|improve this question


Built-in Mathematica symbol

If[condition,t,f,u] gives u if condition evaluates to neither True nor False.

x = 1;
For[i = 1, i <= 3, i++,
 If[x + i > notDefined, x = 100, , Throw["myError"]];
 Print[StringJoin["i = ", ToString[i], ", x = ", ToString[x]]];]

Throw::nocatch: Uncaught Throw[myError] returned to top level. >>

share|improve this answer
This solves the problem if you put the Throw["myError"] into every If[] from the beginning, or if you go through existing code and add it to every If[]. But is there a more general way to have it raise an error if you try to use a variable without a value like notDefined in the example? In other programming languages it wouldn't compile if you didn't declare the variable (e.g., "notDefined:undeclared identifier" in C, "notDefined is not declared" in or would result in an error at runtime if you tried to use the variable without a value. Can you make Mathematica do this too? – JRL Dec 29 '13 at 22:55

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.