I don't see why you think:
{
case 1:
indented code
more indented code
break:
case 2:
code code code
case 3:
code
}
is hard to read.
Edit:
Of course, if the forum code destroys my spacing, it's hard to read.
[code]
switch (day)
{
case 1 : cout << "Sunday";
break;
case 2 : cout << "Monday";
break;
case 3 : cout << "Tuesday";
break;
case 4 : cout << "Wednesday";
break;
case 5 : cout << "Thursday";
break;
case 6 : cout << "Friday";
break;
case 7 : cout << "Saturday";
break;
default : cout << "Not an allowable day number";
break;
}
What is better: Switch statements or If...Else If statements? And why?
[FONT=Courier New,Lucida Console,Courier,Monaco,Monospace,monospace][SIZE=-1]EVALUATE TRUE
WHEN WS-X = 1 AND WS-Y = 2
PERFORM X-100-PROCESS1
WHEN WS-X =1 AND WS-Y NOT = 2
PERFORM X-200-PROCESS2
END-EVALUATE.[/SIZE][/FONT] [FONT=Verdana,Tahoma,Arial,Helvetica,Sans-serif,sans-serif][SIZE=-1]
Here, the whole condition on the WHEN statement is checked and if it is TRUE then the associated statement(s) are executed.
The second way to do this is using EVALUATE ... ALSO.
[/SIZE][/FONT][FONT=Courier New,Lucida Console,Courier,Monaco,Monospace,monospace][SIZE=-1]EVALUATE WS-AGE ALSO WS-SEX ALSO WS-WEIGHT
WHEN 21 ALSO 'M' ALSO 150
PERFORM A-200-ACCEPT
WHEN OTHER
PERFORM A-300-DECLINE
END-EVALUATE.
[/SIZE][/FONT]
[FONT=Courier New,Lucida Console,Courier,Monaco,Monospace,monospace][SIZE=-1]
I don't see why you think:
{
case 1:
indented code
more indented code
break:
case 2:
code code code
case 3:
code
}
is hard to read.
Edit:
Of course, if the forum code destroys my spacing, it's hard to read.
switch (...) {
case 1:
indented code
more indented code
break:
case 2:
code code code
// Fall through to next case.
case 3:
code
}
switch (machine_condition) {
case M_OK:
TurnGreenLightOn();
break;
case M_OFF:
TurnRedLightOn ();
break;
case M_ERROR:
case M_BADHEALTH:
case M_NORESPONSE:
TurnYellowLightOn ();
break;
}
switch (machine_condition) {
case M_OK:
TurnGreenLightOn();
case M_OFF:
TurnRedLightOn ();
case M_ERROR:
case M_BADHEALTH:
case M_NORESPONSE:
TurnYellowLightOn ();
}
Instead of multiple cases, just extend the syntax of the case clause:
case-clause: CASE {range-list | OTHER} :
range-list: {expression [.. expression]} [latex]$\bowtie$[/latex] ,
Ada uses 1..7hmmm, probably need some way of marking the continuous ranges so they don't look like subtractions.
Yes, the syntax allows a range to be indicated with the punctuation "..", like Ada. It also allows multiple ranges separated by commas (the bowtie indicates that the preceding syntax can be repeated using the succeeding punctuation).Kevin said:This is what I like, as long as range lists can be strings, continuous ranges, or discontinuous number ranges.
Speaking C (and C++), I think it is clear:
The break; in switches has a very definite use: You may not want to leave the switch after the first match, you may want to execute the rest of the statements in the switch. This, of course, is a multi-entry, and thus dirty coding, still.....
In any case, why don't you make two tiny programs and compare the disassembled executables ?
If we're talking about C, my guess is they would be identical.
*** EXECUTABLE CODE (.text) ***
Label Opcode Operands Comment
0x00000000 _Z6Switchl:
0x00000000 0x2800 cmp r0,#0
0x00000002 0xD004 beq *+12 ; 0x0000000e
0x00000004 0x2801 cmp r0,#1
0x00000006 0xD004 beq *+12 ; 0x00000012
0x00000008 0x2802 cmp r0,#2
0x0000000A 0xD004 beq *+12 ; 0x00000016
0x0000000C 0xE005 b *+14 ; 0x0000001a
0x0000000E 0x2001 mov r0,#1
0x00000010 0x4770 bx lr
0x00000012 0x2002 mov r0,#2
0x00000014 0x4770 bx lr
0x00000016 0x2004 mov r0,#4
0x00000018 0x4770 bx lr
0x0000001A 0x2000 mov r0,#0
0x0000001C 0x4770 bx lr
*** EXECUTABLE CODE (.text) ***
Label Opcode Operands Comment
0x00000000 _Z2Ifl:
0x00000000 0x2800 cmp r0,#0
0x00000002 0xD101 bne *+6 ; 0x00000008
0x00000004 0x2001 mov r0,#1
0x00000006 0x4770 bx lr
0x00000008 0x2801 cmp r0,#1
0x0000000A 0xD101 bne *+6 ; 0x00000010
0x0000000C 0x2002 mov r0,#2
0x0000000E 0x4770 bx lr
0x00000010 0x2802 cmp r0,#2
0x00000012 0xD101 bne *+6 ; 0x00000018
0x00000014 0x2004 mov r0,#4
0x00000016 0x4770 bx lr
0x00000018 0x2000 mov r0,#0
0x0000001A 0x4770 bx lr