Share this page with someone:
C++ Case/Switch Statement Warning
If you are a programmer of any kind, you are most likely familiar with the Case/Switch statement. This is simply a way in programming for you to do different things depending on the value of a variable. For example..if your working with a variable named abc. You might want to do different things depending on what the value is. The long way would be to put down a long if/else statement to deal with each potential value type. The easy way is to use a simple switch/case statement. No matter what language you are using, this will come into play at some point.
If you are using PHP a switch/case statement might look like follows:
<?php $variable = 'test'; switch($variable) { case 'test': // do something break; case 'test2': // do something break; case'test3'; // do something break; } ?>
That is very simple. It's quite similar to JavaScript, but with a little different syntax. Now here is the tricky part. I don't know every language out there. However, quite a few of them have the same type of structure of Switch/Case statements. Then suddenly C++ throws you a loophole. In working with C++ it's just about the same as PHP. Except for one strange thing. If you initialize a variable inside of a switch/case statement you have to use brackets around each interior case when you are initializing a variable. Just something tricky that I had to figure out with a lot of trial and error.
- Add new comment
- 217 reads