hello friends and welcome back in this lecture we will talk about the break and continue keywords in Java here is our outline we will talk about the break keyword and the continuing keyword and we will see some examples so let's get started break and continue our special keywords that can be used within loops alright so whenever we use the break keyword inside a loop this means that we want to exit the loop regardless of the condition even if this loop is an infinite loop when we use the break keyword we will exit the loop and we will stop executing its body all right now what about the continue keyword when we use the continue keyword we will skip the rest of the loops body in other words we will continue executing the loop as if we finished executing the body so suppose that we use continue inside a for loop so when the continue statement is executed then we will go and execute the change part of the for loop and after that will check the condition if the condition is true we will execute the body and if it is false we will exit the loop and if you use the continue keyword inside a while loop or a do-while loop then we will check the condition so the condition is true we will execute the body of the loop if the condition is false we will exit the loop all right now let's see some examples over here I am using a for loop so I'm saying for int I equal 1 as long as I is less than or equal to 10 and I plus plus alright so this loop runs ten times and inside the loop I'm testing if I is an even number so if I is an even number as you can see over here I'm executing continue so in other words if I is an even number I will stop executing the body of the loop and I will go and execute that change and after that we will continue executing the for loop normally alright now what if I is an odd number so this condition over here will be false right so this continue statement will not be executed so we are going to continue executing this code over here so this print statement will be executed so we are going to print I plus space right so this code over here prints the odd numbers let's go over this code in details I starts at 1 1 is less than or equal to 10 this is true so now we would execute the body is 1 an even number no this is false 1 is an odd number so the continual I executed and you are going to print one with a space now as you can see the body of the for loop is finished so we're going to execute that change over here we are going to increment I so now I is equal to 2 is 2 less than or equal to 10 yes this is true so now we are going to execute this if statement is to an even number yes it is so we are going to execute continue so we will skip the rest of the body and we will go ahead and execute the change so 2 will not be printed so now we want to increment I so I is now equal to 3 is 3 less than or equal to 10 yes it is so this condition over here is false and after that we will print 3 with a space and then increment I now I is equal to 4 this condition is true and also four is an even number so we'll execute continue so we'll go to the change and continue executing the loop so the continue keyword inside the for loop skips the rest of the body and takes me to the change all right now let's see another example over here I declared a variable an outside the while loop and as you can see I'm saying while true so this is an infinite loop so even if this is an infinite loop right now there is a way in order to exit this loop and it is by using the break keyword right so have a look over here I'm printing enter a number between 1 and 10 and after that I'm reading the input from the user and storing it inside an alright now suppose that I want to keep reading an input from the user as long as the number n is not between 1 and 10 so look what I'm going to do I will say if n is less than 1 or if n is greater than 10 so if n is not between 1 and 10 I want to continue so what's going to happen when this continue is executed we will go and check the condition in this case the condition is always true so we are going to reiax acute this statement so we'll ask the user to enter another number and then we will read the number from the user and stored inside N and as long as n is not between 1 and 10 we'll keep executing continue andrey asking the user to enter another input all right now suppose that n is between 1 and 10 so this means that this condition is false so the continue will not be executed so we will continue executing the code that is over here so we can print for example and is between 1 and 10 so now what's going to happen as you can see the while loop ends over here so we're going to recheck the condition in this case it is true and then we are going to ask the user to and another number but obviously we don't want to do this because they use an n-type the number between one and ten so over here we can use the break keyword to exit the loop so as long as the user doesn't enter a number between one and ten we will keep executing continue and when the user enters a number between one and ten we will break out from the loop now you might be asking why did I print this over here why didn't I print it outside the loop so I did this because I know that this statement will be executed only once because I know that I'm going to break outside the loop and also I did this because I know whenever we reach this point over here and is a number between one and ten all right and of course you can write the code like this when they use that enters a number between one and ten this break would be executed so we will exit the loop and after that we are going to print and is between one and ten but what's important over here is that the variable an is declared outside the loop so because it is declared outside the loop we can access it over here all right but if an is declared inside the loop for example like this this statement will give us an error so as you can see over here we are declaring the variable end and initializing it to be equal to s dot next int so the variable n is only available inside the loop we cannot use it outside the loop just like when we declare a variable I inside the for loop all right so now to be able to access and outside the loop we have to declare it outside the loop like this so this is it thanks for watching and I'll see you in the next video [Applause] you [Music]