Podcast Lookup

Java Loops & Jump Statements Explained | for, while, do-while, break & continue

The Java Developers Hub

  • December 3, 2026
  • 1 h 25 min
  • Episode 13
Episode page
About this episodeLoops are fundamental programming structures that allow developers to execute a block of code repeatedly without writing the same…
Loops are fundamental programming structures that allow developers to execute a block of code repeatedly without writing the same instructions multiple times. They make programs more efficient, flexible, and capable of handling repetitive tasks.In this episode, we explore Java loops and jump statements, including for, while, and do-while. You'll also learn how to control loop execution using break and continue.Through practical examples, you'll understand when to use each loop, how Java evaluates loop conditions, and how to control the flow of your programs.Whether you're a beginner learning Java or strengthening your programming logic, this tutorial will help you write more efficient and structured Java code.What You'll Learn
  • Understanding loops and iteration in Java.

  • Why loops are important in programming.

  • Using the for loop for repeated execution.

  • Understanding the while loop.

  • How the do-while loop works.

  • Differences between for, while, and do-while.

  • Understanding loop initialization, conditions, and updates.

  • Using nested loops in Java.

  • Understanding the break statement.

  • Using continue to skip the current iteration.

  • Differences between break and continue.

  • Avoiding infinite loops.

  • Practical examples of Java loops and jump statements.

Who Should Watch?
  • Beginners learning Java programming.

  • Computer science and software engineering students.

  • Developers strengthening their programming fundamentals.

  • Students preparing for Java examinations.

  • Anyone interested in improving logical thinking and problem-solving skills.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Java Loops & Jump Statements Explained | for, while, do-while, break & continue

