D - [bug?] Expressions within switch statements
- C (22/22) Feb 02 2004 Should this be allowed ?
- Manfred Nowak (12/17) Feb 02 2004 [...]
- C (18/35) Feb 02 2004 Hmm ? I was refering to arbitrary code within the switch statement, not
- davepermen (5/45) Feb 03 2004 switch is just a shortcut for a lot of goto's to the different case-labe...
- J Anderson (18/23) Feb 03 2004 I didn't realize that. This means no-fall-through cases are possible
- Manfred Nowak (8/9) Feb 05 2004 It is correct code. If you look at the docs then you will see, that
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
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
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
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
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/
Feb 03 2004
C wrote:Its not correct codeIt 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