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.

Cubical 10-07-2010 06:45 AM

Quote:

Originally Posted by fowlplay4 (Post 1604719)
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.

It was mainly just for my curiosity because I don't even have Graal installed right now. I was wondering for something along the lines of this.
PHP Code:

switch(cmd){
  case 
"command1":
    break;
  case 
"command2":
    break;
  case 
"help": default:  // if help or no params are given then continue
    
sendtorc("commandlist");
    break;



Tigairius 10-07-2010 08:02 AM

Quote:

Originally Posted by Cubical (Post 1604724)
It was mainly just for my curiosity because I don't even have Graal installed right now. I was wondering for something along the lines of this.
PHP Code:

switch(cmd){
  case 
"command1":
    break;
  case 
"command2":
    break;
  case 
"help": default:  // if help or no params are given then continue
    
sendtorc("commandlist");
    break;



Best way is to do it like this, as suggested in original post:

PHP Code:

switch (cmd) {
  case 
"command1":
  case 
"command2":
    
dostuff;
  break;
  
  default:
    
domorestuff;


either command1 or command2 will activate "dostuff;" and doesn't really hurt readability.

Cubical 10-07-2010 07:30 PM

I assumed that was just part of the example, I read it forever ago. Should have reread it.

xXziroXx 10-08-2010 03:55 PM

Sticky the thread already!

MrOmega 10-08-2010 05:15 PM

Quote:

Originally Posted by xXziroXx (Post 1604892)
Sticky the thread already!

Second and this should be added to the bible as well.

Devil_Lord2 03-01-2012 09:18 PM

Seriously, is it possible to change the title of this to Switch and Case?
So then when I google Switch, or Case, I won't have to be redirected from another forum spot to here, and come here at the start?

Cubical 03-01-2012 09:32 PM

You know the title now so you can use the forums search button

cbk1994 03-01-2012 11:01 PM

Quote:

Originally Posted by Devil_Lord2 (Post 1686566)
Seriously, is it possible to change the title of this to Switch and Case?
So then when I google Switch, or Case, I won't have to be redirected from another forum spot to here, and come here at the start?

Try using a bookmark; modern web browsers are very good at navigating the web, and, surprisingly, you're not the first person who has ever needed to save a page for later.

Also keep this page handy.

Devil_Lord2 03-02-2012 05:24 PM

Quote:

Originally Posted by Cubical (Post 1686569)
You know the title now so you can use the forums search button

I do not use the forums, I use google since it usually has more value then people telling others who are asking in the forums how to do something to search the forums. The searches, which by the way don't always turn up what you are looking for, aren't very great nor are they organized.

Then you can get into the many people who tell you to use the wikia and the bible which are even more horrible than the forums lol...

Also, you could have said that the first three times I had to search for it, not everyone remembers every single thing they find. ;]
I for one have horrible memory, and would rather look for "GS2 Graal Cases" on google. In turn that would send me to Astrams post about Switches on here, which you then have to scroll down to see someone saying to go to the spoils of switch... Redundant clicks which could be simplified by naming something correctly.


Quote:

Originally Posted by cbk1994 (Post 1686583)
Try using a bookmark; modern web browsers are very good at navigating the web, and, surprisingly, you're not the first person who has ever needed to save a page for later.

Also keep this page handy.

I'm probably not the first person who has had to replace my hard drive in the last four weeks twice either.

And I highly doubt myself bookmarking it is going to help the others who might need this as well...








Thank you both for your help, you have both changed my life in some way. :]
I'd appreciate it if the question that was asked was actually answered instead of veering off topic next time however..

I can't say I appreciate the person who spammed my Green, Red, and Neutral bar comment thing though. At least comment in something relating to the reason of the picking...

Devil_Lord2 03-03-2012 05:22 AM

Lol... I am told I am being an ass, because selfishly people are telling me to do things, when I'd like something done for everyone else to have easier access to this..

I'm welcome to more red bars if anyone likes.
And I'd still like the name to be changed so many people can find this easier.

MattKan 03-03-2012 05:24 AM

Quote:

Originally Posted by Devil_Lord2 (Post 1686698)
Lol... I am told I am being an ass, because selfishly people are telling me to do things, when I'd like something done for everyone else to have easier access to this..

I'm welcome to more red bars if anyone likes.
And I'd still like the name to be changed so many people can find this easier.

Just bookmark it. The title is fine. Isn't this page bookmarked on the graal bible too?

Edit: It is! Bookmark this page: http://wiki.graal.net/index.php/Crea...rol_Structures

Devil_Lord2 03-03-2012 05:29 AM

Quote:

Originally Posted by MattKan (Post 1686699)
Just bookmark it. The title is fine. Isn't this page bookmarked on the graal bible too?

Edit: It is! Bookmark this page: http://wiki.graal.net/index.php/Crea...rol_Structures

I try not to use Graal Bible unless I absolutely have to, if you go to table of contents there is basically nothing when one should expect a list from A-Z of commands or topics lol... Also, searching for things on it doesn't seem reliable either... D:

Eventually I'd like to make a list of commands GS2 and 'better' details how to make everything. Thank you though..


Quote:

Originally Posted by cbk1994 (Post 1686583)
Also keep this page handy.

By the way, I missed this, still not relating to my question, I'm glad you posted it. This does look like it has some useful things!

I might be turning my computer in for another due to the Lemon Law which I will have to look up on.. So I can just search for this page to find this lol..

MattKan 03-03-2012 05:41 AM

Quote:

Originally Posted by Devil_Lord2 (Post 1686701)
By the way, I missed this, still not relating to my question

http://img641.imageshack.us/img641/1844/umya.png

fowlplay4 03-03-2012 05:42 AM

Dear mod who changed the thread title,

Since you're renaming it, please rename it to 'switch case statement' not 'switch and case statements'. What you've done is just wrong, considering no one (excluding special_lord2) around here has ever referred to it as a case statement.

Devil_Lord2 03-03-2012 08:17 AM

Quote:

Originally Posted by fowlplay4 (Post 1686704)
Dear mod who changed the thread title,

Since you're renaming it, please rename it to 'switch case statement' not 'switch and case statements'. What you've done is just wrong, considering no one (excluding special_lord2) around here has ever referred to it as a case statement.

Isn't name calling a little uncalled for? And if you'd like to check around the posts, you are actually the only one who has used the word statement(s).

I have only said Switch and Case, and Switch, or Case.
If you are going to make fun of me, at least do it with a little bit more dignity lol...

Quote:

Originally Posted by Devil_Lord2 (Post 1686566)
Seriously, is it possible to change the title of this to Switch and Case?
So then when I google Switch, or Case, I won't have to be redirected from another forum spot to here, and come here at the start?


Since the question is not registering through anyone's head, the question as stated basically asks to rename a thread. Renaming a thread on http://forums.graalonline.com/ has absolutely nothing to do with Graal Bible (Or so I believe).

Once again, is it possible that this thread get renamed to something a little more relevant?

---

Also, fowlplay4, I believe either you are seeing something that I am not, or if I am correct, the name was never changed and has always been "The Spoils of the Switch and Case". I'm not sure if statements were ever in it though..

Crow 03-03-2012 09:17 AM

Quote:

Originally Posted by Devil_Lord2 (Post 1686708)
Once again, is it possible that this thread get renamed to something a little more relevant?

The title is relevant enough and linked to in several places. The content is something you shall learn and then not forget again, and I don't think this any difficult either. There's not much to switch and case, but the information the thread provides is useful nonetheless.

Devil_Lord2 03-03-2012 09:34 AM

Quote:

Originally Posted by Crow (Post 1686711)
The title is relevant enough and linked to in several places. The content is something you shall learn and then not forget again, and I don't think this any difficult either. There's not much to switch and case, but the information the thread provides is useful nonetheless.

The linking may be true, but it isn't easily found in google which is the main search engine I think most people use, and as it is my main point since I personally don't find the websites that link it that useful.. Nor have I checked out how hard it is to find it on them, but I have tried searching for other things on them.

And you shouldn't speak for others about not forgetting things. >_<
As I've said before, I've found and used this page quite a few times.. I've learned and used them for about 1-2 weeks afterwards and forgot them.

Coincidentally, I've also forgot most of GS2 after 4+ months of not having RC and not using GS2. And this happens often.. Even riding a bike isn't very easy for me each time on the first try. Everything starts off a little wobbly if you don't keep at it..

I can explain the top part if you'd like, fowlplay4 would be happy.

And sure, I suppose it is relevant if everyone is basically saying we should not use switch and case because it isn't good.. I personally find it of value some times.. :[

===


Anyway, if no one really cares about how easily something useful can be found, without having things bookmarked, I suppose my question really doesn't matter. I didn't think the post would be worthy of arguments by only asking to rename a thread, it all seems silly to me.

But I did enjoy everyone flaming me while they were off topic, and I did get to learn about a helpful page. Thanks again Mattkan!

P.S.: I did check on the main page to find the post that you linked, I have no clue how you would find it on Graal Bible without having the actual link.. x.x;

fowlplay4 03-03-2012 09:43 AM

Regardless the title is still bad and I do not like it.

P.S.: Write less, nobody gives a **** about your constant life stories, how you can't remember anything, how something makes you feel, etc. If they did you would have 5 light green rep bars already.

Crow 03-03-2012 09:47 AM

Quote:

Originally Posted by Devil_Lord2 (Post 1686712)
The linking may be true, but it isn't easily found in google which is the main search engine I think most people use

Boo hoo. Seriously, who searches on Google for GS2 help? :oo: The thread comes up when searching for "switch" here on the forums, that should be more than enough.


Quote:

Originally Posted by fowlplay4 (Post 1686713)
P.S.: Write less, nobody gives a **** about your constant life stories

^

cbk1994 03-03-2012 11:26 AM

Quote:

Originally Posted by Devil_Lord2 (Post 1686712)
The linking may be true, but it isn't easily found in google

Which Google are you using?

http://uploads.graalcenter.org/uploa...24.47%20AM.png

Devil_Lord2 03-03-2012 06:26 PM

1 Attachment(s)
Quote:

Originally Posted by fowlplay4 (Post 1686713)
Regardless the title is still bad and I do not like it.

P.S.: Write less, nobody gives a **** about your constant life stories, how you can't remember anything, how something makes you feel, etc. If they did you would have 5 light green rep bars already.

I want people to give me bad rep, (didn't know what it was called), since I feel others use it for no real purpose, and it doesn't seem to have one.

Sorry though, it seems I have upset you greatly, didn't mean to get to you that bad.


Quote:

Originally Posted by Crow (Post 1686714)
Boo hoo. Seriously, who searches on Google for GS2 help? :oo: The thread comes up when searching for "switch" here on the forums, that should be more than enough.

Someone who wants to find things quickly I suppose. ;]

Although one could also argue you aren't learning anything by doing so.. Seeing it often enough is learning, and it isn't like I don't know what they do.


Quote:

Originally Posted by cbk1994 (Post 1686718)

I use www.google.com, but since there are so many, I guess I can see how you could see how there could be mixed signals of some sort.. :D

Edit-
Oh look above, I've clicked the ones I thought would be relevant, didn't help at all.. Anything I search for I use "GS2 Graal" and the command I'd like to figure out or refresh on.

Add in "and case" and you find my page for scripting help faster than you find this one..

salesman 03-03-2012 06:30 PM

seriously?

Crow 03-03-2012 06:31 PM

Quote:

Originally Posted by Devil_Lord2 (Post 1686734)
Someone who wants to find things quickly I suppose. ;]

