Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   New Scripting Engine (GS2) (https://forums.graalonline.com/forums/forumdisplay.php?f=153)
-   -   The Spoils of the Switch and Case Statements (https://forums.graalonline.com/forums/showthread.php?t=76294)

zokemon 08-16-2007 02:50 AM

The Spoils of the Switch and Case Statements
 
This is not a question thread at all but rather a thread to make the scripting community more aware of the power of using switch in your GS2 coding.

I'm also posting this because I don't want to steal the pride of a certain someone after he updated a certain wiki's switch statement page (by updating it my self).


Using the switch to do a case, break, case, break, case, break series is often common. But what people don't know is what you can do outside of such a series. Rather then try to explain it off the bat, I'll show you an example code I made on GK Debug (can't say what its for, sorry):

PHP Code:

LINE 01: switch (this.mystatus) {
LINE 02:   case 1:
LINE 03:   case 3:
LINE 04:   case 5:
LINE 05:     fnGetItem(1"board""boards");
LINE 06:   break;
LINE 07:   case 7:
LINE 08:   case 9:
LINE 09:     fnGetItem(1"iron""iron");
LINE 10:   break;
LINE 11:   default:
LINE 12:     true;
LINE 13:   break;
LINE 14: } 

Basically here is the rule for a switch:
When ever a case is encountered, if that case proves true, all the code following that case statement will be executed (from top to bottom) ignoring all following case statements. A break can be used to "jump out" of the switch to avoid executing unwanted statements of another case.

In a sense, a switch just jumps into the point that the correct case is found and jumps out at the first break found after that (if there is one).

Let's just jump into the code by presenting certain examples:

Let's say this.mystatus is 5:
The code when then start at line 4 and execute both line 5 and then line 6. At line 6 is a break so the switch then ends at that point.

Let's say this.mystatus is 8:
The switch contains no cases for 8 so it starts at the default (line 11). Lines 12 and and 13 are then executed. At line 13 is a break so the switch then ends at that point but keep in mind since this is the last "case" (so to speak) in the switch, the final break is really only there for looks (the switch would end regardless since it is the end).

Another thing to note about default: A switch will start at default if it has found no matching cases before. Thus, if you have any cases after the break in your default, they will always be ignored. For this reason, your default should go at the end of your switch no matter what (putting it at the beginning without a break would be just stupid because you could put that code segment before the switch).

Let's say this.mystatus is 1:
Now the switch starts at line 2 and continues on with the code from there. Following our rule (which says to ignore all following case's), it only executes lines 5 and 6 because lines 3 and 4 are both case's that will be ignored (a case was already found). Just like if this.mystatus was 3 or 5, this code will do the function on line 5 and then end the switch on line 6.

I think the rest is pretty self explanatory, if you have any questions though or are confused, please feel free to ask!

Hope this helps,
- Zero

EDIT:

Here is info on the basics of a switch for those that don't know:

The format for a switch can be read from above but you may not know how to implement it. Look at this:
PHP Code:

switch (this.myvar) { 

This is getting the contents of "this.myvar" and using it to compare to all the switch's case's. The variable (this.myvar) could be a float (number) or a string (series of letters, numbers and/or characters) too! I currently haven't tested if an array works (array members work just fine such as switch (this.myvar[2])) or if an object works. I'll have to get back to you on that.

Now the case's:
The case's should just match the variable type that you used in the switch. If the switch variable is a float, you should do something like I did in the above example. As for strings, here is a good example:

PHP Code:

LINE 01: switch (this.mystring) {
LINE 02:   case "abc":
LINE 03:   case "beans":
LINE 04:   case "ghi":
LINE 05:     fnGetItem(1"board""boards");
LINE 06:   break;
LINE 07:   case "jkl":
LINE 08:   case "I'm a donkey":
LINE 09:     fnGetItem(1"iron""iron");
LINE 10:   break;
LINE 11:   default:
LINE 12:     true;
LINE 13:   break;
LINE 14: } 

Make sense? :)

Twinny 08-16-2007 03:27 AM

Nice work.

Chompy 08-16-2007 12:15 PM

Very nice Zero! :)
_____________________________
Oh, and for arrays:

This code will not work:

PHP Code:

function onCreated() {
  
temp.= {01};
  
  switch(
temp.a) {
    case {
01}:
      echo(
"foo");
    break;
  }


But, this code will work
PHP Code:

function onCreated() {
  
temp.= {01};
  
  switch(@ 
temp.a) {
    case @{
01}:
      echo(
"foo");
    break;
  }



Inverness 08-16-2007 08:27 PM

Quote:

Originally Posted by Chompy (Post 1340472)
PHP Code:

function onCreated() {
  
temp.= {01};
  
  switch(@ 
temp.a) {
    case @{
01}:
      echo(
"foo");
    break;
  }



Its converting to string: "0,1"

Chompy 08-16-2007 09:04 PM

Quote:

Originally Posted by Inverness (Post 1340613)
Its converting to string: "0,1"

:o

Crow 08-16-2007 10:29 PM

Ya, isnt @ converting arrays to strings?

zokemon 08-17-2007 01:08 AM

@ is only associated with strings yes so it converts any array into a string format (like you would see if it was saved in a text file).

Did you try like:

PHP Code:

function onCreated() {
  
temp.= {01};
  
temp.= {01};
  
  switch(
temp.a) {
    case 
temp.b:
      echo(
"foo");
    break;
  }


Because you can only use {} for arrays in certain cases (like assignments and in "in" [some other things too]). I know you can't do like:

PHP Code:

if (array == {"a""b""c"}) 


Switch 08-21-2007 02:36 AM

Oh sweet
I never knew I would be brought up in a thread nor be a script command :D

zokemon 08-21-2007 04:23 AM

Quote:

Originally Posted by Switch (Post 1341903)
Oh sweet
I never knew I would be brought up in a thread nor be a script command :D

PHP Code:

zokemon (myvar) {
  
script "bean":
    
stuff();
  break;



Crow 08-21-2007 03:11 PM

Thats so good about you Switch. Though you are missing the most important function. You cannot be turned off...

coreys 08-21-2007 08:56 PM

Quote:

Originally Posted by zokemon (Post 1341941)
PHP Code:

zokemon (myvar) {
  
script "bean":
    
stuff();
  break;



What? 0_0

Chompy 08-21-2007 09:09 PM

Quote:

Originally Posted by coreys (Post 1342107)
What? 0_0

It was a joke ;o

xXziroXx 06-21-2009 01:38 PM

Why was this never stickied? :confused:

Cubical 10-07-2010 06:35 AM

Is it possible to do something like
PHP Code:

  switch(blah){
    case 
"blah" "blah"//if either are true then proceed
      
doStuff();
      break;
    default case 
"blah"//if either are true then proceed
      
doStuff();
      break;
  } 


fowlplay4 10-07-2010 06:37 AM

Quote:

Originally Posted by Cubical (Post 1604717)
Is it possible to do something like
PHP Code:

  switch(blah){
    case 
"blah" "blah"//if either are true then proceed
      
doStuff();
      break;
    default case 
"blah"//if either are true then proceed
      
doStuff();
      break;
  } 


You can do that like this..

PHP Code:

  switch(blah){
    case 
"blah": case "blah2":
      
doStuff();
      break;
    case 
"blah3": default:
      
doStuff();
      break;
  } 

Drops read-ability quite a bit imo though.


All times are GMT +2. The time now is 09:04 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.