Get startedGet started for free

Throw in more conditionals

In the previous exercise, you simulated the interaction between a driver and a driver's assistant: When the speed was too high, "Slow down!" got printed out to the console, resulting in a decrease of your speed by 7 units.

There are several ways in which you could make your driver's assistant more advanced. For example, the assistant could give you different messages based on your speed or provide you with a current speed at a given moment.

A while loop similar to the one you've coded in the previous exercise is already available for you to use. It prints out your current speed, but there's no code that decreases the speed variable yet, which is pretty dangerous. Can you make the appropriate changes?

This exercise is part of the course

Intermediate R

View Course

Exercise instructions

  • If the speed is greater than 48, have R print out "Slow down big time!", and decrease the speed by 11.
  • Otherwise, have R simply print out "Slow down!", and decrease the speed by 6.

If the session keeps timing out and throwing an error, you are probably stuck in an infinite loop! Check the body of your while loop and make sure you are assigning new values to speed.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Initialize the speed variable
speed <- 64

# Extend/adapt the while loop
while (speed > 30) {
  print(paste("Your speed is", speed))
  if (___) {
    print("___")
    speed <- ___
  } else {
    print("___")
    speed <- ___
  }
}
Edit and Run Code