GS2 is a very minor language, you really shouldn't use Google to find out things about it.


Quote:

Originally Posted by Devil_Lord2 (Post 1686734)
Oh look above, I've clicked the ones I thought would be relevant, didn't help at all.. Anything I search for I use "GS2 Graal" and the command I'd like to figure out or refresh on.

You need to refine your search. Learn how to search on Google efficiently, there are a couple guides about it. "Switch" is too common a word to just search for it. In this case, "switch statement" works wonders, like in Chris' query.

Devil_Lord2 03-03-2012 09:15 PM

Salesman, it is really hard for me not to be sarcastic about your comment.
So I will only say, I wouldn't have posted if I didn't think it would help anyone.

Quote:

Originally Posted by Crow (Post 1686736)
GS2 is a very minor language, you really shouldn't use Google to find out things about it.




You need to refine your search. Learn how to search on Google efficiently, there are a couple guides about it. "Switch" is too common a word to just search for it. In this case, "switch statement" works wonders, like in Chris' query.

I have learned to use search engines, which is why I'm only using three words lol.. for the most part, searching GS2 Graal and anything turns out what ever I need.

Unless it links me to the wiki or Graal bible, then the results are usually not the solution.. However, it does take me to the forum in spots that I need or that can help..

I use to have commands 2.RTF and an old GS1 list, but once again my computer was wiped.

Next time, should I remember, I'll try GS2 Graal Switch Statement. The only problem is, since I don't use the word statement for GS2, it never comes to mind. x:

Honestly, I'm still unclear why the word statement keeps coming up lol..

salesman 03-03-2012 09:25 PM

Quote:

Originally Posted by Devil_Lord2 (Post 1686751)
Honestly, I'm still unclear why the word statement keeps coming up lol..

http://en.wikipedia.org/wiki/Stateme...puter_science)


All times are GMT +2. The time now is 12:11 PM.

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