[Date Index]
[Thread Index]
[Author Index]
Re: How to tell Mathematica to stop conditional testing in an If statment if one condition is niether True or False? McCarthy evaluation rules? 'and then' test?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg69586] Re: How to tell Mathematica to stop conditional testing in an If statment if one condition is niether True or False? McCarthy evaluation rules? 'and then' test?
*From*: dimmechan at yahoo.com
*Date*: Sat, 16 Sep 2006 03:50:29 -0400 (EDT)
*References*: <eebfu3$5dt$1@smc.vnet.net>
***SameQ works as desired if b is symbolic or numeric with a value
different from 5.
Indeed
In[102]:=Quit
In[4]:=xc=Table[i,{i,1,3}]
x=5;
If[x===b&&xc[[10]]\[Equal]4,Print["True"],Print["False"],Print["Can't
decide"]]
Out[4]={1,2,3}
>From In[4]:=
False
***and
In[7]:=xc=Table[i,{i,1,3}]
x=5;
If[x===(b /. b -> 5))&&xc[[10]]\[Equal]4,Print["True"],
Print["False"],Print["Can't decide"]]
Out[7]={1,2,3}
>From In[7]:=
False
But if you assign the value 5 to b you get again the same error
message.
In[10]:=xc=Table[i,{i,1,3}]
x=5;
If[x===(b/.b\[Rule]5)&&xc[[10]]\[Equal]4,
Print["True"],Print["False"],Print["Can't decide"]]
Out[10]={1,2,3}
>From In[10]:=
Part::partw: Part 10 of {1, 2, 3} does not exist. More...
>From In[10]:=
Can't decide
***But you can overcome this problem as it is shown below:
In[17]:=b=.
In[18]:=
xc=Table[i,{i,1,3}]
x=5;b=5;
If[b\[Equal]5,Print["False"],If[x===b&&
xc[[10]]\[Equal]4,Print["True"],Print["False"],Print["Can't
decide"]]]
Out[18]={1,2,3}
>From In[18]:=
False
In[19]:=
xc=Table[i,{i,1,3}]
x=5;b=8;
If[b\[Equal]5,Print["
False"],If[x===b&&xc[[10]]\[Equal]4,Print["True"],
Print["False"],Print["Can't decide"]]]
Out[19]={1,2,3}
>From In[19]:=
False
In[20]:=
xc=Table[i,{i,1,3}]
x=5;b=a;
If[b\[Equal]5,Print["
False"],If[x===b&&c[[10]]\[Equal]4,
Print["True"],Print["False"]],Print["Can't decide"]]
Out[133]={1,2,3}
>From In[133]:=
Can't decide
Dimitris Anagnostou
Nasser Abbasi wrote:
> I can better describe this with simple example:
>
> ------------- code ------------
>
> Remove["Global`*"];
> xc = Table[i, {i, 1, 3}]
> x = 5;
> If[x == b && xc[[10]] == 4, Print["True"], Print["False"], Print["Can't
> decide"]]
>
> ----- end code -------------
> In the above, 'x==b' is neither True nor False, since 'b' has no numerical
> value.
>
> But what I want is when this happens, for Mathematica to NOT continue with
> the testing if xc[[10]]==4 is True (because even if it is True, it will
> not change the outcome, which is can't decide.
>
> I am looking for something like 'and then' which says to test the next
> condition only if the one just tested was true.
>
> The interesting thing is that if 'b' had a value, say 7, which makes the
> first test (the x==b) to be False, then Mathematica does the right thing,
> and will not try to check the xc[[10]]==4 condition. I need it to do the
> same thing when also the result of the check is 'undecided', not just
> 'False' or 'True'.
>
> Is there a way to do this? Notice in the example above, I get the error
> that xc[[10]] is out of bound, but still get the can't decide message.
>
> It is clear to me that the way Mathematica does it now is not the right way.
> I do not see why it tries to check for xc[[10]]==4 when it will not make a
> difference to the final result.
>
> any thoughts?
>
> thanks,
> Nasser
Prev by Date:
**Re: Why is the negative root?**
Next by Date:
**RE: Variables Within Homemade Functions**
Previous by thread:
**Re: How to tell Mathematica to stop conditional testing in an If statment if one condition is niether True or False? McCarthy evaluation rules? 'and then' test?**
Next by thread:
**(again) common problem with FindMinimum and NIntegrate**
| |