Raised This Month: $51 Target: $400
 12% 

if -> switch


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Kowalsky
Senior Member
Join Date: Mar 2015
Location: Poland
Old 11-08-2016 , 06:46   if -> switch
Reply With Quote #1

how can I optimize the code?

Code:
if( a > 90 ) ... else if( a<=90 && a>80 ) ... else if( a<=80 && a > 70 )
etc..

How can I convert it to switch?
__________________
I ONLY LOVE STEAM, NON-STEAM IS VERY BAD FOR YOUR HEALTH!
Kowalsky is offline
indraraj striker
Veteran Member
Join Date: Mar 2014
Location: Under the water
Old 11-08-2016 , 07:48   Re: if -> switch
Reply With Quote #2

PHP Code:

switch (a)
{
    case 
.. 30:
    {
       
//This code will run if 0 <= a <= 30
    
}
 
    case 
31 .. 60:
    {
       
//This code will run if 31 <= a <= 60
    
}
 
    case 
80 .. 90:
    {
       
//This code will run if 80 <= a <= 90
    
}
 
    default: 
    {
       
//This code will run if all other cases fail 
       //when a > 90
    
}

__________________
Thanks everyone. #miss_you_all

Last edited by indraraj striker; 11-08-2016 at 07:49.
indraraj striker is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 11-08-2016 , 08:14   Re: if -> switch
Reply With Quote #3

Better to keep it as an if/else statement than using ranges.
PartialCloning is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 11-08-2016 , 08:28   Re: if -> switch
Reply With Quote #4

Quote:
Originally Posted by PartialCloning View Post
Better to keep it as an if/else statement than using ranges.
Can you explain why you think this? Just curious
__________________
Bugsy is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 11-08-2016 , 08:39   Re: if -> switch
Reply With Quote #5

I read ranges were removed from sourcemod in switch statements because "0 .. 30" would be turned into "if(a == 0 || a == 1 || a == 2 || ... a == 30)" by the compiler, rather than "if(0 <= a <= 30)".

Maybe only the sourcemod compiler does that, I'm not 100% sure.

Edit:

Quote:
Originally Posted by BAILOPAN View Post
Don't use ranges. They were a bad/buggy feature in the AMX Mod X version so we removed them.
Quote:
Originally Posted by BAILOPAN View Post
rhelgeby: We removed it because the implementation is extremely bad. It essentially expands it to a series of "if" statements, and this can mislead people into doing something like "0..100" whereas this would be much better:

Code:
if (value >= 0 && value <= 100)

Since the ".." syntax would result in:
Code:
if (value == 0 || value == 1 || value == 2 || value == 3 || value == 4 || value == 5) ...

(We could re-optimize this at run-time, but it seems like the right idea is to use if statements anyway.)

Last edited by PartialCloning; 11-08-2016 at 08:41. Reason: Because BAILOPAN.
PartialCloning is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 11-08-2016 , 11:22   Re: if -> switch
Reply With Quote #6

Yeah, i heard this information too.
An advanced member said in one post that when you do case x:y it makes entries from x to y like if x == 1 or x == 2 and so to x = y.

So basically if you do case 1:100 it makes 100 checks .

I don't know if it's true but i read this once in a post.
siriusmd99 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 11-08-2016 , 11:25   Re: if -> switch
Reply With Quote #7

Ok that makes sense then. It's a shame it works like that though.

@Kowalsky, you can handle ranges like this which looks cleaner, IMO.
PHP Code:
if( 90 )
...
else if ( 
80 <= 90 )
...
else if ( 
70 <= 80 )
... 
__________________

Last edited by Bugsy; 11-08-2016 at 11:35.
Bugsy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 15:53.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode