View Full Version : Numeric Comparison in switch statement
Raabi
October 12th, 2009, 08:55 AM
Hello geeks!
First time working with numeric comparison, I found myself in dark, as below:
switch (_hours)
{
case _hours = 0:
_hours = 12;
break;
case _hours < 12:
_hours = hours;
break;
case _hours = 12:
_hours = 12;
break;
case _hours > 12:
_hours = (hours - 12);
break;
}
But, the code is not functioning. I don't want to use if .. else construct.
Would anybody kindly straighten this syntax, please! Any explaination, if necessary, would be highly appreciated.
Iamthejuggler
October 12th, 2009, 09:05 AM
I don't think it's possible to have conditionals in a switch statement. I've certainly never seen it done.
Raabi
October 12th, 2009, 09:27 AM
I believe, I have seen numeric comparison in switch statement sometime back. Unofortunately, I don't remember the exact syntax, used there.
I am sure, some AS3 guru will show up with a solution.
Jeff Wheeler
October 12th, 2009, 09:40 AM
I'm pretty sure that's not possible, unfortunately. :-/
brndn
October 12th, 2009, 09:51 AM
http://www.kirupa.com/forum/showthread.php?t=335188&highlight=switch
Jeff Wheeler
October 12th, 2009, 10:08 AM
That's disgusting, but I guess it works. :lol:
Raabi
October 12th, 2009, 11:16 AM
Thanks, brndn, for the reference. I tried it and it works.
Have a good time.
brndn
October 12th, 2009, 11:48 AM
That's disgusting, but I guess it works. :lol:
truely revolting i must say, but it does the trick.
efos
October 12th, 2009, 12:28 PM
That's disgusting, but I guess it works. :lol:
That's what I does. :)
Switch statements compare your argument to each case.
Each case statement is resolved from top to bottom as: if (n == case) {}
If it resolves true, all remaining code is executed; omitting the case lines, and stopping at breaks or returns.
var i:int = 4
switch(true) {
case (i>5):
trace(i+" is greater than 5")
case (i>4):
trace(i+" is greater than 4")
case (i>3):
trace(i+" is greater than 3")
case (i>2):
trace(i+" is greater than 2")
break
case (i>1):
trace(i+" is greater than 1")
}To the compiler, above will read as follows:
var i:int = 4
switch(true) {
case (i>5):
trace(i+" is greater than 5")
case (i>4):
trace(i+" is greater than 4")
case (i>3):
trace(i+" is greater than 3")
case (i>2):
trace(i+" is greater than 2")
break
case (i>1):
trace(i+" is greater than 1")
}
Switch can be abused to do just about anything a series of if...else statements can do; and has the bonus ability to execute multiple conditions with less code.
Jeff Wheeler
October 12th, 2009, 03:16 PM
Yeah, I understood how it worked, I just . . . uh, didn't imagine anybody would ever actually write something so gross. :P
Krilnon
October 12th, 2009, 05:56 PM
For what it's worth, there is syntactic sugar for doing this in other languages like Ruby:
i = 5
case
when i < 7
puts '< 7'
when i == 4
puts '== 4'
else
puts 'nil'
end
# < 7
It just so happens that nobody put a similarly intuitive syntax into AS3.
Scythe
October 12th, 2009, 06:26 PM
Exhibit A:
switch (true)
{
case _hours == 0:
_hours = 12;
break;
case _hours < 12:
_hours = hours;
break;
case _hours == 12:
_hours = 12;
break;
case _hours > 12:
_hours = (hours - 12);
break;
}
Exhibit B:
if (_hours == 0)
_hours = 12;
else if (_hours < 12)
_hours = hours;
else if (_hours == 12)
_hours = 12;
else if (_hours > 12)
_hours = (hours - 12);
Not hard to see which is shorter. And exhibit B can be shortened even further to
if (_hours == 0)
_hours = 12;
else if (_hours < 12)
_hours = hours;
if (_hours > 12)
_hours = (hours - 12);
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.