FAQ: Conditionals - Switch Statement

This community-built FAQ covers the "Switch Statement " exercise from the lesson “Conditionals”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Swift

FAQs on the exercise _Switch Statement _

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Still unclear why Switch is needed over an Else If. In the example with the cities, they seem to simply confirm you can do the same thing with Switch OR Else If. Any insight is appreciated.

2 Likes

I’ve got some confusion…

The lesson made me believe that using switch was INSTEAD of the else if, yet the only way to pass is for ALL of it to be used? And if this is true, then the next question I had was how the switch knew the expression was [Tony Stark]-but if it’s not true, then I’m unsure how switch figures this out, exactly.

So, is it switch OR else if or is it switch AND else if (and WHY, oh why???).

Yeah I’m with @philiphy—I don’t understand the difference between switch and else if, especially if the results are the same and the difference in syntax doesn’t really save you that much time. Else-if syntax feels more semantic than switch, too, so I could see myself more readily using else-if and remembering how to do it if I was working on a project.

Looks like this thread is dead since no one ever replied, a little disappointed in that.

EDIT

I Googled it and found that switch is probably a more advanced function than what Codecademy needs to teach at that point in the Swift course. The benefits just aren’t clear yet, though it makes sense to teach it since switch and else-if are so similar.
The switch function is “exhaustive”, so it’ll spit back an error if you forgot a case. An if-statement won’t. That’s the main benefit that’s relevant to someone at this point in learning Swift, I think. There are more but they wouldn’t make sense yet. I think if the module simply explained this then it would mitigate confusion.

3 Likes

I am also confused…
How does the computer interpret an else if statement from a switch statement?
The lesson says,

Unlike the if statement, a switch statement does not check for the value of a condition and instead finds and matches a case to a given expression.

But then it proceeds to say…

The value of the expression, originally "Rome" , is checked against each case within the switch block.

So my question is, how is checking an expression against each case in a switch statement different from checking the value of each condition in an if/else if/else statement? Or does the computer run it the same way, only we write it using different terms?

Thanks in advance for your input!

I am confused, the example shown in the article shows the following:
switch city {
case “Rapa Nui”:
print(“Moai :moyai:”)
case “New York”:
print(“Statue of Liberty :statue_of_liberty:”)
case “Rome”:
print(“Colosseum :classical_building:”)
default:
print(“A famous landmark is the Eiffel Tower!”)
}

The correct syntax used in the practice is
switch secretIdentity {
case “Tony Stark”:
superheroName = “Iron Man”
case “Natasha Romanoff”:
superheroName = “Black Widow”
case “Prince T’Challa”:
superheroName = “Black Panther”
case"Thor":
superheroName = “Thor”
default:
superheroName = “Unknown”
}

Are these both switch syntaxes that will produce the correct result?

Hi. On this Switch Statement lesson, I am confused why Print isn’t used for the code? It has superheroName instead, and doesn’t explain why that is being used instead of Print. And, the example on the lesson uses print, but the actual exercise does not. It’s like this new way of completing the lesson was added, without an explanation of why or how. Can someone explain to me why it switched over to this new syntax and how and when we should use it instead of print? Please and thank you

1 Like

The block of code for any case of the switch statement can contain a variety of statements. We can print or assign values to variables or do some calculations or …

If we simply want to print the superheroName, then sure we can print directly from the case blocks. However, if we expect to use the value somewhere else in our code, then it should be assigned to a variable so that later we can reuse the result. If we just want to print a string but not use it elsewhere, then we can print directly from the case blocks and skip assignment to a variable.

In the exercise, you are correct printing directly from the case blocks OR assigning to a variable and then printing the value of the variable after the switch statement, both will give us the same output. In the exercise, we are only printing the name, so assigning to a variable isn’t necessary other than to pass the automated check.

That being said, the if-else version in the exercise is not printing but assigning the names to the variable superheroName. So, it makes sense that in the switch version, we want to do the same instead of printing directly.

As an aside, in the following situation, it may be preferable to use a variable rather than repeating code:

// VERSION 1: Printing directly
var secretIdentity = ...
 
switch secretIdentity {
  case "Tony Stark":
    print("The superhero name of \(secretIdentity) is Iron Man.")
  case "Natasha Romanoff":
    print("The superhero name of \(secretIdentity) is Black Widow.")
  case "Prince T'Challa":
    print("The superhero name of \(secretIdentity) is Black Panther.")
  case "Thor":
    print("The superhero name of \(secretIdentity) is Thor.")
  default:
    print("The superhero name of \(secretIdentity) is Unknown.")
}

// VERSION 2: SAVING TO VARIABLE BEFORE PRINTING
var secretIdentity = ...
var superheroName: String 
 
// Write your code below
switch secretIdentity {
  case "Tony Stark":
    superheroName = "Iron Man"
  case "Natasha Romanoff":
    superheroName = "Black Widow"
  case "Prince T'Challa":
    superheroName = "Black Panther"
  case "Thor":
    superheroName = "Thor"
  default:
    superheroName = "Unknown"
}

print("The superhero name of \(secretIdentity) is \(superheroName).")