00:00—
  1. Speaker 1

    Yes, so let's start.

  2. Speaker 2

    Welcome back Coder Army to our Java series. And today we are going to continue our last lecture. We were looking at control statements in Yadav. And we saw the selection statement. In which you get if, else if, switch, etc. In this lecture, we will go one step ahead and look at iteration statements which we also call loops like while loop, for loop, do while loop etc. And we will look at another type of statement which we call jump statements. You will get to learn a lot of things. If you already know iteration statements like loops etc. and jump statements then also you will get to learn something. So let's start and quickly move to the screen. So, as I told you that in Java or any programming language, we call iteration statements as loops. Now, what are loops? You must be able to understand from the name itself. What we discussed in the last lecture is that we have a control flow of execution. So, let's say we have a code written in this way and normally our flow of control runs in this direction. So, first the first line is executed, then the second, then the third and so on. But we used to have a conditional statement. We used to divide a control flow in multiple ways. That means you used to have a choice. That you can go here or there. If you talk about if else, you used to have two choices.

  3. Speaker 1

    And we used to have an if else if letter.

  4. Speaker 2

    Which used to give you multiple choices. It can have more than 2, 4, 5, 10 choices. Switch used to work in the same way. But now the question is, how do your loops work? So see, loops say, let's suppose in this control flow, I have a part of code written, let's say this one. And I want this part of code to execute again and again. How many times? I will define a number for that. So let's say I have a part of code here, let's say it starts with a curly bracket and ends with a curly bracket. And I want it to execute in loops again and again. Meaning, first of all, we should execute the statement written in the loop and then go back up. Execute that statement again and then go back up. Execute that statement again and then go back up. And it should continue like this. For a predefined number, we call it loops. So, what happens in this? Control flow comes from here first and enters the loop. It will iterate in the loop that time. We call it iteration.

  5. Speaker 1

    That's why it is also called iteration statement.

  6. Speaker 2

    Let's say the number was n equal to 3 And n tells the number of iterations So we will iterate in this loop 3 times So 1, 2, 3 And after that we will come out of this loop And we will start executing the normal again So basically our control flow will be like this First it will come down from the starting It will keep rotating in the loop as many times as we have given the number And then it will come out of the loop again So this is called loops in programming And in java our loops are divided into 3 parts First and the simplest we have is while loop One is do while loop and the other is for loop. We just have to read these three loops and I think we will go directly to the code part so that you can understand it better. Once you understand these three loops, the part of loops will be completely finished and you will understand well how to use them and how to use them. So let's go to our editor. So yes, I have come to my empty project. And I have made a file demo.java. So let's start and see our loops first. So first of all, I will tell you how we got the use of loops. So let's say I give you a situation. Let's say I declare a number int i equal to 1. Very good. Now I want you to print this value on the screen and increment it till 10. Basically, I want to tell you to print 1 to 10 on the screen. Now there are two ways to do this. One is to simply write system.out.println and here, one second, println, yes, now it's correct. And write 1 here.

  7. Speaker 1

    So you printed 1. Then write println again.

  8. Speaker 2

    And write 2 here. So you printed 2. And keep doing this till 10. This is one way. Second, you print i here. i will be printed 1. And every time do i + +.

  9. Speaker 1

    Print i again.

  10. Speaker 2

    Right? And keep doing i + +. This is the second method. Till now we have learnt this only. Right? These are the only two methods we have learnt. But how long will you keep doing this? Till 10, you can do it for once.

  11. Speaker 1

    You wrote 1, 2.

  12. Speaker 2

    Here, I did it for 3. Here, now 1, 2 and 3 will be printed. Let's do it once on the screen and see. Right?

  13. Speaker 1

    Look, 1, 2 and 3 are printed. Very good.

  14. Speaker 2

    So, we can keep doing this. But with this, a very long code will be written. If you keep writing like this, right? So the better way is to use loops. So we will not write like this. This is not the way. We will comment it. And we will use loops. So the first loop that we get is called while loop. Now let's put it from here. Now see. What is your while loop? Let's see its syntax first and then we will understand how it works. First of all, we will write the keyword while. Now what will come here? I will tell you. Then again we will open our curly brackets, that is, we will open our block. And here it will be do something, whatever we want to do in the loop. Now see what will happen here, here will be such an expression that will evaluate in a boolean variable. Boolean means either it can be true or it can be false. Simple. Now again, we talked about this in the case of if. If we write here, an expression used to come here which evaluates for boolean.

  15. Speaker 1

    So let's say I write a boolean. I had to print 1 to 10. So I write a boolean.

  16. Speaker 2

    Till the value of i If it is less than or equal to 10, I will write less than equal to 10. So I have written i less than or equal to 10. This is a boolean condition because its answer will be.

  17. Speaker 1

    Either true or false.

  18. Speaker 2

    Now you must be understanding the name while. It means until it is not like this, you keep doing this.

  19. Speaker 1

    Whatever I will write here will keep happening.

  20. Speaker 2

    Means again till this condition is not true, whatever is written in the loop will continue to happen.

  21. Speaker 1

    Now what will happen?

  22. Speaker 2

    What I had to do? I had to print. So here I will write system dot out dot println and i will print it. What does it mean? First of all, what was the value of i? It was 1. That means a variable will be declared as 1. After that, we will enter in this loop. First of all, we will check this condition. Is i smaller than 10? The answer is true. If the answer is true, then only we will enter in this block. Then I printed I on the screen. Remember, whenever you write println, it changes the line for you. So, as soon as I printed 1, my line will change next. Now, as soon as I printed 1, My loop statement is over. Now I went up again. Why? Because I'm working in a loop. I can't get out of this loop until this condition is false. It's not like if I executed this block once and came out of this block. No. I won't come out of this loop until this condition is false.

  23. Speaker 1

    I went up again.

  24. Speaker 2

    I still have I1. Then I checked again. Is 1 smaller than 10? Yes. Then I printed 1. Then what will be printed on the screen? Then our 1 will be printed like this. Then I will go up and see that 1 is still smaller than 10, then I will print 1. And in this way, I will keep printing 1, 1, 1, 1, and I will not be able to get out of this loop for the rest of my life. Because I don't have any terminating condition. And we also call this kind of loop as infinite loop. So first of all, we saw that while loop can also be made infinite.

  25. Speaker 1

    Infinite means that it will continue for.

  26. Speaker 2

    The rest of your life because you can never exit from this loop because this condition will always be true. Now to make this false, what we.

  27. Speaker 1

    Do is we make i + + every time.

  28. Speaker 2

    Right? Something like this. We have written the same statement here. First print, then plus plus. This is what I am doing. Print, then plus plus. Now see, first 1 will print, then i2 will happen. Then 2 will print, then i3 will happen. And this will keep happening until this condition is true. And when will this condition be true? Until i is equal to 10 or smaller. As soon as i is 11, this condition is false. And we will come out of this while loop here. Your control flow came here. So now you can print from 1 to 10. You printed from 1 to 10. Very good. So this is your simple while loop. There is no rocket science in it. If I give you a basic syntax of this while loop, then see how it works. Let's make a multiple line command. So that it is easy for me to give.

  29. Speaker 1

    Okay, better. Now I will give you a basic syntax of while loop.

  30. Speaker 2

    So it works like this. Here we write expression and here we have curly bracket open and here we write do something And here we can either upgrade this expression or not. This is the basic syntax of while loop. While i is less than or equal to 10, we printed i and kept doing i + +. Now let's see 2-3 more things. We closed curly bracket open and closed curly bracket close because we wrote more than one statement like if. If we have only one statement in the while loop then we can avoid this curly bracket. So we can write it like this. So we can write like this. But in this case, we can't write i + + because we have written one statement. So this will become our infinite loop. If you are thinking to write i + + in the next line, then also it won't work. Because according to that, the condition of the loop is just this.

  31. Speaker 1

    This is out of the loop.

  32. Speaker 2

    Right, remember this is out of the loop, according to this while loop, because we didn't give any curly bracket, if you want, our loop, consider this also, inside the loop, then you have to give, here and here, curly bracket, like we did here, right, so this was one thing, And generally, again, it's not a good coding practice to avoid curly brackets.

  33. Speaker 1

    Even if you have one line of code, you.

  34. Speaker 2

    Should always use curly brackets because you never know when you have to add a new line. This is what we talked about in the if case statement. Okay, here we printed from 1 to 10. Can we do the opposite? Right? If our i value was 10 from the beginning, something like this, 10. Okay? And we would write that i less than equal to 1. till i becomes 1. So I wrote it as less than equal to 1. I would have printed i, but this time I would have done i minus minus instead of i plus plus. Right?

  35. Speaker 1

    I would have done something like this.

  36. Speaker 2

    Right? So would my 10 to 1 be printed? Yes, it would have been printed. See, we can do this too. We have printed reverse till 10 to 1. So we can take a loop either straight or reverse. It depends on our first two expressions and on our incremental or decremental statement. Okay? Okay. till now we had read operators in java, can we write something in this expression without writing it separately? we can write it, what if I want to print 1 to 10 again, so what I do is, I make the value of i to 1 again, Now I want to print i to 1 to 10. So first I will write i is smaller than or equal to 10. And here I would have done i + +. So we would have written something like this. While i is less than or equal to 10, we would have printed i and i + +. So without writing this statement, In this statement, if I need to write it, I can increment it here. We can do it. Remember, when we read operators in Java, we read an increment operator. We read that i + + is a valid operator. Which makes i a number plus. So what if I write it like this? i + + is less than or equal to 10. Now if we write like this, then will my 1 to 10 print? First i will be 1. We will compare 1 to 10. 1 is smaller than 10. Then we will print 1. If we do it like this, then it will be 2. And as soon as we print 1, 1 will be incremented to 2.

  37. Speaker 1

    Because 1++ is there. Then we will compare 2 to 10. It should be printed like this.

  38. Speaker 2

    It won't be. Why won't it be?

  39. Speaker 1

    Look, we have compared i + + to 10.

  40. Speaker 2

    As soon as we compare 1 plus plus with 10, 1 is compared with 10, then 1 becomes 2. As soon as this line was about to execute, our 1 became 2. So here 2 will be printed instead of 1. And instead of 2, 3 will be printed. Instead of 3, 4 will be printed. Instead of 10, 11 will be printed. This loop will print 11 from 2. If you can't believe it, you can run it. See, 11 from 2. So, I hope you understood how it is printing from 2 to 11. Because we are comparing first, but in the next line, we increment it. So, the best way is to start i from 0 instead of 1. Now, we can start it from 0 instead of 1. But if we start it from 0, then also, it will not be 1 to 10, it will be 1 to 11. Because the lower condition is fulfilled. First, it compares 0 to 10, then it becomes 1. But the upper condition is still not fulfilled. For this, we can remove this equal to sign. Now, our i + + should be less than 10, not equal to 10. So that the big condition, the condition of 11, can never be true. So, if we run this now, then see, everything is printed again from 1 to 10. Very good. So you don't have to do anything. You have to play with increment decrement operator. And you have to play with this while loop. And you can easily print 1 to 10 or any such thing. Okay. I hope you understood while loop well. If you understood while loop well. Then do while loop is also not magic. It is very simple. So first I will tell you a syntax of do while loop. See it is like this. You write keyword do. So do means do something. Do let's say something. And here you write while And you terminate it Now what comes here, here comes any expression This is its syntax, I will again comment it, this is its syntax Now again if we want to use it then how will we do it, I will write the syntax again First we wrote do, here will come some expression and here will come a while.

  41. Speaker 1

    And here we will terminate.

  42. Speaker 2

    It Now this do while loop is a little different from while loop It says that first do something Whatever is written in do Let's say do something First do something And after that check that condition So let's say again if I want to print 1 to 10 So do while loop will say that first you print i So system.out.println I will first write i which we have to print Okay, I have written this. The value of i was 0 in the beginning. Let's make it 1 again. So, I printed 1 first. After that, let's say I will also increment i. So, i is now 2. And I will compare in the end whether i is less than or equal to 10. If it is, then it is okay. If not, then continue this loop. Sorry, this will not come inside, it will come out. Okay. So I did something first and then compared it. But what was happening here? If I uncomment it again, what was happening here? Here I used to do comparison first and then I used to print it. If I write it as normal statement and do i + + here, now you might understand it better. Look, In the while loop, we used to check the expression. If your expression is true, then only we used to enter this loop. And we used to execute this loop until the expression was true. Do while loop is a little different. It says, first you do all the work that you have to do. And at last you check the condition. If the condition fails once and you come out of the block and the condition is still true, then do that work again and then check. Then do that work again and then check. So basically normal while loop first checks the condition and then works. Do while loop first works and then checks the condition. That's all the difference. But where is the difference practically used? Let me tell you that. First, let's comment on it and see what is printed here. So, we have printed 1 to 10 again in this do while loop. So, if we have printed like this, and we were able to print like this, then what is the difference between the two? And why did we need do while loop? This is a very good question. Now, there is a slight difference between these two. And that is the difference of one line. That is, if the condition fails for the first time in while, then no statement will be executed. Right? This means, let's say I said I less than 10. But suppose, I is 11. Now suppose I is 11. Right? This means if I is 11 and I run this loop and let's say I comment this loop for now, then what would happen?

  43. Speaker 1

    We compare I with 10 first. Is I smaller than 10?

  44. Speaker 2

    No, 11 is bigger than 10. So we don't enter this loop even once and we get out of this loop. Which means nothing is printed. Right? Absolutely correct. But now you see this do while loop. this do while loop. This do while loop tells us to enter the loop first. We haven't checked anything yet. We came here. We directly printed 11. Then we made an increment of 11 to 12. And then we compared 12 with 10. And when we compared, we found out that the condition is false. It is false. So we came out of this loop and stood here. But once it was printed, right? So let's see it quickly. First, let's print it with while. So no output should come here. Because our while will not enter. So see, no output came. Blank. And let's print it once with do while So here we should get 11 output Because it will print once and then it will stop running So we got 11 output, very good But now what is the benefit of this? Why do we need such a loop? While loop is understandable, right? If the condition fails for the first time, then it doesn't enter the loop But why do we need such a loop? That even if the condition is wrong, we enter the loop once It has a very specific use case and what do we call that use case? Menu item selection. Selection. Imagine any game, right? What used to happen in that game? You used to get a menu in the starting. Let's say, I again write this by commenting. You assume that you are playing a game and it has some menu items. Like one was play game. One was return, saved game, anything. And let's say one was exit. So you had these three options. If you click on the play game, the game would start. Then you would return to the saved game. If you go to the saved game and exit, then you would come out of the game. What do you think? How can you implement this? If I give you a problem statement. I will tell you that you have to make a menu item. And there will be these three options. If the user clicks on the play game, then he will be able to play the game. If he loses the game, then he will come back to this menu. If he clicks on the play game again, he will be able to play again. If he loses, he will come back to this menu. the game, he will come back to this menu. And if he goes to the return saved game, he will go to a saved game. Then he can come back here. And if he exits, he will exit from here. Now you must be understanding that the user plays the game again and again, and when he comes back after playing the game, then we bring him back to this menu, right? That means some loop is being used here, right? So we can show the loop here very well. But which loop? While loop or do while loop? And its answer is do while loop. For menu item selection do while loop is the best loop. Why? Because no matter if any condition is true or false, you will show the menu to the user first.

  45. Speaker 1

    So as soon as you open a game, you.

  46. Speaker 2

    Show the menu to the user that you want to play the game, return saved game or exit. Now if the user wants to play the game, he will click on play game and come back. If he doesn't want to play and wants to return the saved game, he will click on play game. And if he doesn't want to do anything and wants to exit the game, he will click on exit. So at least once you show him the option of what to do. So for such conditions, for menu item selection, do while loop is best. Because do while loop first executes.

  47. Speaker 1

    Means first it will show the option to the user.

  48. Speaker 2

    That these are the options you have, tell me what to do. Enter 1, 2 or 3. Let's say this is 1 point. This is your 1 and this is your 2. Obviously, you will move it with cursor. But let's say we ask the user to enter some input from the keyboard. 1, 2 or 3, whatever will come, I will do it for you. To do this, you think do while loop is the best loop. Because first you show it. If the user put 3, then the condition failed. You come out of the loop. If the user put 1, So now let it play the game using another to You take it to the saved game And when that saved game is over then come back here And keep going like this So if you ever get a situation Where you have to execute something at least once And after that the condition will also depend on how many times it will be executed In that case you will use do while loop Otherwise your normal while loop is also very good Where did it go? See your normal while loop is also very good Okay So I hope you understood while loop and do while loop well Both are variants of Wild. The thing to remember is that this expression It should always be a boolean expression. That means it should be evaluated in a boolean expression. Okay, very good. We have read while, do while. Did we have any other type of loop? Absolutely. We call it for loop. This is the last type of loop. But it is a very interesting loop. In fact, when you write code or you see any code, then the most used loop in it is for loop. Okay. So, let's first see the syntax of for loop and understand it.

  49. Speaker 1

    So, look.

  50. Speaker 2

    Again, our problem statement is the same. Let I have to print 1, 2, 10, not I to 10, 1, 2, 10. Okay. So, I told you how to do while loop and do while loop. Now, what is for loop? Now, for loop is a little different from while loop in syntax. So, let's see its syntax first. First of all, we write the keyword for. And here, something will come inside. Let me tell you what will come. And then we do open bracket and close bracket. And again, because this is also a loop, we can write do something. Let me tell you how many times you have to do something. Now let's see what will come in the for loop. See the red line here means the syntax is not correct. So three things will come in the for loop. First of all, you will tell an initialization variable. Let's say I wrote int i equal to 1. So I initialized a variable, int i equal to 1. Or you can write it outside as well. Or simply declare this int i outside as int i. So here you write int i equal to 1, not i equal to 1. It means that I just need one such variable, minimum, which I will increment or decrement in the loop. Like we saw in while, right? So for now, I declared int i above, and here I assigned it with 1.

  51. Speaker 1

    So the first statement is this.

  52. Speaker 2

    If you don't want to do it separately, then you can simply do it here. Int i equal to 1. And you can remove it. Something like this. In fact, we mostly follow this practice. Because I know, the work of this i variable, It is going to remain till this loop. So I declared it here. So int i equal to 1. After this you put a semicolon. After that, you will tell your conditional statement which you used to write in the while loop, the same statement will come here. How long should this increment be? Until the value of i is smaller or equal to 10. Okay, then a semicolon. So you told two things. What is the initial value of i? And how long should this loop run? How long should this loop be true? Until the value of i is smaller or equal to 10. And finally, your increment or decrement condition will come. What you have to do? You have to keep doing i + +. This is the exact syntax of for loop. So see, you have written three things in it. What is the initial variable? How long do you have to run this loop? And every time, how much increment or decrement do you have to do that loop? Okay, what did we have to do? We had to print this. So system.out. Let's say I write println and I print i. Something like this. And here we have written it wrong, so let's correct it. Now let's print it and see. We got the output of 1 to 10, which was expected. Very good. Now see how the flow of control works here. It works in such a way that first of all, our flow of control will be outside the loop.

  53. Speaker 1

    Then our flow of control will come inside the loop.

  54. Speaker 2

    So first of all we will execute the first statement, we will declare a variable i and give it a value of 1. So let's assume i is a container with a value of 1 in it. So this statement will be executed only once because every time you will keep making new declarations. So we declared it only once. As soon as this statement is executed, then we will check our second statement. Is our i, which is 1, equal to or less than 10? The answer is true. So again, its answer should be a Boolean. Either it will be true or it will be false. Right? So we compared this i with 10. So first we assigned i as 1. Very good.

  55. Speaker 1

    After that we compared it with 10.

  56. Speaker 2

    Very good.

  57. Speaker 1

    I is less than true.

  58. Speaker 2

    As soon as it comes true, then I will go inside the body of my loop. Whatever work I have to do, I have to do it. So first of all, I will see flow of control of if, sorry of for. So first of all, what will happen? First assignment statement is executed. Okay, what is assignment? Which I will Which we also call as Variable Definition. This line of yours. Okay. So first of all, this will be executed when you enter the loop for the first time. After that, we will write our conditional expression. Okay. Then, second conditional statement is evaluated. Is evaluated. So we will evaluate whether i is smaller than 10. Its answer will be either true or either false. Okay? Very good. So, if true, if our second statement is true, if true, control flow will evaluate the body of the loop. So, after that, if the condition is true, then we will evaluate the body of the loop. The body of the loop means nothing. Whatever is written in this open and closing bracket, that is the body of the loop. So, we will evaluate the body of the loop. It means that we have printed 1. Okay?

  59. Speaker 1

    Now, as soon as once loop body.

  60. Speaker 2

    Loop body is finished as soon as it is finished, our end will come here as soon as we print 1 ok, what will we do? what will be the control flow? control flow will go back to the for statement our for keyword will go back there and third increment statement will be evaluated Evaluated So.

  61. Speaker 1

    After that what will happen?

  62. Speaker 2

    As soon as it goes up, this increment statement will be evaluated. So first 1 got printed, we went up, now 1 got 2. Now as soon as 1 got 2, then what will happen in the 5th point? Again, your conditional statement is evaluated. Statement is evaluated. Again we compared 2 with 10. Is 2 smaller than 10? Yes. Then again we will go to the body of the loop. And after that this process will continue. I mean I will write in.

  63. Speaker 1

    The 6th point.

  64. Speaker 2

    Repeat 2 to 5 Now repeat this 2 to 5 again and again This is the flow, repeat it again and again Means, you incremented the i again, it became 2 Compare the 2 with 10, it is small Print it again Go up again, incremented the i again, it became i3.

  65. Speaker 1

    Checked, 3 is smaller than 10.

  66. Speaker 2

    Yes, it is.

  67. Speaker 1

    Printed 3.

  68. Speaker 2

    Then came up again and it kept on running. Till when? Till this statement is not false. What will happen in the end? As soon as 10 is printed here, we will go up, do 11 of 10 and try to compare 11 with 10. When it fails, we will come out of this loop and stand here. So, this is how the for loop works. These are the pointers for how it works. Now, again, we can reverse this for loop. What can we do?

  69. Speaker 1

    We can start i.

  70. Speaker 2

    From 10 And we can say until i is less than or equal to 1 And we can do minus minus instead of plus plus So in this way you can see what you can do See it printed in reverse 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 Absolutely good So I hope you understood this too Now in for loop there is one more enhancement Which we call in java For each loop. For each loop. Okay.

  71. Speaker 1

    This is also a very good loop.

  72. Speaker 2

    But we will read this when we will understand the arrays and strings in Java. Okay. Because after that you will enjoy using this loop. Now you will not understand it properly. Although there is not much. It is the same of this small loop. It changes the syntax a little. It gets simplified a little. There is nothing else. So when we will see the arrays then we will see this for each loop too. But for now, I hope you understood this for loop well. So if you understood once what is while loop, for loop, do while loop in java. We will discuss a few more things about it. Let's go inside a bit more. See, first of all, let's say I declared a variable int i equal to let's say 5. Okay, very good. You can leave a loop's body empty too. Means if I write like this while i less than 10. Till i is less than 10, don't do anything. I left it empty. Now what will happen? Suppose I left it empty. And after this I tried to print something. System.out.println Here I printed something.

  73. Speaker 1

    Let's say hello. So will this hello print ever happen?

  74. Speaker 2

    Think carefully. It won't happen.

  75. Speaker 1

    Why won't it happen?

  76. Speaker 2

    Look. I said i is 5. And in this while loop, I wrote till i is less than 10.

  77. Speaker 1

    Is i less than 10?

  78. Speaker 2

    Yes.

  79. Speaker 1

    The control flow statement.

  80. Speaker 2

    Will come inside. It saw that nothing is written here. That means the loop is finished. Then it went back up. But the value of i is still 5. No one has incremented or decremented i. So 5 is still less than 10. Again, what is this? It became an infinite loop. The control flow of this loop will never come out. That means this line will never be executed. That's why we call it infinite loop. You should execute it once.

  81. Speaker 1

    But look, I'll show you.

  82. Speaker 2

    No statement is coming And see our code is going on and on This is not ending because it is stuck in the infinite loop How long will this go on? Until our memory crashes Basically we have terminated our loop We have terminated our program here Then we stopped our code till the time your ram's memory is not finished you can terminate it. So this was the way to make any loop infinite in while loop.

  83. Speaker 1

    Now let's see the way in for loop.

  84. Speaker 2

    Let's suppose we write for loop like this. for int i equal to 1, i is less than 10 and we write i + + like this. But I have declared it here so I will remove it. Now here I have done that from 1 to 10 this loop will run. I + + and I can do anything inside. Let's say I write system.out.println and this time I am not printing 1 to 10.

  85. Speaker 1

    I am printing hello 10 times.

  86. Speaker 2

    Think about it. If you write like this then you also know what will happen. Hello will be printed 10 times.

  87. Speaker 1

    Like this.

  88. Speaker 2

    See this. Very good. Now I am saying that I want to make it an infinite loop.

  89. Speaker 1

    So I can remove i + +.

  90. Speaker 2

    Now i will be 10 from 1, but there won't be any plus plus, so this infinite loop will continue. If you see how the infinite loop looks, then it looks like this. It's getting printed, and it won't stop. Until I terminate it myself. See how many times it's been printed. So I'll have to terminate it. i + + means optionalize. I can write it or remove it. In the same way, you know, I can remove i less than 10 too.

  91. Speaker 1

    Like this.

  92. Speaker 2

    But yes, I will have to give two semicolons in between. Basically, I can't write anything here. But I will have to leave its empty space. So I will have to write int i equal to 1 semicolon, then empty space, then semicolon and i + +. So I can do it like this. This means I said i is 1 and keep doing i + +. But I didn't give any upper condition that till when I keep doing i + +. So this is also an infinite loop. This will also be printed. If you want to see, this is also an infinite loop. If I declare int i equal to 1 somewhere above, So this condition is also optional.

  93. Speaker 1

    But I still have to give this semicolon.

  94. Speaker 2

    So I have to give semicolon, empty space, semicolon, i + +. So what I am saying is, start i with 1, keep doing plus plus, and there shouldn't be any above statement. So it's okay, there is still an infinite loop, and this is the right loop. This can work, it's syntactically correct. Compiler, see, no syntax error will show here, and it will keep running here. If I run it, see, your hello print is getting done, see, continuously. So I cut it, I terminate it. Apart from this, I am telling you that since I + + was optional, I can remove this too. That is, I can write my entire for loop like this. See, how strange the syntax is. For colon colon semicolon semicolon and I.

  95. Speaker 1

    Print a line.

  96. Speaker 2

    Hello. Obviously, this is also an infinite loop because here we are not doing any increment, nor are we comparing with anyone, nor are we assigning anything.

  97. Speaker 1

    That is, the three parts that we write in the for loop are optional.

  98. Speaker 2

    If you run this too, then your hello hello will be printed throughout your life. I hope you have understood how to write for loop infinite loop and while loop infinite loop. Now you can write infinite loop in do while loop in the same way. If you let the while condition run throughout your life and you don't increment it. Let's go back to the normal for loop. Now, there is another type of variation in the for loop which we call comma separated variation. Now, let's see what it says. It says that in the same loop, we were playing with one variable int i equal to 1, i less than 10, i plus plus. Suppose I need two variables i and j and I want to use both of them in this loop, then it is not necessary that I can use the next j variable inside the loop.

  99. Speaker 1

    I can use it inside the syntax of this for loop.

  100. Speaker 2

    Let's see how. Let's say if I had i equal to 1 i less than 10 i plus plus and if I was printing only i like this then you know we have less than equal to so you know what happens we have 1 to 10 number printed whatever we were doing till now but let's say I don't want to print number 1 to 10 I want to print 1 to 10 squares so let's say 1 into 1 is 1 2 into 2 is 4 3 into 3 is 9 4 into 4 is 16 so I want to print all the squares so now if I want to print all the squares then what can I do? First, I have a way to print simple i into i. So, 1 into 1, 2 into 2. If we run it, then all the squares are printed. Second, I have a way to play with i and j, two variables.

  101. Speaker 1

    So, what can I do? I can declare one more variable. Let's say j equal to 1.

  102. Speaker 2

    See this. I declared i equal to 1 and another variable j equal to 1. Now, you know, if I write int once, I can define both i and j there. So, in the same way, I separated comma and declared two variables. No problem, we can do this. Now, just like I did i + + here, I can also do comma j + +. As soon as i increases, I can increase j too. Now, here the condition is i less than or equal to 10. So, let's say I leave the same condition for now and do i into j in the loop. Something like this. So look, now you check the condition of my loop. First, two variables are declared, i and j. Both start from 1, right? And 1 into 1 happens. So the answer of 1 into 1 will be 1.

  103. Speaker 1

    Then it will go up.

  104. Speaker 2

    Both will be plus plus. Now both will be 2, 2. Then the condition of i will be checked. Because till now i is smaller than 10. So it will be 2 into 2. Then it will go up. So by doing this, our loop will continue.

  105. Speaker 1

    Till when?

  106. Speaker 2

    Till our 10 into 10 becomes 100. So, we have printed this again. Now, what is the benefit of two variables? Now, you will say that we can print 1 into 1, 2 into 2 from one variable. The benefit of two variables is that it is not necessary that I just make j plus plus. Let's say I am making j plus equal to 2. I can write like this that j is increasing by 2 every time. So, what will be i into j? Let's say in the first iteration, it will be 1 into 1 because both will be 1 and 1. In the second iteration, i will be 2. But j must be 3, right?

  107. Speaker 1

    Because after 1, it is greater than 2. So, it will be 2x3.

  108. Speaker 2

    In the third iteration, i will be 3. And this 3 will be greater than 2, i.e. 5. In the fourth iteration, it will be 4. But it will be greater than 2, so it will be 7. So, the loop will run like this. Let's run it and see if we get the answer like this. And see, 1, 16, 15, 28, 45. Something like this. So, basically, I can also control this. in the loop that I can play with two variables, i and j. In fact, not just two, but comma, k equal to 1, something like this. I can play with three variables too. I can play with any number of variables in my loop and I can increment every variable in my way. So let's say i is plus plus, j is plus equal to 2. Let's say I am saying k into equals to 2. So what happens every time? k is equal to k into 2. It is increasing by 2. It is increasing by 2's multiplication. I can do this too. But let's say you want to put a condition on 2 that i is less than or equal to 10 and j should be less than equal to 5, then I can't put the condition comma separately. I can't write it like this, j less than equal to 5. See, here will be an error. What is it saying? Syntax error on token comma. So basically, it is not expecting a comma from us. It is expecting a logical operator from us. So here I can put and. Let's say j less than equal to 5. We had studied logical operators. What does AND mean? If one of these conditions is false, then we will come out of the loop. If I put OR instead of AND, then if both the conditions are false, then we will come out of the loop. Right? So you can also play with these logical operators. Now suppose I put AND on 5, j less than equal to 5. So you know what will happen? i says it can go up to 10. j says it will go up to 5. Who will be false before both? J. Because as soon as J is 6, this condition will be false. As soon as this condition is false, even if it is true, we have mixed both the conditions and it is false. Because there is an end between them. That means we will come out of this loop. Now see, we will have the things printed. They will be printed only till the time J is less than 5. So let me run it quickly. So see, we have only 3 values printed, 1, 6, 15. Why? Because see, the j is dividing by 2. So first j1 will remain, so its answer will be 1 into 1. Then after j1, it will directly become 3. So 3 into 2, the answer will be 6. Now after 3, it will directly become 5. And the value of j is accepted till 5.

  109. Speaker 1

    So our answer will be 15.

  110. Speaker 2

    And as soon as after 5, it will be bigger than 2 and become 7, our condition will fail. And if this fails, then because there is an and in between, so overall this condition will fail. And if both fail, then we will come out of this for loop. Okay, so I hope you understood what comma separated variation is. Now see, let's discuss one more thing here. Till now, all these loops, let's say, if I write the for loop again, i equal to 1, let's say, this time I write i less than equal to 5, i plus plus. We were writing something like this. We were looking at the condition here. It was such a condition of i. That it will keep running until it is smaller than 5.

  111. Speaker 1

    But it is not necessary. I can also put a Boolean condition here.

  112. Speaker 2

    Let's say I have a Boolean variable b. Which is false for now. Let's say it is true for now. And I will write here. Until.

  113. Speaker 1

    B is equal to true.

  114. Speaker 2

    I can write something like this too. So the condition that comes in the middle is a boolean condition. So I am saying that this loop will work till the time the value of b remains true. Now you will say that the value of b will always remain true. But I can also do something like this. That I will put if in the middle and on the basis of a condition I will false the value of b.

  115. Speaker 1

    Let's say here if there is a condition written in this b.

  116. Speaker 2

    I am just telling you that a condition will be written here and on the basis of that condition, I will set B to false.

  117. Speaker 1

    Let's say B is equal to false.

  118. Speaker 2

    And as soon as B is set to false, this loop condition will fail and we will come out of this loop. We can also do this. It is not necessary that our entire condition is an increment condition. That I should be less than 5, I should be less than 10. It means that it can be any condition, any boolean condition. So, as I just wrote here, it can be this kind of condition. And I can false that condition on the basis of any condition inside if, which is the condition of my loop, right? And then I can come out of the loop in that way. So, this is also a way. Now, one question which should come in everyone's mind, if I comment it again, and let's say I pick up this loop for now, And let's uncomment it. Something like this. And for now, it's in two variables, so I'll make it one variable.

  119. Speaker 1

    Till now, we were making our loop like this.

  120. Speaker 2

    And I told you that in Java, the integers that we have, you can represent them in four ways, in four data types. There's byte, short, int, and you have long. But now look, whenever we iterate in the loop, we mostly declare int. You can think that generally, in any real world application, Integer means you have a very high range of 32 bits, so you can go very high in integer. But generally when we are doing loop, then our limitations, which are basically the iterations of the loop, will be covered in a maximum of a byte or short in most of the applications. I am not saying that it will be covered in all. But in most of the applications, the short range, I believe byte is very less, it is up to 127, right? But suppose the short range is very high, many loops should be covered up to that range. So here instead of int i, Why don't we write short i? In fact, if you look at any loop, you will never see short or byte here. You will always like to see integer here, int. Sometimes you will see log, if you know that range will go out of integer. But mostly that range should come in short.

  121. Speaker 1

    But still we don't write short here.

  122. Speaker 2

    Why?

  123. Speaker 1

    This is what we learned when we studied type promotions in java.

  124. Speaker 2

    You know that whenever you use byte and short, java automatically promotes it in the integer. This is the same thing that happens in the case of loop. If you write short here, thinking that I will use less space, even if I know that I have to go up to 10. This will also come in my shot, in fact, this will also come in my byte. I can also write byte here. Right? But even if I write this, from inside, this i will not convert into byte but into integer.

  125. Speaker 1

    This type will be promoted.

  126. Speaker 2

    Right? And after that, if this type is promoted, then it will take as much space as the integer.

  127. Speaker 1

    So why should I write an int from the beginning?

  128. Speaker 2

    Right? That's why you see that most of the loops that will be written in Java, there will always be an int variable declared. Neither byte nor shot. Okay? Now after this, we have our nested loops. Now you know if you can nest in conditions or switch then why not in loops.

  129. Speaker 1

    Now nested loop looks like this.

  130. Speaker 2

    Let's say I made a loop for int i equal to 1, i less than equal to 5, i plus plus. And inside this I wrote another loop. Let's say for int j equal to 1, j smaller than equal to 4, smaller than or equal to 5 and let's say J + + something like this. So you can also nest the loop. In fact, how many versions can you nest in this? One for and one more for and if you want you can write one more for in it. Now again, there can be benefits of nesting the loop. You have to do something like this that you want a loop to run in every iteration of i. Then when the value of i is 2, then again a loop runs. Something like this. So let's go to the notes and see how the iterations of the nested loops work. Because for those who are doing it for the first time, it may not be intuitive to think a little, so let's go to the notes quickly. Yes, so let's see our nested loops. So obviously you know that we can nest all the loops whether it is for loop, while loop or do while loop. But now we will see the example of for loop because if we understand one loop then we will understand the rest too. So let's assume that we are looking at this loop. So how will it work? How will its flow of control work? So let's see one by one. So first of all we will have this i variable. It will iterate for 1. So let's say I am showing its iteration here. After that, it will do iteration for i2, iteration for i3, iteration for i4, and in the end, iteration for i5.

  131. Speaker 1

    And it will be finished.

  132. Speaker 2

    Because only 5 iterations are possible in this loop. And in every iteration of i, let's say i is equal to 1, in this iteration, we will come inside the loop, in this big loop. As we come inside this loop, flow of control will come in this small loop. I want to go to another loop, no problem. There is a new variable j in this. So, this new variable will work for j equal to 1. And it will do something. We have left that empty.

  133. Speaker 1

    I do something. Here we can print something. We can calculate something etc.

  134. Speaker 2

    Etc. How long will this loop last? If you look at this inner loop, how long will this loop last? Until j is from 1 to 5, right? And this is the inner loop, so until this loop doesn't end, we won't go back to the outer loop. So this loop will end first. So how will it happen? j is equal to 1 to j is equal to 5, it will run first. i is equal to 1 only, right? As it runs to 5, This inner loop is finished, then we came out of it, we saw that there is no other statement, that is, we also finished this first iteration Then we went to the second iteration of the outer loop, now the value of i has become 2 Now as the value of i has become 2, we came back into this loop, we saw that there is another inner loop, it will go from 1 to 5, that is, it will go again from j equal to 1 to j equal to 5 Then the condition of the loop will end and we will see that there is nothing else. Then we will go to the outer loop again and now it will run for i equal to 3.

  135. Speaker 1

    And it will continue like this.

  136. Speaker 2

    That means in every iteration of i, our inner loop will run completely. So in i equal to 1, it will run 1 to 5. In i equal to 2, it will run 1 to 5. In 3, again it will run 1 to 5. In 4, again it will run j equal to 1 to 5. Here also it will run j equal to 1 to 5. Something like this. So what I mean to say is that if you have an idea of time complexity and if not then let me tell you. For example, we had 5 numbers here. i equal to 1 to 5. And for every number of i, for every i, our j also runs 5 times. 1 to 5. Right?

  137. Speaker 1

    So if I have understood it correctly, then look at it like this.

  138. Speaker 2

    I goes from 1 to 5 and for every i, j goes from 1 to 5. For every i.

  139. Speaker 1

    What does this mean?

  140. Speaker 2

    Let's say i goes from 5 to n. It's a random number, n. And similarly j goes from 5 to m. It's a random number, m. So for every i, if the value of i is 1 or 2, for every i, How many times will J work? M times. Right?

  141. Speaker 1

    For this also, how many times will J work? M times. And how many times will I work like this?

  142. Speaker 2

    N times. Right? And for each of them, J will work as M times. So, total, how many times did this loop run? How many times did this complete loop run? This runs as n into m times. Right? So here also, you see what n is. Here n is 5 and m is 5. In this case, n is equal to m. So we can say that our loop ran n square times. What is this? I have written this as order of n square. This is a way to find out the time complexity. This is a different topic. But you might have got a little idea. That time complexity means that we are finding out how many times this loop ran. In this case, it ran n into n times. Means 5 into 5 times. So total 25 iterations took place. 5 iterations of its big loop. And for each iteration of the big loop, 5 inner iterations. I hope you have understood this. Now if you know that there is another inner loop here. Let's say i, j, k equal to 1 to k less than 5, k plus plus. Something like this. So you can find out how many times i, j, k have gone together. They have gone n into n into n times. Which we also call order of n cube. I hope you have understood what is time complexity. It is a different topic. But for now, you understand how nested loop works. Let's see its code example. We have made a nested loop like this. Now let's say we use nested loop to print patterns. What are the patterns? Let's say I want to show a pattern in the output of Java. Let's say a star is printed. I will show it like this. This is how a triangle is formed. I want to print a pattern like this on the screen. In fact, pattern printing problems are very interesting problems to practice loops. And in the last part of this video, if you go to the description, then I have given you the notes, there are also pattern printing problems for you. So you can practice them for loops. So let's first try to print this pattern using two loops. Now what is the simple logic of this? I will tell you by doing one. And you can do any other pattern. It's just a game of practice. I will remove the inner loop. Let's keep the outer loop for now. Now we have two loops. The outer loop should always work by looking at the number of lines. So let's say how many times do we have to print? Let's assume that our pattern starts from here and ends here. Ignore this star and this star because we have put it for multi-line comment. Let's assume that our pattern starts from here and ends here. So how long do we have to go? Once, twice, three times, four times, five times. Means we have to go up to 5 rows. So this loop should run 5 times. Which is running from 1 to 5. The work of this outer loop is to see the lines. To print every line. So first line, second line, then third line, then fourth line and in the end fifth line. Something like this. Its work is to print 5 lines. Now, in every line, we have to print number of starts. So, first time we will do 1 star, second time 2, third time 3, fourth time 4, and fifth time we will print 5 starts. So, we can write this too. So, let's make an inner loop for, here we will put the variable j, and j will be 1.

  143. Speaker 1

    Now, see, at least 1 start.

  144. Speaker 2

    Should be printed. Now, here I can't simply write j less than 5, less than equal to 5 j plus plus. Because this means that I have to print 5 stars every time. Because see, it will work for every i value 5 times. We had seen this before. So it will be like this that it will print 5 stars here too. 2, 3, 4, 5. And it will print 5 stars in the next line too. We don't want any such pattern. We want something like this. That first line should have 1 print. Second line should have 2. Third line should have 3. So how many times should this work? What is the condition of this? Think about it. So I will tell you like this. First time it should work once. First time means when i is equal to 1. So this inner loop should work once. When i is equal i is equal to 3, i is equal to 3 means you went to the third row. In the third row, the inner loop should run three times, once, twice, three times. It will start three times.

  145. Speaker 1

    So basically it should depend on the upper loop.

  146. Speaker 2

    So I can't write that j should be smaller than or equal to i. So if i is equal to 1, then j will run only once. It will start from 1 and will stop at 1. Similarly, when i is equal to 5, the inner loop will be less than or equal to 5.

  147. Speaker 1

    And every time we will try to print.

  148. Speaker 2

    System.out.println. Let's say I printed a star like this. And I'll give it a space too. Now listen to one more thing. Whenever we print ln, ln means that it automatically comes to a new line. Now as soon as I printed a star, our output on the terminal will come to a new line. So it will do something like this. I printed a star and it came to a new line. Sorry. But we don't want this. We want multiple stars to be printed in one line. So, one star in the first line and two stars in the second line. So, instead of println, we write print only. System.out.print. Now see, what will happen till now? Let's run it once. The first time our I will be the value. I keep it in the comments. When i is equal to 1, the inner loop will run only once because j is equal to 1. As soon as j is equal to 1, we will see if 1 is smaller than 1 because i is also 1. So we will print a star and a space. The loop is finished. Next time, we will run for j is equal to 2. So, it will come out of this loop. It came out of inner loop. Now, as it came out of inner loop, there is nothing to do here. So, it went back to outer loop. Now, I's value is 2. Now, I's value is 2.

  149. Speaker 1

    Now, what will happen?

  150. Speaker 2

    It will print star twice on this line. Because, see, now J will work twice. Once, For j equal to 1, it will print a star. This star will print a space. And then it will go for j equal to 2 because j is still smaller or equal to i. Because the value of i here is 2. So 2 is less than equal to 2. So it will print 2 stars here. But it will do everything in the same line. Because we have written system.out.print. We are not changing the line even once. But what should we have done? By printing 1 star, we should have changed the line like this. So what can we do for that? As soon as this inner loop ends, We will also print out a system.out.println. Println means printing a new line. Basically going to a new line. Something like this. So every time when our inner loop ends, we will change the line. I have left this empty because I don't want to print anything else. I just want to print an empty line. So as soon as our inner loop ends, we will go to a new line. We will have to print 2 times and then we will move to a new line. Then we will come here. Then for i equal to 3, the inner loop will run 3 times. So once, then twice, then three times. As soon as it runs three times, we will come out of this loop and print a new line. So we printed a new line. Something like this. So once, twice, thrice, now on the new line, next time i is equal to 4, so 4 stars should be printed here. So we will print 1, 2, 3, 4 because of this loop. And we will come out of this loop and print a new line. Means we will come here, something like this. As it is here, then it will work 5 times in the end. And we will print 1, 2, 3, 4, 5, something like this, 5 stars and finally come to the new line. Something like this And as soon as we come to the new line, the value of i will be 6 6 is not less than or equal to 5, we will also come out of the outer loop So this pattern should be printed So let's do it once, let's remove it Okay, let's run it once Look, this pattern has been printed Now I have run this pattern 5 times If you do this pattern 10 times, then see how big this pattern will look.

  151. Speaker 1

    See this. Now I can do 15 or.

  152. Speaker 2

    20 as well. So it will keep on increasing. So I hope you enjoyed this. This is called pattern printing problem. It has nested loops. Sometimes you can even have 3 loops. So that's a matter of practice. You will have to practice with loops. I will give you practice problems. They will be uploaded on GitHub. You should go and practice those notes. You will enjoy it a lot. First, I will give something easy. After that, we will increase the complications a little bit. So that all the pattern printing problems are cleared. So this is a very good way to improve your loops. Now look, after finishing all this, we have one last thing left. Which we call jump statements in Java. Jump statements in Java. What are jump statements? Till now, we were either doing selection statements, or we were.

  153. Speaker 1

    Playing with loops.

  154. Speaker 2

    Now we have jump statements in Java. There are two types of jump statements. One is break in Java, and the other is continue. Continue, something like this. Now, you had seen the break in the switch. Do you remember when we say that after every case, you have to write break? Otherwise, our switch will execute the next case as well. So, we will see the break once again that the use of break is not only in the switch. In fact, you can use this jump statement anywhere. Same, we have this continue, you can use this anywhere. But the best use comes in its loops. How? Let's see. So, before that, we will do this comment. So that it does not hinder us to write anything. Everything is commented. So from here we can start our new code. So let's say I write a loop again. I equal to 1. I less than equal to 10. And I plus plus. Something like this. I wrote a loop. And every time let's say system.out.println. I try to print I. I will try to print i and my answer will be 1 to 10 which we were doing till now. But let's say for some reason, I want to get out of this loop as soon as i is 5. We will run the loop for 6, 7, 8, 9, 10. Okay. So you will say. It is very simple. Here instead of 10.

  155. Speaker 1

    Do 5.

  156. Speaker 2

    Right. Absolutely. It is simple.

  157. Speaker 1

    But I don't want to tease this.

  158. Speaker 2

    I say.

  159. Speaker 1

    Let this run.

  160. Speaker 2

    Okay. But from inside. I want. In some way. I don't let this loop. Run after 5. Now why I will want this. There can be many reasons. So as I said. I want. That this loop. Shouldn't run after 5. So how can I handle it from inside. So let's see. So I am saying.

  161. Speaker 1

    If I write something like this.

  162. Speaker 2

    If. I is.

  163. Speaker 1

    Greater than equal to 5. Or equal to not. Simple i is greater than 5.

  164. Speaker 2

    If i's value is greater than 5. If i's value is greater than 5. Then I wrote simple break.

  165. Speaker 1

    See.

  166. Speaker 2

    Now what will the break do? Think about it. First the value of i will be 1, we will print 1. Is the value of i greater than 5? No. So we will skip it and we will exit from the loop here. Means we will exit from this iteration. Right? Then the value of i will be 2, the loop will run for 2. Sorry. The loop will run for 2, we will print the i, not the 2. So it will check again, is the value of 2 greater than 5? Not at all. So we will skip it again, the iteration is over. Then it will run for 3, we will print 3. It will run for 4, we will print 4, we will skip it. 5 will print, sorry 5 will print, again 5 is not greater than 5 because it is equal to 5 So we will skip this again, now as soon as it will go for 6 We will print 6 first, but then we will see that 6 is greater than 5 So we will break, break means come out of this loop and stand here And don't run any further iteration, this is what break means This means that the output we will get will only come from 1 to 6 will not come in 7, 8, 9, 10 output. So, let's run this once. See, 1, 2, 3, 4, 5, 6 and after 6, our loop is broken. Now you will say, what is its use? If we had to run it till 6, then I would have written less than or equal to 6. But now think, I have broken it according to i. But there might be some condition outside. Let's say there is a boolean variable outside, boolean b equal to false. And according to this variable, I would say that if B is equal equal true. If B becomes true at any time, if B becomes true for any reason, then you break this loop. And this B becomes true under some other condition, right? When we will be writing this program, you will understand. It has a lot of use of break, that if you want, you exit from any loop by deciding dynamically. By deciding dynamically means, we said if B is equal equal true, this B variable, if it becomes true for any reason, then I don't want to go further in this loop, right? So you can break it. So this is a very good use case. Now I can show you a simple use of this. For example, let me comment on this first. Let's say we are creating a program which will tell whether a number is prime or not.

  167. Speaker 1

    We can write a very simple code.

  168. Speaker 2

    So let's say I give you a number int p and I call that number 9. For now, we will change 9 later because you know that 9 is not a prime number. But if you want to know whether your code is a prime number or not, then how will you do it? So if I want to know whether a number is a prime number or not, then what can I do? I can first divide it by 1, then by 2, then by 3. I don't have to do it from 1, because it will happen from 1. I can do it from 2, 3, 4, 5, and keep going till that number. So I can go till 9. Or till 1 before 9, because it will happen from 9 too. I try to divide it from every number from 2 to 8. And if it gets divided from anyone in between, then I will say that it is not a prime number. But if I don't get divided by anyone, then I will say that this is prime number. Right? This is the right logic. So, if we apply this logic, then we will get loops. So, let's do one thing. Let's write a loop. For int i equal to 2, i less than equal to whatever our number is. So, p, which is our number, will go down by 1. So, either you write i less than equal to p minus 1, or you write i less than p. So, it will stop before i. And every time we will keep doing i plus plus. So, first i will go for 2, then 3, then 4, then 5, sorry, then 4, then 5, and by doing this it will go till 8. And every time we will try to divide this number p, i.e. try to find its remainder using i. And I can write it like this, if p modulo i....

  169. Speaker 1

    Is equal equal zero?

  170. Speaker 2

    That is, if I ever divide this P number 9 with I, then first I will try to divide it with 2, then with 3, then with 4.

  171. Speaker 1

    In any case, am I getting remainder 0?

  172. Speaker 2

    If I am getting it, you know, if this condition becomes true, then it means that number is not prime, right? Because that number can be divided. Like this number will be divided by 3. If I divide 9 with 3, then 3, 3 is 9. Remainder will be 0. So in that case, when the value of i is 3, we will enter in this if and we can print in this if. Let's say system.out.println. What can we print? The number is The number is not prime. We can print something like this. And if we assume that p modulo i is never true. Let's say the number is really prime. Let's write 7 here. So 7 is prime. And when will the loop of 7 run? It will run from 2 to 7. Sorry, it will only run till 6. 7-1, the loop runs till p-1, so we'll divide by 2, 7, remainder will come, 1 will come, we'll divide by 3, 7, again remainder will come, we'll divide by 4, 7, again remainder will come, never remainder will be 0, look, you divide by 5, look by 6, You will always get some remainder. In 5, you will get 2. In 6, you will get 1. Something like this. How much will you get in 4? You will get 3 in 4. How much will you get in 3? You will get 1 in 3. You will get 1 in 2. Something like this. You will always get some remainder. You will never get 0. That means we will never enter in this if. And we will never enter in this if from 2 to 6. As soon as this loop ends, we will come out of this loop. Here. We will come out of this loop. If we come out of this loop, it means our number is prime. Because it is not divided by anyone. So how can we do this? You think. In the previous case, when our number was 9. We must have run the loop. 2 is equal to 0? No, because remainder is left. So, we won't go into this if. This if is finished. In the last iteration of 4, we will go up again. Now, i is equal to 3. It will work for 3. Is 9 modulo 3 equal to 0? Yes, it is. Now, if it is, we will print the number is not prime. This number is not prime because 9 is being divided by 3. If 9 is being divided by 3, do we need to check further? 4, 5, 6.

  173. Speaker 1

    Do we need to check?

  174. Speaker 2

    No, we don't. If we don't need to check, then can't we write a simple break here? Break means come out of this loop. Now we don't need to move this loop forward. So we dynamically decided that if the number is divided, if p modulo i is 0, then I don't need to move this loop forward. I can break from here and come out of this loop. But if it never becomes 0, means this condition never becomes true, for example in the case of 7, then we will never enter this if. If we never enter this if, then it will complete its iterations. We will not break this loop even once. And we will come here and stand. and here we can write that if i is equal to p What does it mean? Now see, i is complete. It worked for 2, 3, 4. How long did it work? Until i was less than p. Now suppose i is working for 6. So is 6 less than p? 6 is less than 7?

  175. Speaker 1

    Absolutely.

  176. Speaker 2

    It will work for 6. It will not enter in this because 7 modulo 6 is not 0. Now it will go up again. Now i's value will be 7. Now as soon as it will try to work for 7, it will check whether 7 is less than 7.

  177. Speaker 1

    Absolutely not.

  178. Speaker 2

    So we will come out of this loop. As soon as we come out of this loop, we will see that the value of i has reached p.

  179. Speaker 1

    That means the value of i has reached 7.

  180. Speaker 2

    That means now we can print system.out.println that p is prime. We can write the number is prime. Because it is not divided by anyone. Println will come here. So this means that it is not divided by anyone. It went from 2 to 7. It is not divided by anyone. Because if it was divided by anyone, then it would have gone inside this if. If it would have gone inside this if, then we would have broken it. If we would have broken it, then this loop would have broken somewhere in the middle. This number which is i, It can never reach 7, i.e. it can never reach P. It has reached P only when we haven't gone inside the shift even once. i.e. the loop didn't break even once. This means that this number is our prime. So we can write a code like this. Now you can see that there is an error on this i. And what is the error saying? I cannot be resolved to a variable. Basically, it means that i is not able to identify this as a variable. Why is it not able to identify? Now the reason for this is called the scope of a variable. Scope of variable means, I will read it in depth later, but for now you understand that the variable I declared, I declared it in this loop only, here it is written int I. So this means, when I am trying to use this I outside this loop, then it is not getting I. Because in this loop, means in this block, the variable I declared, its access will be till that block only. If I want to use this variable here, So I have to declare it outside, something like this, int i. I have to write int i here. And here I will just define i. So I have to do something like this. Something like this. Now as soon as I wrote this, this error went away. Now what is the scope of a variable? We will read it in depth later. But for now, we have to do this work. Now let's run this program. First for p equal to 7, our print should be the number is prime. Okay. Look, the number is prime.

  181. Speaker 1

    Very good.

  182. Speaker 2

    Now, let's run it once for 9. Our print should be number is not prime. The number is not prime. Absolutely right. So, see, this way we used our break. That when we have to exit from the loop and we wrote a prime number method. Now, obviously, this is not a very optimized solution. We can further optimize it. If you go to its algorithm, you will know that we should run this loop not i less than p but i less than root p.

  183. Speaker 1

    But again, that.

  184. Speaker 2

    Is a different topic. But now I just wanted to explain you break. So, I hope you understood. Now let's move ahead. The next thing we had apart from break was continue. Now what is the use of continue? So let's say I have again written a method i equal to 1 to i less than equal to 10 and i plus plus something like this. Like this. And every time I write again system dot out dot println. Let's say I print i. I can do this. And like every time, what will be printed here? 1 to 10 numbers will be printed. Now think, I want that 1 to 10 numbers are not printed. Only odd numbers are printed. So odd numbers mean 1, 3, 5, 7, 9. Print like this. So what can I do? Very simple.

  185. Speaker 1

    You must be thinking. I can write an if condition.

  186. Speaker 2

    If. If I modulate this number from 2 and if it is not equal to 0, then it means our number is odd. Think about it, any number which is not divided by 2 is odd. So if i modulo 2 is not equal to 0, then that number is odd. If the number is odd, in that case if I print it, And if the number is not odd, then I won't do anything. So if we don't enter in this if, then our loop will end, i.e. iteration will end and we will go up again. So which number will enter in this loop? One will enter, three will enter, five, seven and nine will enter. Rest two, four, six and eight will not enter in this if and they will simply escape. So our odd number will be printed 1, 3, 5, 7, 9. Something like this. Very good. Now, I want to do the same thing that I just print odd numbers but in a different way.

  187. Speaker 1

    So, what can I do?

  188. Speaker 2

    You think like this, I will write that you do something simple. System.out.println, you print i every time. But before that, test one thing that if i modulo 2 is equal to 0, This time I am checking equal equal to 0, means is the number even? If the number is even, then I have written continue. So what does continue mean? If the number is even, then continue this loop from the top again. There is no need to go inside it, there is no need to go below it. So this means, let's say the first loop will run for equal to 1. We will check if 1 modulo 2 is equal to 0.

  189. Speaker 1

    Not at all.

  190. Speaker 2

    1 is an odd number, so we will not go in this if, we will simply print 1. Then this loop is finished. Now this time i equals to 2 will work. We will come to this if. We will check. Is 2 modulo 2 equal to 0?

  191. Speaker 1

    Absolutely.

  192. Speaker 2

    As soon as we checked 2 modulo 2, we entered this if. We said continue. What does continue mean? Continue means to take the flow of control again to the starting of the loop. Take it to the next iteration. We don't need to go further. We don't need to implement the things written below continue. So as we saw continue, we went up again. We didn't print it. Now we went to i equal to 3. We went to 3, so this condition will fail. We printed 3. As we printed 3, the loop got over. Now we will go to 4. We will go to 4, this condition will be true. Because 4 modulo to 0 happens. Again continue, we went up again, now it.

  193. Speaker 1

    Will go for 5.

  194. Speaker 2

    So basically what will happen, we will keep skipping every even number. So as soon as there is an even number, we enter in this if and we call it continue. Continue means go up again, don't execute below. So in this way also we will print all our odd numbers, 1, 3, 5, 7, 9. So I hope you understood how we can use break and continue. What does break do? Break brings any flow of control out of the loop. That there is no need to execute further, come out of the loop. And what does continue do? Continue tells the flow of control to keep this iteration up to here, now let's run the next iteration. So if 3 was running, then 3 is equal to 0, no, let's assume 4 was running, then 4 is equal to 0. So as soon as it is, we said continue, that now the next iteration will run, it ended up here. 4th iteration is finished, now we don't need to go down. So for 5th iteration, we will run it again. So continue flow of control is sent in the starting of the loop and break finishes the loop. So this is our break and continue. Now again, you can use break and continue in a nested loop as well. How can we do that? We can do it like this. Let me write here break in Nested loops. So see, what I do is I take my pattern code. Where is my pattern code? Yes, it is up here. Okay.

  195. Speaker 1

    Where I was printing a pattern.

  196. Speaker 2

    Here. And let's uncomment it. Yes. So see, here I was printing a pattern of 10 from these 4. If we run it again, then we were printing a pattern of this type. But you think, I say that I normally put a break here. Let's say that if j is greater than or equal to 5.

  197. Speaker 1

    So I wrote break. So what will happen? You think what will be its output?

  198. Speaker 2

    I said that as soon as the value of this j becomes greater than 5. So obviously when will it be greater than 5? When it will be after the 5th line. Because how does our pattern look? Our pattern looks something like this. In the first line, 1 star is printed. In the next line, star, comma, star is printed. And in the next line, star, comma, star, comma, star, 3 are printed. So now the inner loop is running J 3 times, so it won't enter it. Then in the next line, 4 times will be printed. Something like this. So this time J's value went to 4, it won't enter it yet.

  199. Speaker 1

    Next time 5 will be printed. Something like this.

  200. Speaker 2

    So this time J's value went to 5. Now as soon as J's value went to 5, we said break. Okay?

  201. Speaker 1

    Now see, there are two loops here. One for loop outer and one inner.

  202. Speaker 2

    And the break is written in the inner one. So, from which loop will we break? We will always break from the loop in which we have written the break. So, see, where is this break written? In this loop? So, it means that we want to come out of this loop. From this loop. Coming out of this loop means that we will come out of it and stand. Okay? So, break means that break this loop and come out. But this big loop, which is the outer loop, is still running. Okay? So, now see what will happen. It has been printed till here. Now the next time the loop will run for 6. So for 6, we first printed 1, then 2, 3, 4, 5. Now as soon as j is equal to 5, we said break. We will break again from here. That means we will come out of this loop and stand here. And as soon as we come out of this loop, we will say line change. We will print line change something like this. And after 5, we will not print the 6th one. And next time again, for which i will work? 1, 2, 3, 4, 5, 6. Now i will work for 7. So for 7 also, how many times should it work? 7 times should work logically. But it will work only 5 times. 1, 2, 3, 4, 5. Now as soon as this condition becomes true, we will say break. We will come out of this again. Because what does break mean? We came out of the inner loop. And as soon as we come out of the inner loop, what do we see? We change the line. And then again we execute the outer loop. This time it will run for 8 again. 1, 2, 3, 4, 5. It will not be able to go beyond 5 because we will break. And we will come out. So basically this right angle triangle that was being made, it will only be till 5, after that it will become common, it will become constant, it will not be able to go ahead. So, we will have a pattern like this. If you want, we will run it and see.

  203. Speaker 1

    So, see this.

  204. Speaker 2

    This 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, and 1.

  205. Speaker 1

    2, 3, 4, it ran till 5.

  206. Speaker 2

    But after that, see, 6, 7, 8, 9, 10 became common for everyone. It could not go ahead of 5. Because see, as soon as j is equal to 5, after this, every time after 5, we break from the loop. we break from inner loop. And what was the work of inner loop? To print a complete row. Do you remember? To print a complete row. But why we are not able to do that? Because as soon as jk is equal to 5, we come out of this inner loop. We break. Okay? So I hope you must have understood that how we can use break in nested loop. Okay? Now there are some benefits of this, sometimes we will need to use the break in the nested loop, then it will be very useful for us. In the same way, you can use continue in the nested loop. If you write continue here, then you can imagine how the diagram will be made. I want you to make it yourself once. And do you know what the continue will do? It will bring you back to the starting of this loop and make you stand again. Which loop? The loop in which the continue is written. It will bring you back to the starting of the innermost loop and make you stand again. It will say that if there is anything below the continue, then don't execute it. Go straight to the starting of the loop. With next iteration. Now we will do next iteration. Now one more thing. This is normal break and continue. We have used it. Java gives us another thing which we call labels. What does labels mean? You can give a name to this outer loop and this inner loop. You can give an identifier. So let's say I can call this outer and this inner. So let's say if I copy this and let's comment it here. And here we paste it like this. So now I will give an outer loop a name outer. So I have written outer, colon and space. So I have given it a name outer. space, and I named it inner. Now this is its syntax, that whatever name you want to name it, and after that, put colon and space. Similarly, I want to name it something, put colon and space. So, its name is outer, its name is inner. Now, when I am breaking or continuing here, break or continue, and I want, if I simply write break, you know, we exit from this loop, right? We exit from inner loop, but I want, that here, I exit from outer loop, so I can write break outer, Break outer is something like this. If I write break outer, then as soon as j is greater than 5, I will exit from this outer loop only. So now think what will be your pattern? Your pattern will be in this case, if we try to understand. So see, first of all, the value of i will be 1. And the value of j will be 1 to 1. So we will print 1 star something like this. We will not enter this condition because j is not greater than 5. The value of i will be 2. We will print 2 stars.

  207. Speaker 1

    There is no problem.

  208. Speaker 2

    There is no problem for 3 and there is no problem for 4 either. The problem will come for 5. As soon as the value of i becomes 5. As soon as i is 5, how many stars do we have to print? 5 times. So it will run for the first time.

  209. Speaker 1

    1 time, no problem.

  210. Speaker 2

    2 times, no problem.

  211. Speaker 1

    3 times, no problem.

  212. Speaker 2

    4 times, no problem. As soon as it will be running for the 5th time, first it will print star.

  213. Speaker 1

    It printed star 5 times.

  214. Speaker 2

    After that, in this condition, is j's value equal to or greater than 5? The answer is true. So we will do break outer. So we will say, come out of the outer loop. That means, come here and stand. Sorry, come here and stand. So as soon as we stood here, we both came out of the loop and our final pattern became this. After this, we will not go any further, that is, our pattern will only go up to 5. So if we run this once, see, our pattern is over after 5. So, if you want to break towards outer loop, or if you have 3-4 nested loops, and you have named them as 1st loop, 2nd loop, 3rd loop, and you want to exit from any loop, then you just write break and write the name of that loop. And in fact, we can also use continue here. Continue outer. Now you can do this too. Whatever the output of this is, imagine what should happen.

  215. Speaker 1

    In this form, right? So, I hope you must have understood these stories.

  216. Speaker 2

    Okay?

  217. Speaker 1

    When do you use break and continue?

  218. Speaker 2

    And you can also take the names of all these. Outer, inner, or first loop, second loop. And you can break or continue from any loop. Okay? In fact, if I tell you the right way, then you... What I said was, what is a code block? Whatever comes under a curly bracket is a code block. In Java, without any loop or without any if statement, you can simply make a code block. And you can also name it. So let's say I gave it a name. First. Okay.

  219. Speaker 1

    And in this, I make another code block.

  220. Speaker 2

    Second.

  221. Speaker 1

    Something like this. In this, let's say I make another code block.

  222. Speaker 2

    Third. Something like this. See this. I can also do this, and let's say I don't do anything inside, system.out.println, what I do here, I print hello, assume like this, okay, for once I do one thing, I comment this first, so that it doesn't give us any problem, okay, and here I write, what am I doing, I am writing code blocks, So this is also a valid syntax in java that I have started a code block and its name is first, second and third and I have nested them. And this name is optional, I can keep the name if I want and I can't keep the name if I want. And I have simply printed hello from the inside. So what will be our flow of control? So first of all, our flow of control will come in this code block. After that, it will see the second code block, then it will go into the second one, into this one. It will see the third one in it, then it will go into the third one, into this pink one. And as soon as it went into this, it printed hello. And let's say I wrote here, break first. So what does this mean? I came out from first and I stood here. So these are some code blocks. If we run this too, our hello should be printed simply. Yes, absolutely simple. So in Java, you can make code blocks without any for or if statements.

  223. Speaker 1

    Although, it's not purposely used.

  224. Speaker 2

    Sometimes, if you want to separate a particular code, then you write it in code blocks. But generally, it doesn't have any benefit. So generally, we use this thing with labels. Basically, we can name loops as outer loop, inner loop, or first loop, second loop. And we can continue or break according to us. Any loop, outer or inner. Okay? So, here all our flow of control statements end. We saw selections, iterations and jump statements. I hope you enjoyed it. It was a long lecture. But I want you to practice it. If you are new to loops, then you should practice it. Print all the pattern printing problems one by one. In that, your continue break will be used a lot. Nested loops will be used a lot. So, with this, your brainstorming will be very good. And secondly, you will master in loops. See you in the next video. Till then, thank you so much.