R Basics

4. While Loops

As seen with their ‘for’ counterpart, loops are a really useful tool that allow us to repeat instructions without repeating our code. It keeps programs light and easier to understand. For loops ran for a set amount of times – a number that we explicitly provide. Where while loops differ is that they will run until told not to. Take the example below:

   watchMinutes <- 0

   while (watchMinutes < 45){
       watchMinutes <- watchMinutes + 1
   print(paste0(watchMinutes, " played, it is half-time!"))

   ## [1] "45 played, it is half-time!"

Above, the referee starts their watch at 0 minutes. A while loop then begins, increasing the watchMinutes counter by 1 each time it runs.

It runs until the statement after ‘while’ becomes false (in this case, until minutes passes 45). It then stops and continues with the rest of the code – telling us that it is half time.

The structure is pretty simple, but we have to be very careful when using while loops. If the while statement never turns to ‘False’, we will create an infinite loop and break the program. Use carefully, or just use for loops where you can!

Let’s get another, more complex example, for a team that wants to see how many passes it might make, depending on its pass success rate.

It wants to know how many passes it can expect to make in a row, if they know that their pass success rate is 66%.

    keepPossession <- TRUE
    passSkill <- 66
    passesComplete <- 0

    while (keepPossession == TRUE){

      if (sample(1:100,1) > passSkill){
          keepPossession <- FALSE
          passesComplete <- passesComplete + 1


    ## [1] 1

So if your team has a 66% pass completion rate, you really can’t expect much flowing football!

Let’s break this down.

keepPossession: this tells R is it ‘True’ or ‘False’ that we keep the ball? passSkill: The % chance that we will keep the ball after a pass. Feel free to change this and see what numbers come up (but avoid 100 or more!)! passesComplete: This is simply our counter of how many passes.

We then run our while loop. This will run infinitely until we lose possession (keepPossession equals False).

In this loop, we add one pass to our counter, then run a random number generator with the sample() function. If this random number is greater than our skill level, we lose the ball and set keepPossession to False.

Once the loop is over, we receive our passesComplete count. If we do this lots of times, we’ll get a good average that we can expect to make each time, with some distribution above and below this number. Pretty cool – maybe find the % for your team and see what you can expect!


While loops give us another way to repeat our code, similar to the for loop. The structure is just as simple, but we need to be careful not to send our program into an infinite loop and completely break it!

We used these loops to act as a ref’s watch and to estimate how many passes a team might string together on any given opportunity. You’re now adding numbers to your scouting report & have something to train your team against too!

Next up, read more about the random number generation in general!

Next Lessons...

  1. Numbers, Variables & Strings
  2. Comparisons & Logic
  3. If Statements
  4. For Loops
  5. While Loops
  6. Lists
  7. Packages
  8. Creating Functions
  9. Random Numbers with Expected Goals

This is a R conversion of a tutorial by FC Python. I take no credit for the idea and have their blessing to make this conversion. All text is a direct copy unless changes were relevant. Please follow them on twitter and if you have a desire to learn Python then they are a fantastic resource!