2 registered members (AndrewAMD, dr_panther),
1,275
guests, and 7
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
switch () with C-Script
#235306
11/06/08 22:13
11/06/08 22:13
|
Joined: Jul 2002
Posts: 2,002 Europe
ShoreVietam
OP
Expert
|
OP
Expert
Joined: Jul 2002
Posts: 2,002
Europe
|
Good evening beloved developers, for those still using C-Script (like me) I found a way faking switch () by using the while () instuction. Maybe it looks a bit strange in the beginning, but it aviods those if () structures that look like chinese temples.
while (1) {
// Case 1
if (expression) {
instructions
break;
}
// Case 2
if (expression) {
instructions
break;
}
// And so on...
// Default part
instructions
break;
}
Last edited by ShoreVietam; 11/06/08 22:14.
|
|
|
Re: switch () with C-Script
[Re: ShoreVietam]
#235339
11/07/08 03:35
11/07/08 03:35
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
Why do you put that in a loop? The loop does not make sense at all as it is never executed.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: switch () with C-Script
[Re: Lion_Ts]
#235435
11/07/08 16:35
11/07/08 16:35
|
Joined: Jul 2002
Posts: 2,002 Europe
ShoreVietam
OP
Expert
|
OP
Expert
Joined: Jul 2002
Posts: 2,002
Europe
|
@Lion_Ts A6.31.4 C-Script (and earlyer) does understand if-structures without {} (having only one following command, which can be another if, to create what you posted). Your if structure in C-Script looks like this:
if (expression) {
} else { if (expression) {
} else { if (expression) {
} else {
} } }
And is aweful to count the amount of closing-brackets. So why the while? To be able to jump to the end of the fake-switch by using break;.
Last edited by ShoreVietam; 11/07/08 16:36.
|
|
|
Re: switch () with C-Script
[Re: ShoreVietam]
#235481
11/07/08 21:35
11/07/08 21:35
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
if else if else does actually not represent the logic behind a switch instruction. In a switch instruction you have multiple possibilities and a default case. If you want to rebuild switch you can do it the closest way like this: if (condition1){
doSomething()1;
}
if (condition2){
doSomething()2;
}
doSomethingDefault(); So there is no need (and even no sense) in using else branches and counting brackets. The breaks of the switch instruction can be easily simulated by goto. I wouldn't recomment using loops and breaks for this case as a loop suggests other readers of your code that you want to do somehting totally different.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: switch () with C-Script
[Re: FBL]
#235489
11/07/08 22:46
11/07/08 22:46
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
It's ok as long as you get the control over it. I personally wouldn't use gotos too. But I'd prefer a goto statement to a semi-switch-like construction that requires a loop that is never looped. ;-)
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: switch () with C-Script
[Re: Uhrwerk]
#235502
11/08/08 00:11
11/08/08 00:11
|
Joined: Oct 2004
Posts: 1,655
testDummy
Serious User
|
Serious User
Joined: Oct 2004
Posts: 1,655
|
|
|
|
|