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

Riley Spencer
Riley Spencer
1,753 Points

Why Cases And Defaults?

Why do you have to use the two words case and default? Are they of any significance and can't you just use the while and while if? An answer would be greatly appreciated.

1 Answer

Hi Riley,

The switch statement is more like a large if statement than a while or whileif. Those are loops which iterate through the same block of code while a statement remains true.

The switch statement filters out particular outcomes or specified ranges of outcomes and executes code specific to that condition. As a best practice, it is wise to include a default statement so you can stipulate what code runs should none of the conditions be met. Granted, there are amny scenarios where the default can never be reached because the set of conditions are closed but the code likes the default to be defined anyway.

There was some discussion on the point in this post - that may help a little too.

I hope this helps a bit.