Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Min Choi
1,517 PointsQuestion about a switch statement inside a for in loop
Okay so in a tutorial that I was watching there was these lines of code:
let airportCodes = ["LGA", "LHR", "CDG", "HKG", "DXB"]
for airportCode in airportCodes { switch airportCode { case "LGA": print("New York") case "LHR": print("London") case "CDG": print("Paris") case "HKG": print("Hong Kong") default: print("I don't know which city this airport is in!") } }
I understand that the for in loop runs every value in the array through airportCode and that is why every city gets printed. However, I don't understand why the default is printed as well. If none of the other options are found to be true, then the default should be printed. In this case though, every value of the array is run and at least one is found to be true so why is the default statement still being printed?
1 Answer
Min Choi
1,517 PointsNevermind, I figured it out. It printed the default since "DXB" lacked a case statement.