Raised This Month: $ Target: $400
 0% 

switch()


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Rolnaaba
Veteran Member
Join Date: May 2006
Old 01-11-2007 , 12:58   switch()
Reply With Quote #1

I am fuzzy on what is needed for a switch to work properly so would this:
Code:
new temp switch(temp) {      case 0:      case 1:      case 2:      case 3: }
be the same as
Code:
new temp if(temp == 1) { } else if(temp == 2) { } else if(temp == 3) { } else if(temp == 4) { }
since temp is a int. var. ranging from 1-4 in both cases.
__________________
DO NOT PM me about avp mod.
Rolnaaba is offline
teame06
i have a hat
Join Date: Feb 2005
Location: Hat City
Old 01-11-2007 , 13:04   Re: switch()
Reply With Quote #2

Yes, it is the same
__________________
No private support via Instant Message
GunGame:SM Released
teame06 is offline
Send a message via AIM to teame06
Orangutanz
Veteran Member
Join Date: Apr 2006
Old 01-11-2007 , 13:07   Re: switch()
Reply With Quote #3

Yep that is right.

With the cases you can also do ranges:

Code:
new temp switch (temp) {    case 1 .. 5:    {       // do something    }    default:    {       // do something else    } } // You can also do new temp[12] = "hello world" switch (temp[0]) {    case 'h':    {       // do something    } }
__________________
|<-- Retired from everything Small/Pawn related -->|
You know when you've been Rango'd
Orangutanz is offline
Rolnaaba
Veteran Member
Join Date: May 2006
Old 01-11-2007 , 13:12   Re: switch()
Reply With Quote #4

ok ty
__________________
DO NOT PM me about avp mod.
Rolnaaba is offline
Greenberet
AMX Mod X Beta Tester
Join Date: Apr 2004
Location: Vienna
Old 01-11-2007 , 13:13   Re: switch()
Reply With Quote #5

it isnt the same
Fact1: in your case example you compare 0 - 3 and in your if example 1-4. It would be the same if you use the same values.
Fact2: the switch/case statement is a lot faster then if/elseiif
Fact3: it looks better in the file
Fact4: you cant compare strings with the switch/case statement
Greenberet is offline
Send a message via ICQ to Greenberet Send a message via MSN to Greenberet
VEN
Veteran Member
Join Date: Jan 2005
Old 01-11-2007 , 13:26   Re: switch()
Reply With Quote #6

#5: You can use only constant values for cases
VEN is offline
Rolnaaba
Veteran Member
Join Date: May 2006
Old 01-11-2007 , 13:44   Re: switch()
Reply With Quote #7

ok thanks green/ven
__________________
DO NOT PM me about avp mod.
Rolnaaba 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 22:27.


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