Help Settle a Debate - C++ - "if"s nested in "switch"s
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
switch ( variable )
{
case 0:
...
break;
case 1:
...
break;
if ( boolean )
{
case 2:
...
break;
case 3:
...
break;
}
case 4:
...
break;
}
no, it isn't - though I can't point you to a specification.
Quote:
Originally Posted by cmfarley19
Note cases 2 & 3 are encapsulated within an if.
That's the point. While any code is allowed (though useless) between a break and the next case, the case keyword is only allowed in a switch scope. In your sample, however, there are two cases as children of an if statement.
switch is within "section-statement". Which can contain "labeled statement" and "section statement". "case" is "labeled statement". and "if" is "section statement".
Also, 6.4.2 (stmt.switch) states that:
Quote:
case and default labels in themselves do not alter the flow of control, which continues unimpeded
across such labels. To exit from a switch, see break, 6.6.1. [Note: usually, the substatement that is the
subject of a switch is compound and case and default labels appear on the top-level statements con-
tained within the (compound) substatement, but this is not required. Declarations can appear in the sub-
statement of a switch-statement. ]
Plus this compiles on g++/cl.exe:
Code:
int main(int argc, char** argv){
switch(argc){
case(0):
break;
if (true){
case(1):
break;
case(2):
break;
}
}
return 0;
}
So it should be a perfectly valid expression.
Get yourself a copy of C++ standard, and read it next time instead of asking.
Yes. The code compiles under gcc as well as Visual C++. That does not mean it does what is implied. The 'if' statement is completely ignored by both compilers. Lint flags the 'if' statement as 'unreachable code.'
SigTerm: Rather than post comments about your interpretation of the standard, why don't you test the results?
SigTerm: Rather than post comments about your interpretation of the standard, why don't you test the results?
Perhaps you missed "it compiles" part.
If block shouldn't be ignored, if there is no "break" before if. break terminates switch case.
Quote:
Originally Posted by cmfarley19
SigTerm - No need to be a douche.
I explained to YOU WHY this works, pointed you to relevant sections of standard, and this what I get?
Solve your problems yourself next time. There's plenty of documentation for everything - all at your fingertips, quickly available via any search engine.
"Yes. The code compiles under gcc as well as Visual C++." Hmm. Nope. I don't think I missed that part. Compilers typically implement switch statement using a jump table. Think of it as a list of goto labels. Since the 'if' is smack in the middle of the list of labels it is unreacheable. Given that Visual C++ and gcc both compile the code and produce results that illustrate that the 'if' statement has no influence on the enclosed cases, indicates that Microsoft and GNU have a different interpretation of the standard than you do, SigTerm.
Perhaps you did. Had you left out the last line I would have considered it a fine response.
Since you're registered since 2002 I suppose that you should be aware that "RTFM" is actually a useful advice and is not necessarily hostile.
Checking documentation takes minutes. Getting a meaningful reply from a thread - hours or days. So, yes, it makes sense to actually get a copy of C++ standard and read it when you're in trouble - if you value your time, that is.
Hey, if you think your if-in-a-switch is dubious, get a load of Duff's device. The note's worth reading, including "but it compiles and runs just fine". You're in good company.
Wow! That Duff code is scary stuff. I can see how it works. But it is the kind of thing that is likely to trip up the unwary pretty easily. Or even the wary for that matter. I think the reason it works is that there are no breaks following the cases. The labels are still reachable destinations for the switch jumps, but there is no coding hanging out in space following a break that is inaccessible.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.