| Author |
Comment/Response |
J F
|
08/26/08 2:44pm
I'd like to check one condition and perform a number of tasks if that condition is met. Here's logically what I'd like to do:
If condition == True
Print["True"]
Graphics3d[show two spheres]
Print["Spheres are pretty"]
c = a+b;
Print[c]
Else Print["Not true"]
However, I'm not sure how to frame this with Mathematica. Right now, I have it written as:
If[condition==true, Print["True"]]
If[condition==true, Graphics3D[show two spheres]]
If[condition==true, Print["Spheres are pretty"]]
If[condition==true, c = a+b;]
If[condition==true, Print[c], Print["Not true"]]
My notebook is filled with many cases like this, and right now it's quite unreadable. This program will be used and further edited by multiple people, so readability is very important. When I try to combine each of the tasks, however, I get lots of Null returned:
INPUT:
If[condition==true, Print["True"] Graphics3D[...]]
OUTPUT:
True
Null [small picture of spheres]
I've also tried If[True, {Print, Graphics3D}], but this returns the same poor result, except with the Null and the picture in brackets.
Ideally, it would be great if I could define functions and call them with simple If statements:
define a function called "True"
[
Print["True"]
Graphics3d[show two spheres]
Print["Spheres are pretty"]
c = a+b;
Print[c]
]
define a function called "False"
[
Print["False"]
Do other things;
]
If[condition==True, call function True]
If[condition==False, call function False]
Fitting everything into one If statement would be fine, though, as long as I stop getting those Null values returned.
Apologies if this has already been asked and answered - I searched the archive and did not find anything about this topic there. If I haven't been clear in my question, let me know and I'll try to elaborate. (Sorry about the amateurish pseudo-code!)
Many very heartfelt thanks in advance. :)
URL: , |
|