Loops for Beginner

Hi,

Welcome to the RStudio community!

It's nice of you to be honest and label this question as homework! Do know however we have policies around helping out in such cases:

So I can give you some tips, but no solutions :slight_smile:

  • The sample function is a great way to have a random pick out of a 'bag' of options, you can even set the probability of each outcome.
  • A while loop keeps on running until its condition to continue is no longer met. If you like to consider multiple conditions, you can chain them together with logical operators AND, OR, who are in R functions represented as &, | respectively. E.g. x == 0 & y < 5
  • If you're not sure what is happening in a while loop, either run the code inside manually or add a print statement to see the intermediate values appear of any variables of interest. E.g. print(x)

Let's see if this already helps :slight_smile:
Good luck!
PJ

1 Like