Topic: complex switch case (Page 1 of 1) |
|
|---|---|
|
Obsessive-Compulsive (I) Inmate From: Edmonton |
posted 03-27-2006 05:42
hi all, code: //something like =>
var variable = something;
switch (variable)
{
//interval in ruby
case 1 .. 5:
do something;
break;
//multiple label
case variable ==2 || variable ==3:
do somtehing;
break;
}
|
|
Paranoid (IV) Inmate From: Mexico |
posted 03-27-2006 06:00
Tried already?: code: switch(var)
{
case 1:
doBefore2(); //only if needed
case 2:
do1and2();
break;
case 3:
do3();
break;
}
|
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 03-27-2006 06:51
If you need multiple conditions, I suggest keeping the cases together. code: switch(var)
{
case 1:
case 2:
do1and2();
break;
case 3:
do3();
break;
}
|
|
Obsessive-Compulsive (I) Inmate From: Edmonton |
posted 03-28-2006 04:24
Interesting example. I did not know that we can keep the cases together. code: switch (var)
{
case < 4:
do action;
break;
case <6:
do action2;
break;
....
//and so on until 18.
}
|
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 03-28-2006 07:01
And you're sure polymorphism isn't the answer you really are looking for? |
|
Obsessive-Compulsive (I) Inmate From: Edmonton |
posted 03-29-2006 04:13
Polymorphism?!? |
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 03-29-2006 06:51
Polymorphism basically means 'having different objects and treating them the same'... |
|
Maniac (V) Inmate From: Lebanon |
posted 04-02-2006 07:49
Hello Sirius, code: var rangeTable = [1,6, 0, // 1..6 -> 0
10,44,1, // 10..44 -> 1
88,100,2 ];
var val = 88;
var decision=-1;
for (var i=0;i<rangeTable.length;i+=3)
{
if ((val >= rangeTable[i]) && (val <= rangeTable[i+1]))
{
decision = rangeTable[i+2];
break;
}
}
switch (decision)
{
case 0: // for range 1..6
break;
case 1: // 10..44
break;
case 2: // 88..100
break;
}
|
|
Bipolar (III) Inmate From: Umeå, Sweden |
posted 04-04-2006 16:30
Well, there's this structure: code: switch(true){
case (a==1||a==2):
dosomething();
break;
case (a==2||a==3):
dootherthing();
break;
case (a>3)
dosomething();
if(a>4)
dootherthing();
break;
}Though there is absolutely NO benefit to using that structure as compared to using multiple if-statements. |
|
Nervous Wreck (II) Inmate From: |
posted 04-05-2006 07:43
TP, does PHP actually have actual interfaces? I thought the PHP5 approach was, "well, we gave you class inheritance... shut up and be happy". quote: |
|
Nervous Wreck (II) Inmate From: |
posted 04-05-2006 16:17
Edit: double post. It's too early. |
|
Nervous Wreck (II) Inmate From: |
posted 04-05-2006 16:17
Why did I think Sirius was talking about PHP? I don't know. The switch is obviously JS, and it's in DHTML/Client Side... must have been the Ruby reference. |
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 04-05-2006 20:27
An interface can mean two things: |
|
Paranoid (IV) Inmate From: Dublin, Ireland |
posted 04-06-2006 06:40
Doesnt javascript have continue; like break ? It helps reading code when writing if your know what I mean, its easier to see a 'continue' than it is to see the lack of a 'break'. just my 2c :P |
|
Obsessive-Compulsive (I) Inmate From: Edmonton |
posted 04-06-2006 07:00
quote:
|
|
Bipolar (III) Inmate From: Umeå, Sweden |
posted 04-06-2006 13:24
Tiberius Prime: By "Interface" the typical object oriented jargon means a set of feature signatures brought together as one named unit. Stating in your code that a class implements an interface - or an object, depending on how the language does these types of things - means a form of contract; a guarantee that a class - or object - implements that set of signatures. |