Python Tutorial for Beginners 19 - Python break, continue statement

hey guys welcome to the next blog on Python tutorial for beginners in this blog I'm going to show you how you can use break and continue key words with your Python loops so to start with I have here two loops one is a for loop and other is a while loop now this for loop takes a list and then print every element out of this list using this print function in a similar way while loop evaluates the value of I if it's less than five then it's going to print the value of I and then increment the value of I by one now this is all normal and we have already seen this kind of code now you can use a break keyword with your loops in order to terminate the execution of loop immediately so let's see how we can use break keyword first and then we will see how to use continue keyword so here I'm going to give a condition inside my for loop and I'm going to just evaluate whether X is equal to 3 or not and if X is equal to 3 I'm going to use this break keyword in a same way I'm going to use the safe condition inside my while loop also but instead this time I'm going to evaluate I is equal to 3 and F I is equal to 3 then I'm going to use this break inside this if condition so let's run the code and let's see what happens and you can see in the for loop it has only printed 0 1 2 even though this for loop is supposed to print from 0 to 5 it has printed until 2 from 0 because as soon as this x value becomes 3 and we call break here the loop is terminated immediately and our program will come out of the loop immediately as soon as this break keyword is called in the case of this while loop also as soon as this I value becomes 3 we are calling the break keyword and as soon as this break keyword is called or loop is broken and execution of code will come out of this loop so until two everything was okay but as soon as the value becomes three this break is called and then we come out of the loop let me just print a line here in order to separate these two loops so we know that from where one is starting right so I'm going to once again run the code and you can see for loop prints from zero to two and also while loop prints from zero to two if you change the value here for example here you want to evaluate X is equal to two or not and here you want to evaluate if X is equal to four or not and then you break out of the loop and once again when I run the code you can see the for loop runs only two iteration four zero and one and as soon as the value of X becomes two we call the break keyword and we come out of this loop and in the case of while loop as soon as this value of I becomes four we come out of the loop so the value from zero to three is printed now let's see how we can use a continue keyword instead of this break keyword so instead of this break keyword I will use a keyword called continue here and also in the while loop also I'm going to use this keyword called continue and I'm going to run the program and let's see what happens so now when you see here so you can see here for loop starts from printing zero one and as soon as the value of x becomes two this condition is met and this continue is called and as soon as this continue keyword is called everything whatever code comes after this continue keyword will be skipped and your program execution goes once again to a for loop for the next value so you can see in case where X is equal to two and continuing is called this statement is not executed because as soon as we execute continue in our loop then that iteration is skipped and all the code after the continue will not be executed okay so we go to the next iteration so that's why two is not printed because this was kept in case of two and then directly three is printed and then four and five is printed in case of while loop also as soon as the value of I becomes four you can see four is not printed here you can also change this value to two and then I'm going to run the program once again and you can see while loop prints zero one and why it prints zero and one so because as soon as the value of I becomes two this continue keyword is called and after that this iteration is skipped and also because this iteration is skipped these two lines of code are not executed and because this line of code is not executed that's why the value of I is not incremented and the value of I always remains two and that's why this becomes infinite loop right so instead of increment the value of I at the last line we can increment the value of I just after this condition check whether I is less than five or not and then we run the code now you can see it prints 1 3 4 & 5 so now what's happening here is initially the value of I is equal to zero and this condition is evaluated and then we first increment the value by 1 and that's why it starts with the printing of 1 by using this print function and as soon as I is equal to 2 this continue is called and that's why here too is not printed because as soon as the value of I becomes 2 this continue is called and every code or every statement after the continue will be skipped for that iteration that's why two is not printed here so in this way you can use break and continue keywords with loop I hope you have enjoyed this blog and I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News