Ranges in C++ Switch Case block
Today, we will do something really stupid! Yes, I am talking about the ranged switch case support that is available in C++. In all the commercial/hobby source code that I have seen, implemented and used, rarely have I come across the ranged switch case use. Most of the code when in need of ranges inside a switch case start using multiple case cascading or (the more niche programmers) resorting to nested if..else blocks!
So much so that when I told one of my co-workers that we can use ranges in C++ switch case, he started laughing (hysterically if I may add). Taken aback, I wanted to see if I have learned stuff wrong. So here it is, just to brush up your basic cpp knowledge and fall in love with it again! Below is the code in ideone ready for you to take for a spin.
Just to make it plain simple, you can use ranges in your cpp switch case block. And you can do it using ” … ” without the block quotes as shown in the code snippet above. This is quite useful when you don’t want to pollute your code using nested if..else blocks. Switch case is simple, readable and maintainable. Of course, don’t forget your “breaks” :).
Send in your comments & Suggestions. Follow me on Twitter @wolverine2k and share the love by clicking the buttons below and posting this on your social networks.