Hey all, I have a couple of quick questions.
With the switch statement are you supposed to place "break" inside or outside the curly brackets? I searched the manual (1.4) but the example given doesn't use curly brackets.
I haven't noticed any difference either way so I'm just curious does it matter? Is one way better if you plan on learning other languages?
The basic answer has already been given, inside the brackets, but I'm not sure that it actually makes a difference in GML as GML doesn't even require you to use brackets for cases in a switch statement. However, I think that it is more helpful to think of break as what it actually is, rather than just in its usefulness in a switch statement, to understand where you should use it. In other words, break is not just a way to end a specific case in a switch statement. Quoting from the manual, "The "break" is used to end prematurely a
for,
repeat,
while,
do... until loop of some kind, or to tell a
switch statement to end at that point, or to prematurely end a
with function."
The reason it is important to think of it this way is that it allows you to do so many more things than just end a switch statement. See, for example outside of a switch statement, the manual on this:
https://docs.yoyogames.com/source/d...e/001_gml language overview/401_13_break.html
But it also has other uses in a switch statement. For example:
Code:
switch (something) {
case 0:
/* code here */
case 1:
/* code here */
break;
}
If something == 0 then what is in both case 0 and case 1 will run whereas if something == 1 then only what is in case 1 will run. Additionally, you may sometimes want to do this:
Code:
switch (case) {
case 0:
/* some code */
if (something_else) {
/* some more code */
break;
}
/* remaining code */
break;
}
Here some code in case 0 runs, then it checks an if statement, and if it is true some more code runs AND it exits the case before running the remaining code.
So a more complete answer to your original question is you can put break in a lot of places and your code will do exactly what it is told to, break out of the loop, switch statement, or with statement and continue on. You should decide where to put it based upon what you want to happen, when you want to break out.