Topic: complex switch case (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=27696" title="Pages that link to Topic: complex switch case (Page 1 of 1)" rel="nofollow" >Topic: complex switch case <span class="small">(Page 1 of 1)</span>\

 
Sirius
Obsessive-Compulsive (I) Inmate

From: Edmonton
Insane since: Feb 2003

IP logged posted posted 03-27-2006 05:42 Edit Quote

hi all,

I'd like ot know if I can put interval as in ruby or multiple label in a case.

I want to give a value to a variable after comparison with conditions. I know I can do it with the If() statement but it seems to me that it would be better to use a switch() statement.

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;
}



I've tried both of those example with multiple version but it just don't work...or maybe that switch() cannot take more than one label... and that isn't written anywhere...

cheers again,

Patrick

Alevice
Paranoid (IV) Inmate

From: Mexico
Insane since: Dec 2002

IP logged posted posted 03-27-2006 06:00 Edit Quote

Tried already?:

code:
switch(var)
{
    case 1:
        doBefore2(); //only if needed
    case 2:
        do1and2();
        break;
    case 3:
        do3();
        break;
}



If it wasn't clear what changed, is just not putting a break statement for a mutilple cases. This is the way it works in C/C++ at least.

__________________________________


Sexy Demoness cel

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

IP logged posted posted 03-27-2006 06:51 Edit Quote

If you need multiple conditions, I suggest keeping the cases together.

code:
switch(var)
{
    case 1:
    case 2:  
        do1and2();
        break;
    case 3:
        do3();
        break;
}


otherwise you'll never see this when it's a couple hundred lines somebody left between the case and the break...
And I've been bitten by this quite hard. It took C# to finally get it right (compiler simple requires a break, I believe,
except in the case 1: case 2: case.)

So long,

->Tyberius Prime

Sirius
Obsessive-Compulsive (I) Inmate

From: Edmonton
Insane since: Feb 2003

IP logged posted posted 03-28-2006 04:24 Edit Quote

Interesting example. I did not know that we can keep the cases together.

My range of number is between 3 and 18. Grouped by two.

Though a need a break in every case because they have different action that I don't want to apply to other case.

I've think about this too:

code:
switch (var)
{
     case < 4:
      do action;
      break;
     case <6:
        do action2;
        break;
   ....
//and so on until 18.

}



I've think of it before but forgot to try it...I'm doing it right now!

What do you think?

Patrick

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

IP logged posted posted 03-28-2006 07:01 Edit Quote

And you're sure polymorphism isn't the answer you really are looking for?

Sirius
Obsessive-Compulsive (I) Inmate

From: Edmonton
Insane since: Feb 2003

IP logged posted posted 03-29-2006 04:13 Edit Quote

Polymorphism?!? hum...is it a kind of coktail??


I don't know polymorphism...I'll check on the net...but can you explain what you mean anyway?

Thanks,


Patrick

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

IP logged posted posted 03-29-2006 06:51 Edit Quote

Polymorphism basically means 'having different objects and treating them the same'...

so no matter wether you have a bike, a car or a truck, all you say is
$aVehicle->MoveTo ( $aLocation ) ;
and it's the object's responsibility to handle the actual details.

Whenever you have a switch statement twice, especially if you want to 'combine some, but still to seperat things depending on what it is'
you're a pretty good canidate for an interface plus maybe an class inheritance strategy.

Why in the world do you switch on weird numbers anyhow? Or is that just your example?

lallous
Maniac (V) Inmate

From: Lebanon
Insane since: May 2001

IP logged posted posted 04-02-2006 07:49 Edit Quote

Hello Sirius,

If you got lots of conditions and ranges, why not write some sort of logic table coupled with case statement?

say you want to do this on these conditions:
range 1..6 : action1()
range 10..44: action2()
range 88..100: action3()

etc...now define the range table as:

var rangeTable = [1,6, 0, // 1..6 -> 0
10,44,1 // 10..44 -> 1
88,100,2 ];

