Full transcript of “Transfer Statements in JavaScript - break and continue | JavaScript Made Easy - Session 16”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.
0:00 Hello all, welcome to an another session in this course. As part of this session, I'm going to cover the next topic under flow control statements that is transfer statements. The last category of the flow control statements are transfer statements. In the previous session, I covered iterative or looping flow control statements for you. Okay, these were the things I covered. Okay, let me just show you the notes of the previous session to show you what exactly I have covered in the previous session. So just see here what exactly I have covered. I have covered this iterative statements which are also called as looping statements or loop statements. Okay. And
0:46 uh these are the ones. Okay. For loop, while loop, do loop, for loop, for in loop, for loop. In this all these type of loops I only only covered the first three types. Last two I told you that I'll be covering this one with after covering the object object kind of concepts and uh and I'll be covering for off with itable items like arrays and all those stuff. Okay. So for now in the last session I covered only these three loops and in the upcoming sessions I'm going to cover the remaining type of loops. Okay. So these are all the things guys. Okay. for loop while loop then uh we have this uh dowhile loop for in loop and for off loop I'll be covering later so out of all these five loops I covered the first three loops last two loops I'll be covering later so that happened
1:33 in the previous session now coming to the current session okay I'm going to cover the transfer statements for you okay I'm going to cover the transfer statements for you let me also open the notes for the transfer statements Here majorly there are four transfer statements in the JavaScript. There are majorly four transfer statements. One is break, second one is continue, third one is return and fourth one is through. Okay, you can clearly see here the most of the here are the most common transfer statements in JavaScript. Break continue, return and through. Break continue return end through
2:18 transfer statements. Okay. Transfer statements break. Next one is continue. Break continue. Third one is written. Third one is written. Fourth one is through.
3:04 Break, continue, return and throw. These are the major transfer statements we have in JavaScript language. Okay. So out of these four items in this session I'll be covering only the first two items. Okay. Uh return I need to cover in the upcoming sessions guys. At this point of time I cannot cover return because uh return need to be explained along with the functions kind of topic which is which will be covered later. So return will be postponed. The explanation or practical demonstration of the return will be done in the upcoming sessions. And coming to the throw also throw is somehow somewhat related to the exception handling and functions and all those stuff. Okay kind of stuff guys. So I cannot explain this uh throw right now. Okay. So these two items will be covered in the upcoming sessions. But in this session I'm going to cover two transfer statements that is break and continue one by one. So let's
3:51 get started with break. Okay break transfer statement. And uh what exactly are these transfer statements? If you remember guys I told you right transfer statements are one of the category of the statements under the flow control statements. And uh assume that this box is a JavaScript file and in that we have multiple lines of code like this. Okay, in this we have multiple lines of code like this for example. Okay, in the JavaScript file we have this multiple lines of code available. Now when I run this code the execution the execution of this code may happen something like this guys. Okay, the execution of the code may happen something like this. The execution may start here. From here it may directly jump here. Okay. From here it may go here. From
4:36 here it may go here. From here it may come here. May go here. Can come down. It may go here. Okay. Like this guys. Okay. Jumping. Okay. Jumping like a monkey. Okay. So if a if a monkey is jumping on a tree from one branch to another branch. The same way if you want the execution also to jump from one line of the code to another line of the code upward and downward. Okay. That is also possible with the help of the flow control statements and the category of statements which actually make this kind of scenario possible where you know jumping from one line of the code to another line of the code like monkey is jumping on a tree from one branch of the tree to another branch zigzag way. Okay, that is possible with the transfer statements. Okay, which category of statements under
5:22 the flow control statement this kind of situation is possible transfer statements. And these are the major there are many other transfer statements guys. Majorly if you have to discuss or learn about the transfer statements these are the major ones okay break continue return through in this these two items I'll cover now and the remaining two items I'll cover later return and through I'll cover later so let's get started with break let's get started with break remember guys where we have used break in the previous session okay in one of the previous sessions when I have explained about this conditional or selection statements there was another one of the conditional conditional or selection statement known as switch case Okay, whenever a particular case of the switch case matches and we go inside the case to come out of the switch case, we have used a break statement already. Okay, I have already explained how to how to use
6:08 a break statement and what will happen if you don't use a break statement in the switch case I already have covered this uh break statement which will break this break the switch case and come out. Okay, whenever this break statement under the switch case encounters the switch case will be broken and will be coming out of the switch case. Apart from this u apart from this switch case, we can also use break statement in loops. Okay, break statement can be used in switch case which I already have demonstrated in the previous sessions. Whereas we can also use this break statement in the loops like for loop, while loop, dowhile loop, for in loop, for of loop, all the type of the loops, you can use break statement. Okay. So what will happen if you use a break statement inside the loops? Whenever this break statement is encountered will come out out of the loop. Okay. like uh whenever the break
6:54 statement got encountered in the switch case we came out of the switch case completely outside right similarly whenever the break statement is encountered or executed inside the loop it will take you out of the loop simple it will take you out of the loop so for example I'll I'll demonstrate how to use a break statement in one of the loops that is for loop so for that I'll launch the visual studio code guys okay so in this editor I'm going to demonstrate how to use a break statement Okay, do one thing. Let's close all this stuff. Let me drag it here and let me delete this file. It's not uh required anymore. So, let's go here and create a new file. And I'll just uh write it out as break demo dot guess. Here I'll be writing the code for
7:41 uh sorry for let I = 1 I less than or equal to 10 or five other uh yeah I less than or equal to 5 then I ++ okay like this I'm writing the code okay for let I equ= 1 I less than equal to 5 I ++ and in this okay in this I'll be writing Okay, break put a semicolon. You see if I write the code like this and uh here I'll write break and I'll just print the value of I here. Okay, I would like to print the value of I and break statement is there. I'm trying to print the value of the I. If I run this code, what will be the output, guys? If I run this code, what will be
8:28 the output? Let's understand. First, this section, the value of I will be one. I will be created variable I will be created and it will be initialized with the value one. I is one. Now once the initialization is done the control should go to the next one that is condition that is one less than or equal to 5. One less than or equal to 5 which is also true. This is also true. I is one then one less than or equal to 5 is true. We'll go inside the loop. Here break statement will be encountered. Okay. And we'll completely come out of the loop. That's it guys. That's the end of the story. Nothing will be printed in the output console using node break demo dot jsd. If I say you see nothing got printed because directly break statement is there in the loop since the first condition itself okay when i is one the
9:14 first condition itself is satisfied will get went into the loop and break statement has taken you out of the loop okay it didn't print 1 to 5 is 1 to 1 to5 got printed if console.log log got executed 1 to5 would have been printed but because of the break statement nothing got printed if I remove the break statement from here the same code will print 1 to 5 you see 1 125 got printed but if I put the break statement nothing will be printed okay if I put the break statement nothing got printed you see nothing got printed that's a problem okay that's a problem now what I will do here instead of directly providing the break statement here I'll write a condition If I is okay equal to three or double equal to three or three
10:00 will equal to three nothing matters. Okay. Starting of the if block and inside that if I write the break statement what will be printed like if I write the uh code what will happen in this case? What will be the output? If this is a situation what will be the output guys? First I will be one. I will be created and I will be one. one less than or equal to five which is true. Okay, initially I is one. One less than or equal to 5 which is true. 1 is less than equal to 5 which is true. It will go inside the for loop. Then um 1 equal to 3 is false will not go inside the block. So break statement will not be executed in the first iteration. And here console.log I I one will be printed here. Okay. Because of this one will be printed.
10:46 Because of this one will be printed. Next we'll go to the end of this loop and I plus place will happen. I will become 2. 2 less than or equal to 5 which is also true. 2 less than or equal to 5 which is also true. Go inside the for loop. 2 equal to 3 is false. This is skipped. This will get skipped. Break statement will not get executed because 2 is not equal to three. Then here console.log of i means the current value of i is two. Two will be printed. End of the loop. I ++ i will become three. 3 less than equal to 5 which is true. 3 less than or equal to 5 is true. We'll go inside the loop. 3 equal to 3 is this time it is also true. If it is true, we'll go inside the block and for the first time break statement will get executed and will come out of the for loop. Only one and two will be printed. Remaining will not be printed because break statement has taken you completely
11:31 out of the for loop. When I became three and this statement from here we came out. Okay, that the reason only one and two will be printed. You see only one and two will be printed. In the third iteration, this condition became true and the break statement got executed. The moment the break statement got executed, we came out of the loop. So this is what is a break guys. Okay. This is what is a break. Okay. Fine. So in the uh third iteration break statement got executed and we came out of the loop and from there nothing got executed anymore. Okay. We directly came out of the the for loop has been broken. Okay. After two iterations the third iteration the for loop got broken. So only one end got printed here. Fine. This is what is all about the break statement guys. break statement will break the loop and take you outside of the loop whenever it is executed in
12:17 the loops. Okay, in any particular loop if the break statement gets executed, it may be while loop or do while loop or for loop break statement will there itself once it gets executed it will directly take you out of the loop. That's what is the concept of the break statement. Okay. Now the next one is next uh statement we have is a continue statement guys. Apart from the break statement, we also have a continue statement guys. Okay. So here in place of the break I'll write continue. The same logic in place of the break I'm writing the continue. What will happen? The continue will cancel the current iteration. The difference between break and continue you have to understand guys. Okay break this side break this side continue. What is the difference between the break
13:03 and continue? What is the difference between these two items? Break will break when used inside this loops or switch case. Okay. Will okay take you out of the loops by cancelling all the pending iterations. Okay. All the iterations otherwise. Okay. Once encountered break will take you out of the loops. break will stop the loop execution and take you out of the loop by calling all the iterations. Okay, by by canceling guys by cancelelling. This is not calling. This is cancelling. Okay. Break will break when executed
13:52 inside a loop will take you out of the loop by cancelelling all the iterations. Okay. Okay. By cancelelling all the iterations. Brave. What about continue? Continue when executed inside a loop will not cancel all the iterations or will not take you out of the loop. Rather continue when executed inside a loop will cancel only the current running iteration. Only current running iteration will be cancelled. Okay. And okay and next iterations
14:37 will be continued. Next loop iterations will be continued guys. Okay, that's what is continue. If you are using continue in place of the break, what will happen? Let me tell you here initially I is one. I value will be created and the value of I is 1. Then we'll go to this condition 1 less than or equal to 5 which is true. If it is true, we will go inside this loop. If it is true, we'll go inside this loop guys. And here I have to say here I value is one, right? 1 equal to 3 is false. This will be skipped because the condition is false. Means this this code will not be executed. And uh we'll go here console.log one will be printed. Okay, here one will be printed. We'll come to the end of the loop and here I place plus I will become two. Two less than equal to 5 which is true again will go inside the loop. 2 equal to 3 is false. This block will be skipped because the condition is resulting in false. Then
15:23 console.log of i means 2 will be printed. Now end of the loop and i + i will become three guys. Three less than equal to 5 which is true again. 3 less than equal to 5 is true again. If it is true go inside this loop and 3 equal to 3. Okay, this condition is for the first time true. So we'll go inside this if condition block if if block and continue statement will get executed. What the continue statement will do when it whenever it is encountered it will cancel the current iteration from here. The current iteration code that is if it is got executed from here to here the current iteration of this particular selected items will be ignored and directly will go to I++ without running this particular code after the continue statement directly will go to I++ because continue will cancel the current iteration. So I will become four. Okay.
16:08 So here 1 2 got printed but three didn't get printed because it has canceled the current iteration. So three didn't get printed here. Then I became four. Four less than equal to 5 is 2 will go inside the loop. And four equal to 3 is false. This code will be skipped. It will not be executed. So console do.log four will be printed. After 1 to four got printed. Third iteration got cancelled. Okay. Because of the three didn't get printed. Okay. Then I will become five which is true. This is false. Continue statement will not be executed. I value five will be printed. End of the loop six which is false here. This condition 6 less than or equal to 5 is false will come directly come out of the for loop. Now only 1 2 4 5 got printed. Three has not been printed because it has canceled the current iteration. Hope guys you are clear the difference between break and continue statement. If I put break here when the condition is directly it will
16:54 take you out of the loop. All the iterations will be cancelled. Whereas in case of continue only the current iteration will be canceled and next iteration will be continued. Okay. So this is what uh okay these what are the transfer statements we have in JavaScript guys. Okay. So let me show you the notes for the last time you know transfer statements are used to control the flow of the they are one category of the flow control statements in Java guys. Okay. Using this you can do all this stuff. Okay you can you can you can you do exit a loop early skip uh skip to the next iteration using the break and continue. These two things are possible. These two things I I'll be explaining later because I need to explain return and throw later. Okay. I only covered out of the four transfer statements we have I only covered break and continue written and throw I'll be covering later guys okay when the time comes I'll cover about the return and throw. So these two
17:39 items are about this written and throw. So I'm not explaining about them. So just scroll down here how to use a break statement and all you see the similar logic what that I have written continue statement how to use return statement I didn't cover and throw statement I didn't cover. So only break and continue you can you can use break and continue with you know you can use break with for for loop while loop do while loop other loops and switch case whereas continue statement only should be used with loops okay not with the switch case only with the loops you can use continue statement will skip the current iteration whereas break and continue with the next iteration whereas break statement will exit when it gets encountered by cancelelling all the iterations okay fine wherever it got encountered from there all the iterations will be cancelceled but continue will only cancel the current iteration and next iteration will be continued. Fine. So
18:26 hope guys uh with this uh okay with this we are done with the transfer statements in JavaScript. Okay. So we are done with the flow control statements in JavaScript guys. That is uh the previous sessions I covered conditional or selection statements, iterative or looping statements and now in this session I covered the transfer statements. The next session I'm going to cover about the comments. Till then see you. Bye-bye.