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!

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

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.