if statement in R can only have one line?

前端 未结 5 1619
盖世英雄少女心
盖世英雄少女心 2020-12-29 04:57

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         


        
相关标签:
5条回答
  • 2020-12-29 05:33

    To group statements, surround them with curly braces as you've done with the while loop:

    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)
    }
    

    This will allow you to place multiple statements inside the if, the else if and the final else.

    0 讨论(0)
  • 2020-12-29 05:39

    while the direct answer is, as has been noted, to use curly braces;
    it is worth adding that you can integrate the <- assignment operator into many functions.

    In your specific case:

        print(n <- 3*n+1)
    
       ## instead of 
       #  n <- 3*n+1
       #  print(n)
    


    note that using = here will NOT work. It must be <-

    0 讨论(0)
  • 2020-12-29 05:41

    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
    
    0 讨论(0)
  • 2020-12-29 05:42

    You have to use {} for allows the if statement to have more than one line. Try this:

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-29 05:46

    Ever heard of curly barces?

    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)
        }
    }
    
    0 讨论(0)
提交回复
热议问题