welcome back to my Roblox beginner scripting tutorial guide my name is brev and in this episode we'll be talking about breaks and continues now before we do uh I want to go back to our Loop script that we created in the last episode because there was something I didn't mention about using task. weight statements for these for loops and also while loops and things like that I forgot to mention that if we were to have multiple for Loops like one for loop after another so not a nested Loop but a for Loop or a while
loop or any kind of loop that that happens back to back like let's say we had another for Loop by saying 4 I = 1 comma 100 comma 1 do and then we write a print statement here saying uh this is the second for Loop like this you might expect both of these for Loops to run but in actuality if we go into the game and hit play then it's not going to print um our second for Loop and instead it's just going to go through the color changing scripts that we had in the previous
um Loop now why is this happening well because when we're going through our first for loop it's going to go through the first iteration and it's going to change the base Plate's brick color it's going to wait one do this wait one do this wait one and it's going to go back up here but it's trying to wait until this for Loop is finished before it goes down here to execute whatever it is that it's going to execute afterwards which is why um it's important for us to um realize when we want to execute our
for Loops or our while loop Loops because the order matters when it comes to um doing these sorts of things now there are ways we can spontaneously run for Loops at the same time but that's going to be covered in a future episode in my Advanced scripting tutorial guide so you don't really have to worry about that too much but that's just something I wanted to add on to that um before we continued with what we're going to be talking about in today's episode okay so now that we've covered that let's go back to our
Loop script so that we can talk about what breaks and continues are so I'm going to delete um basically everything that we have in the script up until this point so I'm going to hit contr a and then hit backspace and what I'm going to do is create another for Loop so I'm going to say um four let's make our incremental value um let's say I this time so we're going to say 4 I = 1 comma uh let's make some really big number like let's say 100,000 like this um and then we're going to
have our incremental value as one now here's another thing I didn't mention in the last episode and that is you actually don't need to have an incremental value if you know that it's going to be one because the incremental value is going to be one by default so I'm actually just going to delete that because we're going to be incrementing by one unless you wanted it to be something different like two or three then yeah this is the only time you would need to have the incremental value but in this case we don't need it
so we're going to say one comma 100,000 do and then we're going to hit enter just like this now if we were to run this for Loop 100,000 times it's pretty ridiculous with how much processing power is going to be needed in order to execute whatever is going to be inside of here so if I were to make a print statement saying um this statement Min is printed just like this um then obviously we wouldn't really want to go through all 100,000 of these iterations because at some point we may want to stop if a
certain condition has met like if let's say we were in the 200th iteration then we would probably want to stop it before it reaches the 100,000th iteration so what we can do is actually write a conditional by saying if I is equal to let's say uh 200 then what we're going to do is use what's called Break just like this so so it's going to go down the loop by starting at one and then it's going to keep incrementing until I reaches 200 and once it does it's literally going to stop the for Loop by
using this break statement so if we go into the game and hit play then what we should see is this statement is printed 200 times and it did not print 100,000 times like we were supposed to because we made it stop at the 200 Mark and so this is very important if certain conditions have been met for your for Loop and you don't want the for Loop or your wall Loop to run anymore we can do the same thing with while Loops so if we were to delete this code right over here and write a
while loop instead so we're going to say well actually we don't need to get rid of this what we can do down here is just say uh our counter variable so we're going to say local uh counter equals one comma or no sorry so we're going to say local counter equals 1 so that's the value we're going to start at and then what we're going to do is say while counter is less than or equal to some ridiculously big number like let's say uh 1 million instead uh we can do this number so we're going
to say do so we're going to write our incremental statement um because as you know a for Loop does it for us but a while loop doesn't so we have to do it ourselves we have to say counter equals counter + 1 just like that and that's going to happen at the very end of the W Loop so next thing we're going to do is check for a condition if counter is equal to let's say 500 then what we're going to do is write a break right over here now the first thing we're going to
do is write our print statement by saying this while statement is printed just like this and so now this is basically our while loop that does um basically the same thing as this up here except it's going to stop at 500 instead of 200 we can even confirm this by instead of 200 so now if we go into the game and hit play then what we should see in the output is this statement is printed in the for Loop 200 times and this while statement is printed 500 times because that's when we decide to stop
it now that is essentially how we use breakes inside of a scripts but there's another one we can use and that is called continue so what I'm going to do is delete these breaks that we had inside of our examples and what I'm going to do for this for Loop is basically say uh if a certain condition is met then we just want to skip this iteration so basically what I mean is if we start at the very top here by saying if I is equal to let's say 10 then uh we're going to make
a print statement by saying I is equal to 10 so skip this iteration just like that and then um that's basically going to okay and then what we're going to do is drop a line and then say the keyword continue like this so what is happening with this for Loop so basically if we start at one then it's going to check if I is equal to 10 if it's not then it's just going to go down here and make the print statement but if it does equal to 10 then it's going to basically use this
continue keyword to skip this iteration so it doesn't make this print statement happen um so if we go into the game and hit play then what should be happening is oh wait a minute I forgot to break the script oh this is about to be really slow uh okay so okay so looks like the the script crashed it said script timeout exhaust allowed execution time and that's because if we go to our script I forgot to leave the break statement inside of here um thinking that it was a good idea to just get rid of
it so uh take that as a lesson for me to not do that um what we're going to do in here is basically say if I is equal to 200 then we're just going to break the statement just like that uh we need to be careful about that and I think I'm going to do the same thing here as well so if I is or no sorry if counter for this wall Loop is equal to uh 200 then we're going to break it just like this okay so I'm just going to separate these so that
it's easier to read but yeah um take that as a lesson to make sure that you do have break statements if you don't want to execute this many number of times so what we were supposed to do here is if we go into the game then we can see that it first states that this statement is printed nine times and then it goes to the 10th iteration by saying I equal to 10 so we're going to skip this iteration and then finally it's going to print the rest of the statements until it reaches 200 and
that is basically uh how this for Loop structure goes with continues and also breaks so these two are very important for controlling our for loops and also our while Loops if we reach certain conditions inside of our scripts so that is what I wanted to show you inside of this episode and I hope you find this to be useful now as an added bonus I'm going to teach you about the concept of comments because when we've been working with Scripts up until this point we've had lines of code that we just didn't want to execute
while we wanted other pieces of code to execute but we didn't want to delete the entire statement because we just wanted to bring it back uh once we were done with whatever it is we were trying to execute so I'm going to be teaching you something that you can actually use to save your pieces of code that doesn't run uh so that we can have other pieces of code run inside of our scripts and that is with the use of comments so what I'm going to do is basically make our for Loop and our while
loop visible uh just like this and this is how we write a comment to basically block out code if we don't want to execute it inside of our script so the way we write a comment is by basically putting in two dashes just like this so it's going to be gr out like this and we can put in whatever we want inside of the script by writing I don't know a print statement here uh with whatever it is we wanted to print inside of here and as you can see it's grayed out so what this
means is this line is isn't going to be printed inside of our script but it is going to be saved here in case we want to use this script later for for testing purposes or if we just wanted to uh add this back inside of our script for later use then we can just uh uncomment this print statement and it's going to show that we we are able to run this again so if we want to hide it then we can just add in two dashes before it and it's not going to run inside of
a script now if we want to comment out multiple lines what we can do is basically put in two dashes and then we're going to put in what's called hard brackets so instead of parentheses what we're going to use is hard brackets and we're going to use two of them so we're going to put in the left hard bracket and then another left hard bracket and then we're going to hit enter and we're going to hit enter again and basically what's going to be contained inside of here is basically comments that make up multiple lines
because if we were to drop a line from this print statement then it's just going to execute in inside of our script but we can have multiple comments within this block right here so we can have a line here we can have a line here a line here and then a line here so it's just going to keep on going as long as it fits within the boundaries of this comment block so now with that in mind we can comment out this while true do statement if we don't want to execute it by putting in
two dashes just like this and then the hard bracket hard bracket and then we're going to add two hard brackets at the end so we're going to drop a line and then add uh the end bracket and then another end bracket just like this and as you can see this code block is now ComEd out and we can put down um more things inside of here and this is not going to be executed so if we go inside of the game hit stop and then hit play one more time then we can see that it's
just going to be the for Loop that executes and it's going to skip this while loop and once again if we want to execute our while loop again then we're just going to get rid of the comments so that we can show it up inside of a script again so that's something very useful that I think you should be able to add inside of your scripts um if you want to execute specific blocks of code and if you also want to comment out blocks of code that you don't want to execute in this current uh
time frame um comments are also useful if you want to just um have a human readable way to like Express like what certain what certain uh pieces of code do so if I were to add a comment over here by saying this for Loop uh goes through 100,000 uh iterations until certain conditions are met so this is another way you can use comments it's just by saying or explaining what pieces of code do and that's another thing you can do with comments as well so that's basically a short introduction to comments in this episode so
for today's learning objective what I want you to do is add break statements and continue statements to your code and also use comments to describe what your for loops and your wall Loops do or just any pieces of code in general and for whatever code that you don't use I want you to comment them out using comment blocks or just comment statements so you have a lot to do for this episode so I'm excited to see what you do for this one so once again once you finish your code I want you to go down
to the comments paste your code so that other people can see what you uh have done for this challenge if you are comfortable sharing it so with that being said that's going to be it I I hope you enjoyed it and I will see you in the next one take care