# st: Re: Trouble with " if"

 From "Michael Blasnik" To Subject st: Re: Trouble with " if" Date Fri, 12 Mar 2004 09:29:41 -0500

```Although it is easy to understand the misinterpretation, Stata is working
according to its stated rules.  The expression is evaluated left to right
and therefore the first equality is evaluated first as a logical condition
and the result of that condition (0 or 1) is then used to assess the second
equality.  Therefore

2==2==1  evaluates to true because 2==2 is true, which Stata assigns a value
of 1 (it would be 0 if it were false) and then 1==1 is true.  Parentheses
might make it more clear:

(2==2)==1  is more obviously true, but Stata does not require parentheses to
follow it's rules concerning order of evaluation

Michael Blasnik
michael.blasnik@verizon.net

----- Original Message -----
From: "Christophe Bontemps" <bontemps@toulouse.inra.fr>
To: <statalist@hsphsun2.harvard.edu>
Sent: Friday, March 12, 2004 8:45 AM
Subject: st: Trouble with " if"

> Hi Everybody,
> I find strange the way stata handle a double equality condition in "if"
> statements
>
> Running  the following trible loop on i, j and k with a condition like"
if
> `i'==`j'==`k'   " gives a wrong answer, no warnings, no errors.
> Of course i know the solution for this problem but why does Stata accept
> these misleading  statements  in "if" ?
> running  on Stata 8 SE the following :
> forvalues i = 1/3 {
>     forvalues j = 1/3 {
>        forvalues k = 1/3 {
>             display in yellow " i=`i'  j=`j'  k=`k' "
>
>              if `i'==`j'==`k' {
>                  di in green " case 1"
>              }
>              else {
>               di in red "case 2"
>              }
>         }
>     }
> }
> Gives
>  i=1  j=1  k=1
>  case 1
>  i=1  j=1  k=2
> case 2
>  .
>  .
>  i=2  j=2  k=1
>  case 1
>  i=2  j=2  k=2
> case 2
>  i=2  j=2  k=3
> .
> .
>  i=3  j=3  k=1
>  case 1
>  i=3  j=3  k=2
> case 2
>  i=3  j=3  k=3
> case 2
>
>
> Thanks
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```