![]() |
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:
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:
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:
|
Nice work.
|
Very nice Zero! :)
_____________________________ Oh, and for arrays: This code will not work: PHP Code:
PHP Code:
|
Quote:
|
Quote:
|
Ya, isnt @ converting arrays to strings?
|
@ 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:
PHP Code:
|
Oh sweet
I never knew I would be brought up in a thread nor be a script command :D |
Quote:
PHP Code:
|
Thats so good about you Switch. Though you are missing the most important function. You cannot be turned off...
|
Quote:
|
Quote:
|
Why was this never stickied? :confused:
|
Is it possible to do something like
PHP Code:
|
Quote:
PHP Code:
|
Quote:
PHP Code:
|
Quote:
PHP Code:
|
I assumed that was just part of the example, I read it forever ago. Should have reread it.
|
Sticky the thread already!
|
Quote:
|
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? |
You know the title now so you can use the forums search button
|
Quote:
Also keep this page handy. |
Quote:
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:
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... |
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. |
Quote:
Edit: It is! Bookmark this page: http://wiki.graal.net/index.php/Crea...rol_Structures |
Quote:
Eventually I'd like to make a list of commands GS2 and 'better' details how to make everything. Thank you though.. Quote:
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.. |
Quote:
|
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. |
Quote:
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:
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.. |
Quote:
|
Quote:
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; |
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. |
Quote:
Quote:
|
Quote:
http://uploads.graalcenter.org/uploa...24.47%20AM.png |
1 Attachment(s)
Quote:
Sorry though, it seems I have upset you greatly, didn't mean to get to you that bad. Quote:
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:
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.. |
seriously?
|
Quote:
Quote:
|
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:
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.. |
Quote:
|
| All times are GMT +2. The time now is 11:32 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.