Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected 'else' in "else" error

Tags:

r

if-statement

I get this error:

Error: unexpected 'else' in " else"

From this if, else statement:

if (dsnt<0.05) {
     wilcox.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE) }
else {
      if (dst<0.05) {
wilcox.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE) }
   else {
         t.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE)       } }

What is wrong with this?

like image 909
Lucia Avatar asked Feb 13 '13 23:02

Lucia


People also ask

How do I fix unexpected token else?

Answer 50aa6266db2df2c0d8006d0fRemove the semi-colon after specifying the condition in () in the first If statement. Correct syntax is : if (yourName. length>0 && gender.

Why is my else statement not working?

If you are getting an error about the else it is because you've told the interpreter that the ; was the end of your if statement so when it finds the else a few lines later it starts complaining. A few examples of where not to put a semicolon: if (age < 18); if ( 9 > 10 ); if ("Yogi Bear". length < 3); if ("Jon".

Why is else unexpected?

Example 1: Reproduce the Error – unexpected else in “else” We received an error message! The reason for this error is that we didn't write the else statement to the same line as the closing curly bracket of the if statement.

What is else error?

This error means that Java is unable to find an if statement associated to your else statement. Else statements do not work unless they are associated with an if statement. Ensure that you have an if statement and that your else statement isn't nested within your if statement.


2 Answers

You need to rearrange your curly brackets. Your first statement is complete, so R interprets it as such and produces syntax errors on the other lines. Your code should look like:

if (dsnt<0.05) {   wilcox.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE) } else if (dst<0.05) {   wilcox.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE) } else {   t.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE)        }  

To put it more simply, if you have:

if(condition == TRUE) x <- TRUE else x <- FALSE 

Then R reads the first line and because it is complete, runs that in its entirety. When it gets to the next line, it goes "Else? Else what?" because it is a completely new statement. To have R interpret the else as part of the preceding if statement, you must have curly brackets to tell R that you aren't yet finished:

if(condition == TRUE) {x <- TRUE  } else {x <- FALSE} 
like image 127
sebastian-c Avatar answered Oct 13 '22 21:10

sebastian-c


I would suggest to read up a bit on the syntax. See here.

if (dsnt<0.05) {   wilcox.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE)  } else if (dst<0.05) {     wilcox.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE) } else    t.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE) 
like image 22
nadizan Avatar answered Oct 13 '22 19:10

nadizan