LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-15-2011, 08:34 AM   #1
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
Help Settle a Debate - C++ - "if"s nested in "switch"s


Is this legal:
Code:
switch ( variable )
{
case 0:
    ...
    break;
case 1:
    ...
   break;

if ( boolean )
{
   case 2:
      ...
      break;
   case 3:
      ...
      break;
}

case 4:
   ...
   break;
}
Note cases 2 & 3 are encapsulated within an if.

Thoughts or opinions?

We can not find documentation or references to support either side so if you know of any please let me know.

Cheers,

Chris
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-15-2011, 08:45 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
It compiles and runs, so I think it's legal.
 
Old 12-15-2011, 08:50 AM   #3
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by cmfarley19 View Post
Is this legal:
Code:
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 View Post
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.

[X] Doc CPU
 
2 members found this post helpful.
Old 12-15-2011, 08:50 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
AFAIK, "variable" can't be a string or a floating point.
 
Old 12-15-2011, 08:55 AM   #5
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by Anisha Kaul View Post
AFAIK, "variable" can't be a string or a floating point.
true, but this has nothing to do with the issue of nested scopes or cross-scope control structures.

[X] Doc CPU
 
Old 12-15-2011, 08:58 AM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by Doc CPU View Post
true, but this has nothing to do with the issue of nested scopes or cross-scope control structures.
Yeah, I had read the question incompletely. ;-)
 
Old 12-15-2011, 09:13 AM   #7
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
2003 standard does not say it is invalid.

According to grammar (Annex A.5, ISO/IEC 14882:2003):
Code:
statement:
        labeled-statement
        expression-statement
        compound-statement
        selection-statement
        iteration-statement
        jump-statement
        declaration-statement
        try-block

labeled-statement:
        identifier : statement
        case constant-expression : statement
        default : statement

expression-statement:
        expression(opt);

compound-statement:
        { statement-seq(opt)}

statement-seq:
        statement
        statement-seq statement

selection-statement:
        if ( condition ) statement
        if ( condition ) statement else statement
        switch ( condition ) statement

condition:
        expression
        type-specifier-seq declarator = assignment-expression
it should be a valid expression.

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.

Last edited by SigTerm; 12-15-2011 at 09:14 AM.
 
Old 12-15-2011, 09:35 AM   #8
dcf
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Rep: Reputation: Disabled
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?
 
1 members found this post helpful.
Old 12-15-2011, 09:39 AM   #9
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
So trying this code out in various compilers produces the same results.
Code:
int main(int argc, char* argv[])
{
 printf("Start\r\n");
 int idx;
 int full_switch=0;

 for (idx = 0; idx < 3; idx++)
 {
  switch (idx)
  {
  case 0:
   printf("case 0\r\n");
   break;
  if ( full_switch )
  {
  case 1:
   printf("case 1\r\n");
   break;
  }
  case 2:
   printf("case 2\r\n");
   break;
  }
 }

 full_switch = 1;

 for (idx = 0; idx < 3; idx++)
 {
  switch (idx)
  {
  case 0:
   printf("case 0\r\n");
   break;
  if ( full_switch )
  {
  case 1:
   printf("case 1\r\n");
   break;
  }
  case 2:
   printf("case 2\r\n");
   break;
  }
 }

 printf("End\r\n");
 return 0;
}
No compiler errors or warnings and produces the following results:
Code:
cmfarley@wombat2:~$ ./switch 
Start
case 0
case 1
case 2
case 0
case 1
case 2
End
dcf - I had not gone down the Lint route, but my testing showed the same. The if statement is ignored. Compiler optimization apparently.

SigTerm - No need to be a douche.
 
1 members found this post helpful.
Old 12-15-2011, 09:56 AM   #10
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Thumbs down

Quote:
Originally Posted by dcf View Post
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 View Post
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.
 
Old 12-15-2011, 10:01 AM   #11
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
Quote:
I explained to YOU WHY this works, pointed you to relevant sections of standard, and this what I get?
Perhaps you did. Had you left out the last line I would have considered it a fine response.

Last edited by cmfarley19; 12-15-2011 at 10:02 AM.
 
Old 12-15-2011, 10:04 AM   #12
dcf
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Rep: Reputation: Disabled
"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.
 
Old 12-15-2011, 10:06 AM   #13
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by cmfarley19 View Post
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.

Well, whatever. I'm outta here.
 
Old 12-15-2011, 10:19 AM   #14
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 211

Rep: Reputation: 67
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.
 
3 members found this post helpful.
Old 12-15-2011, 10:31 AM   #15
dcf
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Make "ls" command show colors without "--color=always" switch SharpyWarpy Linux - General 4 07-16-2007 11:00 PM
C Drive "/dev/hda1" accidently used in dd command at "of=" switch M D Linux - Software 4 04-26-2007 08:56 AM
"NIM thread blocked" & "Deadman Switch (DMS) close to trigger" errors sosborne Linux - Networking 1 06-28-2006 02:07 PM
"NIM thread blocked" & "Deadman Switch" errors sosborne AIX 3 06-01-2006 11:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration