Thread overview
[bug?] Expressions within switch statements
Feb 02, 2004
C
Feb 02, 2004
Manfred Nowak
Feb 03, 2004
C
Feb 03, 2004
davepermen
Feb 03, 2004
J Anderson
Feb 06, 2004
Manfred Nowak
February 02, 2004
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 );

}


February 02, 2004
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.
February 03, 2004
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.


February 03, 2004
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.
>
>


February 03, 2004
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>
>  
>
I didn't  realize that.  This means no-fall-through cases are possible 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/
February 06, 2004
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.