Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Basics (retired) Control Flow Switch Statement

for ... in (meaning)

when you put in the playground

let card = 1...13

for card in cards { switch card{

what is the card ( that comes after the "for") and the cards (that comes after the (in") mean? what are they referring to ?

3 Answers

Stone Preston
Stone Preston
42,016 Points

a for in loop loops over all the values in an array and makes the item accessible in a local variable.

so with

for card in cards 

the card variable is the item in the array, and cards is the array. you can then do something to every single card through the use of the card variable

for example, if I had an array of animals called animals, and wanted to loop over every single animal in that array and print it I would use

for animal in animals {

and every item in that array would get printed.

the name you give to the variable that comes after for does not really matter, it can be anything you want. Most of the time you have an array that stores a collection of things, so usually the variable is just the singular form of that thing

for thing in things {}
for rabbit in rabbits{}

but it can be any valid variable name

for asdf in things {}
for blah in rabbits{}

thanks that helped me understand it a lot better!

Abdurahman Sharif
Abdurahman Sharif
1,205 Points

wow that was really helpful.thanks stone!!

Michael Pastran
Michael Pastran
4,727 Points

Thanks!! really helps clear stuff up

Omar Nunez
Omar Nunez
4,101 Points

card is a variable included in the array cards is a array constant(let)

in your first line you should refer to cards and no card (¨let card = 1...13¨) and it will be more clear.