Aan de slagGa gratis aan de slag

Add an else

You can only use an else statement in combination with an if statement. The else statement does not require a condition; its corresponding code is simply run if all of the preceding conditions in the control structure are FALSE. Here's a recipe for its usage:

if (condition) {
  expr1
} else {
  expr2
}

It's important that the else keyword comes on the same line as the closing bracket of the if part!

Both if statements that you coded in the previous exercises are already available to use. It's now up to you to extend them with the appropriate else statements!

Deze oefening maakt deel uit van de cursus

Intermediate R

Cursus bekijken

Oefeninstructies

Add an else statement to both control structures, such that

  • "Unknown medium" gets printed out to the console when the if-condition on medium does not hold.
  • R prints out "Try to be more visible!" when the if-condition on num_views is not met.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Variables related to your last day of recordings
medium <- "LinkedIn"
num_views <- 14

# Control structure for medium
if (medium == "LinkedIn") {
  print("Showing LinkedIn information")
}



# Control structure for num_views
if (num_views > 15) {
  print("You're popular!")
}
Code bewerken en uitvoeren