MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Text and multiple calculations in an IF condition

  • To: mathgroup at smc.vnet.net
  • Subject: [mg100998] Re: Text and multiple calculations in an IF condition
  • From: Helen Read <hpr at together.net>
  • Date: Fri, 19 Jun 2009 20:48:41 -0400 (EDT)
  • References: <h1en44$i4q$1@smc.vnet.net>
  • Reply-to: HPR <read at math.uvm.edu>

Pascal Schulthess wrote:
> Hi everybody,
> 
> I have a small problem, and haven't found any solution yet. So I hope you 
> can help me.
> 
> I'd like to have describing text, comments and multiple calculations when 
> one condition is true, and others when it's false.
> 
> For example:
> 
> If[a>b,
> 	do something here;
> 	and something else here;
> 	insert a text here;
> else
> 	do something;
> 	and more;
> ]
> 
> As far as I tried this is not possible with the if condition provided?!
> But I also think this quite easy to do, if you know how to.

Here is the proper syntax for If, straight from the Documentation.

If[condition,t,f]

gives t if condition evaluates to True, and f if it evaluates to False.

Use a comma to separate t and f, not "else", like this.

If[a>b,
do something here;
and something else here;
insert a text here,

do something;
and more;
  ]

For example,

f[x_] := If[x > 3, Print[x^2]; Print["Hello"], Print[x^3];
   Print["Goodbye"]]

f[2]

8
Goodbye

f[4]

16
Hello





-- 
Helen Read
University of VErmont


  • Prev by Date: Re: Text and multiple calculations in an IF condition
  • Next by Date: Re: SphericalHarmonics strange behavior
  • Previous by thread: Re: Text and multiple calculations in an IF condition
  • Next by thread: Re: Text and multiple calculations in an IF condition