Raised This Month: $ Target: $400
 0% 

switch() & case


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
capndurk
Senior Member
Join Date: Feb 2006
Old 03-25-2006 , 16:47   switch() & case
Reply With Quote #1

I have two questions:

1) Can I switch a string? For example:

Code:
switch(string) {     case "whatever": //code     case "whatever 2": //code }

and

2) Is it possible to combine cases with an & or |? For example:

Code:
case "whatever": // same code as "whatever 2" case "whatever 2": // same code as "whatever" case "whatever" | "whatever 2": // code

Any help is greatly appreciated.
capndurk is offline
Werewolf
Senior Member
Join Date: Jan 2006
Location: Sweden
Old 03-25-2006 , 16:54  
Reply With Quote #2

Should work. Example:
Code:
if(case 1: || case 2:) { //function }
I don't know if it works but it is worth to try... If not someone says "It won't work"
__________________
~~~~~Hawk~~~~~
Werewolf is offline
capndurk
Senior Member
Join Date: Feb 2006
Old 03-25-2006 , 17:00  
Reply With Quote #3

Quote:
Originally Posted by Werewolf
Should work. Example:
Code:
if(case 1: || case 2:) { //function }
I don't know if it works but it is worth to try... If not someone says "It won't work"
I know, but i just wanted to avoid doing if statements because they can get kind of ugly with 4 difference cases, and then a default case.
capndurk is offline
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 03-25-2006 , 17:31  
Reply With Quote #4

Or, you could just replicate the code twice.
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
capndurk
Senior Member
Join Date: Feb 2006
Old 03-25-2006 , 17:39  
Reply With Quote #5

Quote:
Originally Posted by Twilight Suzuka
Or, you could just replicate the code twice.
Or four times..


Thanks anyway guys.
capndurk is offline
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 03-25-2006 , 20:06  
Reply With Quote #6

Or you can check a single character in a string like I sometimes do:
Code:
switch(str[0]) {   case '+': start_cmd(id);   case '-': stop_cmd(id); }
Here's an example of how I use it:

Code:
#include <amxmodx> public plugin_init() {   register_clcmd("+gaben" , "cmd_gaben");   register_clcmd("-gaben" , "cmd_gaben");   // noticed they are both hooked to the same function } public cmd_gaben(id) {   new arg[8];   read_argv(0 , arg , 7);   switch(str[0])   {     case '+': start_cmd(id);     case '-': stop_cmd(id);   }   return PLUGIN_HANDLED; } public start_cmd(id) {   // } public stop_cmd(id) {   // }
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
capndurk
Senior Member
Join Date: Feb 2006
Old 03-25-2006 , 21:24  
Reply With Quote #7

Thanks a lot v3x, I'll try that.
capndurk is offline
Xanimos
Veteran Member
Join Date: Apr 2005
Location: Florida
Old 03-25-2006 , 22:28  
Reply With Quote #8

to have multiple cases use this
Code:
switch(Var) {     case 1 , 2 , 3 , 4:  DoThis();     case 5 , 6, 7, 8: DoThat(); }
Xanimos is offline
Send a message via AIM to Xanimos Send a message via MSN to Xanimos
capndurk
Senior Member
Join Date: Feb 2006
Old 03-26-2006 , 02:56  
Reply With Quote #9

Thanks for the information guys.
capndurk is offline
Greenberet
AMX Mod X Beta Tester
Join Date: Apr 2004
Location: Vienna
Old 03-26-2006 , 03:12  
Reply With Quote #10

here is an example how to use a case for a range:

Code:
switch( bla ) {         case 0 .. 20: client_print( 0, print_chat, "Bla is  between 0 and 20" );         case 21 .. 50: client_print( 0, print_chat, "Bla is  between 21 and 50" ); }
Greenberet is offline
Send a message via ICQ to Greenberet Send a message via MSN to Greenberet
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 16:43.


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