Skip to content
Back to formatted view

Raw Message

Message-ID: <4EC31BD1.8070707@gmail.com>
Date: 2011-11-16T02:11:29Z
From: P.B. Lecavalier
Subject: if/else scope
In-Reply-To: <001201cca3f0$c8774540$5965cfc0$@charter.net>

On 11/15/2011 06:46 PM, Kevin Burton wrote:
> What is wrong with the following?
>
>
>
> x<- 1:2
>
> if(x[1]>  0)
>
> {
>
>                  if(x[2]>  0)
>
>                  {
>
>                                  print("1&  2>  0")
>
>                  }
>
>                  else
>
>                  {
>
>                                  print("1>  0")
>
>                  }
>
> }
>
> else
>
> {
>
>                  if(x[2]>  0)
>
>                  {
>
>                                  print("2>  0")
>
>                  }
>
>                  else
>
>                  {
>
>                                  print("NONE>  0")
>
>                  }
>
> }
>
>
>
> Gives me
>
>
>
> Error: unexpected 'else' in "else"
>
>
>
> What am I missing?
>
>
>
> Kevin
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
x <- 1:2

#this works

if (x[1] > 0)
    { if (x[2] > 0)   print("1 & 2 > 0")  else  print("1 > 0")  } else
       { if (x[2] > 0) print("2 > 0")  else print("NONE > 0") }

#this works

if (x[1] > 0)
    { if (x[2] > 0)   print("1 & 2 > 0")
     else  print("1 > 0")  }         else
  { if (x[2] > 0) print("2 > 0")
     else print("NONE > 0") }

#this doesn't

if (x[1] > 0)
    { if (x[2] > 0)   print("1 & 2 > 0")
     else  print("1 > 0")  }
else  { if (x[2] > 0) print("2 > 0")  else print("NONE > 0") }


Conclusion: if-else in R is not quite free-form.


-- 
Philippe Baril Lecavalier