MathGroup Archive 1997

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

Search the Archive

Re: Which, If and "neither True nor False"

  • To: mathgroup at smc.vnet.net
  • Subject: [mg9705] Re: [mg9652] Which, If and "neither True nor False"
  • From: David Withoff <withoff>
  • Date: Fri, 21 Nov 1997 01:31:53 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

> I've just stumbled across a bug in some old code of mine, in which I had
> a statement of the form:
>
> Which[
>         a<b, DoSomething[],
>         a>c, DoSomethingElse[],
>         True, DoThis[]
>         ]
>
> which I assumed would call DoThis[] if neither a<b nor a>c were True.
> However, what I didn't realise is that if a is undefined, then the
> first two conditions are neither True nor False, and Mathematica
> returns the whole Which statement unevaluated!
>
> The If statement has an optional fourth argument which is executed if
> the test returns neither True nor False.  Is it possible to do the same
> in a Which statement?  If not, why not?!
>
> Paul
>
> Paul E Howland PhD MEng CEng MIEE        Room BY209 Senior Scientist
> DERA (Malvern) Land Systems Sector                      St Andrews Road
> Defence Evaluation & Research Agency     Malvern tel.
> +44-(0)1684-895767                  Worcestershire fax.
> +44-(0)1684-896315                  UK Email PEHowland at dera.gov.uk
> Web Site http://www.dera.gov.uk
> Official Disclaimer:
> The views expressed above are entirely those of the writer and do not
> represent the views, policy or understanding of any other person or
> official body.

Try

Which[
        TrueQ[a<b], DoSomething[],
        TrueQ[a>c], DoSomethingElse[],
        True, DoThis[]
        ]

This will cause indeterminate tests to be treated as False, so that the
last test will be used more or less in the same way as the fourth
element in If.

Dave Withoff
Wolfram Research


  • Prev by Date: Re: Which, If and "neither True nor False"
  • Next by Date: RE: Which, If and "neither True nor False"
  • Previous by thread: Re: Which, If and "neither True nor False"
  • Next by thread: RE: Which, If and "neither True nor False"