Learn
A Night at the Movies
The Case Statement

Good work! Now we’ll want to create the main body of our program: the case statement, which will decide what actions to take based on what the user types in.

if and else are powerful, but we can get bogged down in ifs and elsifs if we have a lot of conditions to check. Thankfully, Ruby provides us with a concise alternative: the case statement. The syntax looks like this:

case language when "JS" puts "Websites!" when "Python" puts "Science!" when "Ruby" puts "Web apps!" else puts "I don't know!" end

The else is what the case statement will do if it doesn’t match any of its when statements to the case (in this case, the value of language).

Instructions

1.

Below your existing code, create a case statement for the choice variable with the following when conditions:

  1. when "add", please puts "Added!"
  2. when "update", please puts "Updated!"
  3. when "display", please puts "Movies!"
  4. when "delete", please puts "Deleted!"
  5. Otherwise (i.e. else), please puts "Error!"Don’t forget the end statement after your case/when lines.
Folder Icon

Take this course for free

Already have an account?