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

Android Implementing Designs for Android Customizing the Action Bar Adding Icons to Tabs

Shariq Shaikh
Shariq Shaikh
13,945 Points

Break keyword for switch statements?

Should there be a break keyword after each case for both switch statements?

3 Answers

william parrish
william parrish
13,774 Points

Yes, it is important to provide a break statement for each case you provide to the switch statement, and is usually good practice to ensure you provide a default as well, as a catch-all in case none of your case's are addressed.

Shariq Shaikh
Shariq Shaikh
13,945 Points

So did Ben just forget? It's not a big deal but he did do this in the previous course in which he eventually corrected his mistake.

Ruggiero A
Ruggiero A
8,534 Points

Ben didn't forget. Break is an important statement because without it would "invade" all the other case conditions. Anyway breaks not only were not important in that case, but were totally uses. Why? Because there was a return statement, which is much stronger than a break since it closes basically the function's runtime in that line. Basically it's like "enough here, return this value and do nothing more"

Vrezh Gulyan
Vrezh Gulyan
11,161 Points

Im sure it occurred to him that break statements are usually used when using a switch statement and if you have been following the course you'll remember a time where because break statements weren't included we were getting an issue when we'd click to choose recipients. In this case however break statements are unnecessary since once a condition is met we return the value and no other lines of code will be executed regardless.

Jacob Bergdahl
Jacob Bergdahl
29,118 Points

Yes, they are important, but Ben did not forget them in this case! There's a return statement in each case. No code is read after a return statement, and thus, no break is necessary.