FAQ: Conditionals - Switch Statement: Interval Matching

This community-built FAQ covers the "Switch Statement: Interval Matching " 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: Interval Matching _

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!

The lessons says that two variables have been initialized. I thought villain wasn’t initialized? It doesn’t have an assigned value.

Sidenote: the Swift error messages seem quite friendly.

So, I’m confused as to why I have to input
villain = “Emperor Palapatine”
when I could just do
print (“Emperor Palapatine”). I don’t understand why we’d use the former and not the later. The later actually make the code work. The ‘villain =’ doesn’t make the code work but the ‘Next’ but is all excited and says continue?

I definitely agree with the error messages.

I’m not sure if I should be asking this here but this is my code and I’m not getting any errors but nothing is printing just wondering if I did something wrong or if it is a bug.

var episode = 2
var villain: String

// Write your code below :supervillain:

switch episode {
case 1…3:
villain = “Emperor Palpatine”
case 4…6:
villain = “Darth Vader”
case 7…9:
villain = “Kylo Ren”
default:
villain = “”

print(villain)
}

1 Like

The print(villain) statement is meant to be outside the switch statement. So, it should come after the closing curly brace of the switch statement.

2 Likes

@byteblitz hmm I think either the wording may be a little wack or the lesson meant to say that it has already created the two variables we need to use in the exercise…

@blogcoder83184 My take on this is that both ways work, however, if we run it using print (“Emperor Palapatine”), the code isn’t saved, whereas if we store the result as a variable (in this case, villain = “Emperor Palapatine”), we can always use it later, if needed, by calling the variable…

Hope this helped!

1 Like

I had super fun with this lesson. I recognized the mistake I made yesterday and fixed it before I got an error and it worked out ok. I also enjoyed step four and playing around with it until I was able to feel more confident about the whole use of switches

Came here with exactly this question. The lesson definitely doesn’t distinguish exactly where print() goes but accepts its placement within the curly bracket as correct. Very confusing!

1 Like

Hello everyone. Can you explain to a beginner
why I have to write this

villain = names

every time? what is the logical meaning of this piece thank you very much

It should be “Emperor Palpatine” for every switch case :nerd_face:

Why do I get an error when I put in villain == “Darth Vader”" etc instead of villain = “Darth Vader” in the switch statement?

== is used for comparisons.

= is used for assignment.

You want to assign the string "Darth Vader" to the villain variable.

1 Like