0

Is it possible to nest multiple if statements together within a while loop?

I'm attempting to create a simple example just to expose myself to them:

i <- 1 
while(i <=10) {
if(i > 6){
cat("i=",i,"and is bigger than 6.\n")
}else{if(3<i & i<6){
cat("i=",i,"and is between 3 and 6.\n")
}else{  
cat("i=",i,"and is 3 or less.\n")
}
i<-i+1
cat("At the bottom of the loop i is now =",i,"\n")
}

My sample code keeps getting stuck at i=7 and wants to run forever. How can I avoid this?

3
  • You have your answer - there were too many {'s. I would add to this that you would be well advised to take more care with how you format your code. Use spaces and indents correctly, and you will be much less likely to run into such problems. Commented Oct 4, 2017 at 1:48
  • Would you be willing to format the above code correctly so I know what you mean? Or provide an example? The above format follows my professor's approach but I would like to please know your clearer method. Commented Oct 4, 2017 at 12:02
  • Hadley's style guide is here: adv-r.had.co.nz/Style.html or the google style guide is here google.github.io/styleguide/Rguide.xml. Pick one and stick with it. Commented Oct 4, 2017 at 15:40

2 Answers 2

1

You had an extra { after the first else

i <- 1 
while(i <=10) {
  if(i > 6){
    cat("i=",i,"and is bigger than 6.\n")
  }else if(3<i & i<6){
    cat("i=",i,"and is between 3 and 6.\n")
  }else{  
    cat("i=",i,"and is 3 or less.\n")
  }
    i<-i+1
    cat("At the bottom of the loop i is now =",i,"\n")
}
Sign up to request clarification or add additional context in comments.

Comments

0

As mentioned by @Alex P you have an extra {.

However you can also simplify your else if by just checking if i is greater than equal 3 (you already know i will be less than or equal to 6 from it failing the first if condition where you check if i > 6):

i <- 1 
while(i <=10) {
    if(i > 6) {
        cat("i =", i, "and is bigger than 6.\n")
    } else if(i >= 3) {
        cat("i =", i ,"and is between 3 and 6 inclusive.\n")
    } else {  
        cat("i =", i ,"and is less than 3.\n")
    }
    i = i + 1
    cat("At the bottom of the loop i is now =", i ,"\n")
}

Output:

i = 1 and is less than 3.
At the bottom of the loop i is now = 2 
i = 2 and is less than 3.
At the bottom of the loop i is now = 3 
i = 3 and is between 3 and 6 inclusive.
At the bottom of the loop i is now = 4 
i = 4 and is between 3 and 6 inclusive.
At the bottom of the loop i is now = 5 
i = 5 and is between 3 and 6 inclusive.
At the bottom of the loop i is now = 6 
i = 6 and is between 3 and 6 inclusive.
At the bottom of the loop i is now = 7 
i = 7 and is bigger than 6.
At the bottom of the loop i is now = 8 
i = 8 and is bigger than 6.
At the bottom of the loop i is now = 9 
i = 9 and is bigger than 6.
At the bottom of the loop i is now = 10 
i = 10 and is bigger than 6.
At the bottom of the loop i is now = 11 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.