Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement in R can only have one line?

Tags:

I was trying a tiny code with if statement, although it is very simple,but there is something I really confused here is the code

n<-857 while(n!=1){ if(n<=0)  print("please input a positive integer") else if(n%%2==0)  n<-n/2  print(n) else  n<-3*n+1  print(n)   } 

as we see above,when running this code in R, there comes the error,but if I change the if statement like this

if(n<=0)      print("please input a positive integer")     else if(n%%2==0)      n<-n/2     else      n<-3*n+1 

it is ok ,my question is that can we only write one line under each judgement? if I want to do something more after each judge, what should I do ,just like this case, I want to change the value of n,but also want to display it, what should I do? thank you very much

like image 677
TOPMAD Avatar asked Mar 23 '13 12:03

TOPMAD


People also ask

How do you write an if-else in one line in R?

Syntax of ifelse() function This returned vector has element from x if the corresponding value of test_expression is TRUE or from y if the corresponding value of test_expression is FALSE . This is to say, the i-th element of result will be x[i] if test_expression[i] is TRUE else it will take the value of y[i] .

Can you have multiple if statements in R?

Nested if StatementYou can write one if statement inside another if statement to test more than one condition and return different results.

Are one line if statements OK?

Only use single-line if statements on a single line While the compiler sees this as one statement guarded by a single condition, humans often accidentally read this is an if block, whether there are curly braces or not, thanks to the indentation. Humans notice the indentation, the compiler does not.


1 Answers

To be precise, this is not about lines but about statements. You can have the whole if else statement in one line:

> if (TRUE) 1 else 3 [1] 1 

A statement will end at the end of the line (if complete), you can see that nicely in interactive mode if you enter the code line by line:

> if (TRUE)  + 1 [1] 1 > else Fehler: Unerwartete(s) 'else' in "else" # error: unexpected 'else' in "else" > 3 [1] 3 

if can come in form if (condition) statement or if (condition) statement else other.statement, the interpreter assumes the first version is meant if the statement is complete after line 2 - in interactive mode it cannot sensibly wait whether an else appears next. This is different in sourced code - there it is clear with the next line which form it is.

Semicolons end statements as well:

> if (TRUE) 1; else 3 [1] 1 Fehler: Unerwartete(s) 'else' in " else"  # error: unexpected 'else' in "else" 

But you can only have one statement in each branch of the condition.

> if (TRUE) 1; 2 else 3 [1] 1 Fehler: Unerwartete(s) 'else' in " 2 else" # error: unexpected 'else' in "2 else" 

Curly braces group statements so they appear as one statement.

> if (TRUE) {1; 2} else 3 [1] 2 
like image 135
cbeleites unhappy with SX Avatar answered Oct 29 '22 09:10

cbeleites unhappy with SX