AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   switch() (https://forums.alliedmods.net/showthread.php?t=49711)

Rolnaaba 01-11-2007 12:58

switch()
 
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.

teame06 01-11-2007 13:04

Re: switch()
 
Yes, it is the same

Orangutanz 01-11-2007 13:07

Re: switch()
 
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    } }

Rolnaaba 01-11-2007 13:12

Re: switch()
 
ok ty

Greenberet 01-11-2007 13:13

Re: switch()
 
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

VEN 01-11-2007 13:26

Re: switch()
 
#5: You can use only constant values for cases

Rolnaaba 01-11-2007 13:44

Re: switch()
 
ok thanks green/ven


All times are GMT -4. The time now is 22:27.

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