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...
Tyberius Prime
Maniac (V) Mad Scientist with Finglongers
From: Germany Insane since: Sep 2001
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;
}
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.)
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
posted 03-29-2006 06:51
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?
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;
}
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?
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
posted 04-05-2006 20:27
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...
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
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"};
Yeah, for sure... I'm back to the if statements...
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.