I would like to write an if statement that would continue to repeat a question until a certain condition is met
Something like this:
fun<-function(){
x<-readline("what is x? ")
if(x>5)
{print("X must be less than 5")
**repeat lines 3 & 4**
}else{
print("Correct")}
}
Sorry for the **- but I’m not really sure how to correctly write that line. What I am looking to do is to have the prompt “what is x” repeated every time a number greater than 5 is inputted until a number less than five is given. In theory the function would look like this
fun()
what is x? 6
X must be less than 5
what is x? 8
X must be less than 5
what is x? 3
Correct
readlinereturns a character vector, so you need to coerce it to numeric before theif. Then you can use awhileloop (as others have pointed out).