Then write a small for loop that decides in what range does your value exist.
Now suppose 'val' is your value you want to decide upon, here's the code:

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;
}



HTH

--
Regards,
Elias

liorean
Bipolar (III) Inmate

From: Umeå, Sweden
Insane since: Sep 2004

IP logged posted posted 04-04-2006 16:30 Edit Quote

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.

--
var Liorean = {
abode: "http://codingforums.com/",
profile: "http://codingforums.com/member.php?u=5798"};

divinechaos
Nervous Wreck (II) Inmate

From:
Insane since: Dec 2001

IP logged posted posted 04-05-2006 07:43 Edit Quote

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:

Tyberius Prime said:

Polymorphism basically means 'having different objects and treating them the same'...so no matter wether you have a bike, a car or a truck, all you say is $aVehicle->MoveTo ( $aLocation ) ;and it's the object's responsibility to handle the actual details.Whenever you have a switch statement twice, especially if you want to 'combine some, but still to seperat things depending on what it is'you're a pretty good canidate for an interface plus maybe an class inheritance strategy.Why in the world do you switch on weird numbers anyhow? Or is that just your example?

divinechaos
Nervous Wreck (II) Inmate

From:
Insane since: Dec 2001

IP logged posted posted 04-05-2006 16:17 Edit Quote

Edit: double post. It's too early.

(Edited by divinechaos on 04-05-2006 16:19)

divinechaos
Nervous Wreck (II) Inmate

From:
Insane since: Dec 2001

IP logged posted posted 04-05-2006 16:17 Edit Quote

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.
Cheers,
DC

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

IP logged posted posted 04-05-2006 20:27 Edit Quote

An interface can mean two things:
a) an explicitly defined Interface ala java - which enforces your classes to conform to that interface.
b) Just the visible members of any class, by name. So I can say $objectOfTypeA->DoFunkyStuff() and $objectOfTypeB->DoFunkyStuff(),
because they 'just happen' to both have DoFunkyStuff Methods. Of course that's not enforced at all, and you won't know that the code will
run before running it. But that's the way PHP does it (but php5 does have interfaces, I believe, but I didn't check), and it's way better than not being able to do this at all just to satisfy the compiler if ( objectIsTypeA) { (objectTypeA)someObject->DoFunkyStuff()} else { (objectTypeB)someObject->DoFunkyStuff()} to have a C++ like example...

so long,

->Tyberius Prime

Hugh
Paranoid (IV) Inmate

From: Dublin, Ireland
Insane since: Jul 2000

IP logged posted posted 04-06-2006 06:40 Edit Quote

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

Sirius
Obsessive-Compulsive (I) Inmate

From: Edmonton
Insane since: Feb 2003

IP logged posted posted 04-06-2006 07:00 Edit Quote
quote:

liorean said:

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&gt;4) dootherthing(); break;}Though there is absolutely NO benefit to using that structure as compared to using multiple if-statements.-- var Liorean = { abode: "http://codingforums.com/", profile: "http://codingforums.com/member.php?u=5798"};




Yeah, for sure... I'm back to the if statements...

liorean
Bipolar (III) Inmate

From: Umeå, Sweden
Insane since: Sep 2004

IP logged posted posted 04-06-2006 13:24 Edit Quote

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.

It can be seen as a type mechanism that can span multiple different class hierarchies that except for implementing the interface have nothing in common.

Your second alternative sounds more like "feature signature" in typical jargon to me.



Hugh: JavaScript has a continue statement, yeah. It might be more limited than you think however, as continue may only occur in loop constructs, and means that the rest of the loop body should be jumped over this iteration, going directly to the next loop iteration.

--
var Liorean = {
abode: "http://liorean.web-graphics.com/",
profile: "http://codingforums.com/member.php?u=5798"};



Post Reply
 
Your User Name:
Your Password:
Login Options: Remember Me On This Computer
 
Your Text:
Loading...
Options: Show Signature
Enable Slimies
Enable Linkwords

« BackwardsOnwards »

Show Forum Drop Down Menu