# Can I code a conditional jump between cells in a notebook?

How can I code the Mathematica equivalent of the following pseudocode:

if[condition] ... goto[label_1] ... else goto[label_2]


where label_1 and label_2 are in different cells. I've trawled the documentation, the web and this SE, but can't find anything.

Specifically, I'm trying to compute the flow field of different types of fluids. The computation depends on the nature of the fluid, so, I want to say: if fluid_type_1, do algorithm_1, if fluid_type_2, do algorithm_2. Easy to do in Fortran, Python, etc., but I'd like to do it in Mathematica -- which is more accessible to my students.

• I would not try to do this at the notebook level. You can define two functions, then call the appropriate one based on the condition. This is easier and more flexible than trying to evaluate a certain cell based on the condition. Also, it doesn't depend on one particular notebook and its structure. Aug 27, 2015 at 15:54
• What exactly are you trying to do that is making you ponder coding gymnastics of this sort? Aug 27, 2015 at 16:20
• To put it clearer. Perhaps (almost surely) it can be done in some twisted way. But it is very, very far from the recommended Mathematica programming practices. If you care to explain your case someone will show you a much better alternative. Aug 27, 2015 at 18:46
• I'm trying to compute the stress state in hyperlastic materials, given the deformation state. The computation depends on the nature of the material, so, I want to say:
– rdt2
Aug 27, 2015 at 20:02
• Note that Mathematica doesn't depend on the notebook interface either, it can function without it. Programs can be (and are) written in plain text. Making a program dependent on notebook magic will make it impossible to transfer it to plain text (e.g. a package) and will make it difficult to transfer it between notebooks. Aug 27, 2015 at 20:23

Don't do this. You should never ever need this.

Instead, define f[x_, algorithm_] with a flag for which algorithm to use. This is roughly how all built-in functions work, though they usually use Options instead. Same principle, though. For example, say condition were 5>6, and you wanted to perform the Tarragon transform if condition were true, and the Manifesto transform otherwise.

f[x_, "Tarragon"] := x+5
f[x_, "Manifesto"] := x/2

If[5>6, f[1, "Tarragon"], f[1, "Manifesto"]]


You could even push the boat out and define two separate functions:

tarragon[x_] := x+5
manifesto[x_] := x/2

If[5/6, tarragon, manifesto][1]


As an aside, you may find Label useful (if you put everything into one cell, because it only works within CompoundExpressions). Never use Label, it's extremely bad practice.

f[a_] := Module[{x = 1., xp},
Label[begin];
If[Abs[xp - x] < 10^-8, Goto[end]];
xp = x;
x = (x + a/x)/2;
Goto[begin];
Label[end];
x]

• Sometimes I find it more readable to do f["Tarragon"][x_]:=... and etc. +1 Aug 27, 2015 at 20:22
• @N.J.Evans f[x_, ...]["Tarragon"] := ... would be more versatile, due to the fact that one cannot define Hold* attributes that affect subvalues. Aug 27, 2015 at 20:51
• I have found use for Label, admittedly I was symbolically evaluating Fortran code, but I found a use. To say it is bad practice, though, is open for debate. However, it is primarily useful in multiple exit points which can be easily handled by either a Catch/Throw block or restructuring you code to be more functional, so it is likely not useful in Mathematica. Oh, and +1. Aug 27, 2015 at 20:58
• Your example is better written: f[a_] := Module[{x = a/2., xp = a}, Label[begin]; If[Abs[xp - x] < 1.*^-8, Return[x]]; xp = x; x = .5 (x + a/x); Goto[begin]] Aug 28, 2015 at 15:45
• I just copy-pasted it from the docs - I think it's there for pedagogical value rather than anything else. Aug 28, 2015 at 15:50