0

I'm trying to convert from below if-statement to switch statement but I'm keep getting error.

let meal = "breakfast"

if meal == "breakfast" {
    print("Good morning!")
} else if meal == "lunch" {
    print("Good afternoon!")
} else if meal == "dinner" {
    print("Good evening!")
} else {
    print("Hello!")
} 

this was my switch statement:

switch meal {
let meal = "breakfast"

    case 1: 
        print("Good morning!”)
    case 2: 
        print("Good afternoon!")
    case 3: 
        print("Good evening!")
}
1
  • assign let meal = "breakfast" before the switch and your cases should include the strings: case "breakfast" etc. And you'll need a default statement. Commented Aug 4, 2018 at 2:18

1 Answer 1

1
  let meal = "breakfast"

   switch meal {

   case "breakfast":

         print("Good morning")

   case "lunch":

          print("Good afternoon!")

   case "dinner":
          print("Good evening!")
   default:

          print("default case")
   }

put this switch block in your code :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.