www.digitalmars.com         C & C++   DMDScript  

D - [bug?] Expressions within switch statements

reply "C" <dont respond.com> writes:
Should this be allowed ?
--

import std.c.stdio;

void main ()
{

 const int option = 3;
 int assignAnInt = 0;

 switch (  option )
 {

  if (  true )
  {
   puts( "never executed" ) ;
  }

  assignAnInt = 21;

  case 3:
  puts( "3" );
  break;
  default:
  break;


 }

 printf( "%d",assignAnInt );

}
Feb 02 2004
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
C wrote:

 Should this be allowed ?

  switch (  option )
  {
   assignAnInt = 21;
   case 3:

Yes. It is the same as with: void main(){ if( true ) printf("Not executed.\n"); else printf("Executed.\n"); } So long.
Feb 02 2004
parent reply "C" <dont respond.com> writes:
Hmm ?  I was refering to arbitrary code within the switch statement, not
belonging to a case .  Its not correct code I thought the compiler would
complain , however that also seems to be legal in C++.

#include <cstdio>
#include <cstdlib>

int main () {
 int option = 4;

 switch ( option ) {
  if ( option == 4 ) exit(0);

 case 4: puts("here");
 }

 return 1;
}

outputs : here

shrug,
C
"Manfred Nowak" <svv1999 hotmail.com> wrote in message
news:bvlf1b$m16$1 digitaldaemon.com...
 C wrote:

 Should this be allowed ?

  switch (  option )
  {
   assignAnInt = 21;
   case 3:

Yes. It is the same as with: void main(){ if( true ) printf("Not executed.\n"); else printf("Executed.\n"); } So long.

Feb 02 2004
next sibling parent "davepermen" <davepermen hotmail.com> writes:
switch is just a shortcut for a lot of goto's to the different case-labels.
so the behaviour is entierly understandable. it jumps over the rest to the
first case that fits..

"C" <dont respond.com> schrieb im Newsbeitrag
news:bvmvfq$58e$1 digitaldaemon.com...
 Hmm ?  I was refering to arbitrary code within the switch statement, not
 belonging to a case .  Its not correct code I thought the compiler would
 complain , however that also seems to be legal in C++.

 #include <cstdio>
 #include <cstdlib>

 int main () {
  int option = 4;

  switch ( option ) {
   if ( option == 4 ) exit(0);

  case 4: puts("here");
  }

  return 1;
 }

 outputs : here

 shrug,
 C
 "Manfred Nowak" <svv1999 hotmail.com> wrote in message
 news:bvlf1b$m16$1 digitaldaemon.com...
 C wrote:

 Should this be allowed ?

  switch (  option )
  {
   assignAnInt = 21;
   case 3:

Yes. It is the same as with: void main(){ if( true ) printf("Not executed.\n"); else printf("Executed.\n"); } So long.


Feb 03 2004
prev sibling next sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
C wrote:

Hmm ?  I was refering to arbitrary code within the switch statement, not
belonging to a case .  Its not correct code I thought the compiler would
complain , however that also seems to be legal in C++.
<snip>
  

in C++ (and C) ie #define when(T) break;case T: int main(int argc, char* argv[]) { int X = 5; switch (5) { when(5) printf("5\n"); when(1) printf("1\n"); } return 0; } -- -Anderson: http://badmama.com.au/~anderson/
Feb 03 2004
prev sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
C wrote:

 Its not correct code

It is correct code. If you look at the docs then you will see, that switchstatement derives to blockstatement and not something like casesequence. So consecutive breakstatements are also correct. Also code between two breakstatements without a casestatement. And code after the last breakstatement also. So long.
Feb 05 2004