Can the last case of a switch statement skip including the break?

Posted on:
tags: , , , , ,


Can the last case of a switch statement skip including the break?

Answer:

Even though the last case of a switch statement does not require a break statement at the end, you should add break statements to all cases of the switch statement, including the last case. You should do so primarily
because your program has a strong chance of being maintained by someone other than you who might add cases but neglect to notice that the last case has no break statement. This oversight would cause what would formerly be the last case statement to “fall through” to the new statements added to the bottom of the switch
statement. Putting a break after each case statement would prevent this possible mishap and make your program more “bulletproof.” Besides, most of today’s optimizing compilers will optimize out the last break, so there will be no performance degradation if you add it.

Cross Reference:


---------------------------------------------------------------------------------
Posted By Sundeep aka SunTechie

Sundeep is a Founder of Youth Talent Auzzar, a passionate blogger, a programmer, a developer, CISE and these days he is pursuing his graduation in Engineering with Computer Science dept.
Add Sundeep as a Friend on 


No comments:

Post a Comment

< >