Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: If without else

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113054] Re: If without else
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Tue, 12 Oct 2010 04:26:44 -0400 (EDT)
  • References: <i8ukmb$odm$1@smc.vnet.net>

If you want to drop the else part, you may also drop the comma from
the If.
If[b > 2, x = 10] will do.

Don't forget that If returns a value, so another option would be to
use something like:

x = If[b > 2, 10,x]

Cheers -- Sjoerd

On Oct 11, 11:16 am, Sebastian Schmitt <sschm... at physi.uni-
heidelberg.de> wrote:
> Dear all!
>
> (I recycle my disclaimer.)
>
> I'm new to Mathematica with a background mostly in C++. Many times I
> have the impression that my style is not natural-Mathematica
> (Mathematicaesque so to say).
>
> I have a question to the usage of the If condition.
>
> In[72] := x = 2; b = 3;
>
> In[73] := If[b > 2, x = 10,]; x
>
> Out[73] = 10
>
> In[74] := If[b > 3, x = 20,]; x
>
> Out[74] = 10
>
> Is it good-practice to leave the "else" empty? Is
>
> x := 10 /; b > 2
>
> what I'm looking for?
>
> Thanks in advance,
>
> Sebastian



  • Prev by Date: Re: Why I keep getting error exporting notebooks to HTML? Crashes
  • Next by Date: Re: Generation of polynomials
  • Previous by thread: Re: If without else
  • Next by thread: Re: If without else