Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8102057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:08:19+00:00 2026-06-05T23:08:19+00:00

I would like to write an if statement that would continue to repeat a

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:08:21+00:00Added an answer on June 5, 2026 at 11:08 pm

    readline returns a character vector, so you need to coerce it to numeric before the if. Then you can use a while loop (as others have pointed out).

    fun <- function() {
      x <- as.numeric(readline("what is x? "))
      if(is.na(x)) stop("x must be a number")
      while(x > 5) {
        print("X must be less than 5")
        x <- as.numeric(readline("what is x? "))
        if(is.na(x)) stop("x must be a number")
      }
      print("Correct")
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write an if statement that will do something base on
I would like to know how to write this inside my hittest if statement:
I would like to write a T-SQL statement, that structures data to be loaded
I would like to write an If Statement in jQuery that will accomplish the
I would like to write inside that code (pretty-config.xml): <pretty-config xmlns=http://ocpsoft.com/prettyfaces/3.3.2 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://ocpsoft.com/prettyfaces/3.3.2 http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.2.xsd>
I would like to write a cross-platform function in C++ that contains system calls.
I have a template class, and would like to write a member method that's
I would like to write a JavaScript function that validates a zip code, by
In clojure, I would like to write a defn-my macro that creates a function
I'd like to write a SELECT statement that uses just one test to return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.