Hello and welcome back. This is our fourth video of the series flow control in Python. In this video we will be discussing break statement two programs related to break statement in which break statement will be used and the logic of those programs. I hope you will have a good time watching this video. Thank you That's called What does a loop loop iterate over a block of code until test expression is false. So, it is going to continue iterating over a block of code until the test expression becomes false.
We discussed for loop and while loop and we also discussed by as statement just after or that that is the part of the for loop and while loop we discuss few programs within the for loop and while loop. Now, how does the break statement works? There are a lot of cases when we need to terminate iteration of the block of code, whether the loop or it doesn't need to exit the loop and then that time we use break and continue statements. These are very handy during these circumstances, big statement transfers controls of the program to statement exactly after the loop. What is the syntax of break see this when the code enters the loop it test expression and if the expression is true, it checks. Now, there could be a break statement there if there is a break statement it exits the loop.
The practice break statement is not there, it continues the it continues iterating over the body of the loop. Now generally breaks are within if condition so when the condition becomes true, it the goop enters the body of f and they're fine to break, maybe some other code and alongside break And then it exits the iteration of the loop or it exits the whole body of the loop. So, I hope this is clear. Let's see an example. This is the Jupiter new notebook you are very used to have your head since we have already made many many videos using the Jupiter notebook here we're using the for loop and this is the variable Well, that is iterating through the object that is a string and we are using an if statement if well, he was to I that the code should break otherwise it should continue treating the for loop.
So once the for loop started prints s 10 it prints ci because if condition is not getting true, so it's not entering the body of if statement, then it prints r but when it checks high, then it becomes equal and the comparative sign shows that they are both same so therefore it breaks minute breaks, it just prints the end, like the iteration of our loop start. And this is exactly what the purpose it serves. So I've just already run this. If I run again, the output would be seen. However we can try this with any other thing. Let's try this with the name.
Johnson and B stop it when it is Esther. Let's run it again. So j h o n and it ends because as compared s and when it found the s and while RC stops, okay, there is another practical application of break. This is where we tried to find out if the prime number, a number given to the user is a prime number or not here. We saw that we used to break now the code is breaking up when it's checked and it finds it the remainder is So every time when the code or the system enters the body of fall loop, it checks if the remainder is zero or not. If the remainder is zero, it is great to print it is not a prime number because a prime number cannot have a remainder, zero when we divide it by any of the number except that number.
So for example, the number is 33. And I'm dividing it by two it's starting from two. So 33 divided by two there is some remainder. So it does not enter this if block of code and directly go about treating again 333 divided by three, it finds it the remainder is zero, it enters the body of a, it means that it is not a prime number, it prints out how many times that number is equal to that double by that number. And then it brings it does not include again, who does not go before it just breaks and then comes out of that follow. So that was another example of break.
I hope you enjoyed the video. May you've been question feel free to contact me. And don't forget to subscribe and like today's videos and thank you