Raised This Month: $32 Target: $400
 8% 

Does switch reevaluate per case statement?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 02-10-2011 , 15:38   Does switch reevaluate per case statement?
Reply With Quote #1

One thing that's been bugging me for a while is not knowing if Switch re-evaluates per case statement.

In the wiki it has something like:
PHP Code:
new Team GetClientTeam(client);
switch (
Team)
{
        case 
Whatever:
        case 
Differentval:

Now that's all good fun, but what about this?

PHP Code:
switch (GetClientTeam(client))
{
   case 
Whatever:
   case 
Differentval:

Is GetClientTeam called twice in this case? once for each case?
KyleS is offline
Monkeys
Veteran Member
Join Date: Jan 2010
Old 02-10-2011 , 15:47   Re: Does switch reevaluate per case statement?
Reply With Quote #2

It loads the function once, then compares it to the values.
No worries.
__________________
Get a lid on that zombie,
he's never gonna be alri-i-ight.
Oooh get a lid on that zombie,
or he's gonna feed all night.
Monkeys is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 02-10-2011 , 18:37   Re: Does switch reevaluate per case statement?
Reply With Quote #3

The reason for declaring it before the switch is in case it's being used again. As Monkeys said, it's evaluated once, and then stored for further comparison between the cases.
bl4nk is offline
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 02-10-2011 , 18:48   Re: Does switch reevaluate per case statement?
Reply With Quote #4

Awesome, thanks guys!
KyleS is offline
Monkeys
Veteran Member
Join Date: Jan 2010
Old 02-10-2011 , 21:43   Re: Does switch reevaluate per case statement?
Reply With Quote #5

By the way, variables can't be used as cases in a switch. So your Whatever and Differentval would throw errors.
__________________
Get a lid on that zombie,
he's never gonna be alri-i-ight.
Oooh get a lid on that zombie,
or he's gonna feed all night.
Monkeys is offline
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 02-11-2011 , 00:33   Re: Does switch reevaluate per case statement?
Reply With Quote #6

Quote:
Originally Posted by Monkeys View Post
By the way, variables can't be used as cases in a switch. So your Whatever and Differentval would throw errors.
In this case lets make them numerical defines
KyleS is offline
Monkeys
Veteran Member
Join Date: Jan 2010
Old 02-11-2011 , 00:44   Re: Does switch reevaluate per case statement?
Reply With Quote #7

Doesn't need to be numerical, though. :p
Can be a char too.
__________________
Get a lid on that zombie,
he's never gonna be alri-i-ight.
Oooh get a lid on that zombie,
or he's gonna feed all night.
Monkeys is offline
Reply


Thread Tools
Display Modes

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 04:47.


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