Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Performing many tasks in one If statement' topicStudent Support Forum > General > "Performing many tasks in one If statement"

Next Comment >Help | Reply To Topic
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: ,

Subject (listing for 'Performing many tasks in one If statement')
Author Date Posted
Performing many tasks in one If statement J F 08/26/08 2:44pm
Re: Performing many tasks in one If statement Xavier 08/27/08 00:12am
Re: Re: Performing many tasks in one If statement J F 08/28/08 10:58am
Re: Re: Re: Performing many tasks in one If sta... Xavier 08/29/08 09:16am
Re: Re: Re: Performing many tasks in one If sta... Jason Biggs 08/29/08 11:44pm
Re: Re: Re: Re: Performing many tasks in one If... J F 09/02/08 12:52pm
Next Comment >Help | Reply To Topic