if statement in R can only have one line?

前端 未结 5 1647
盖世英雄少女心
盖世英雄少女心 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.

提交回复
热议问题