 Hello everyone, my name is Nikhil Gadjum, working as assistant professor in department of computer science and engineering in Valchand Institute of Technology, Sallapur. In today's session, we will see controlling the loop in Python. This is a learning outcome. At the end of this session, students will be able to write a Python program using break, continue and L statement. As we know, there are different types of loops are present in Python. Example is for loop is there and while loop is there. We will see an example, general for loop and while loop. This is pycham id. Suppose I will write a for loop for i in range 1, then 10, this is colon. Suppose this is print i. This is your normal for loop, if you execute, it will execute from initial value 1 to last value 9. So whatever second argument value you have passed, that will be excluded. This is your normal for loop, similarly you can write while loop also. What is break? Again it is a keyword. Break statement in Python terminates the current loop and resumes the execution with the next statement. So break statement is used to break the loop, generally. So control of the program flows to the statement immediately after the body of the loop. So when break statement is executed, the loop will be exhausted or will be stopped and control will be out of that loop, whatever loop is there and it will start executing from the next statement. If the break statement is inside the next loop, loop inside another loop, the break statement will terminate innermost loop. If there is a nested loop is there, for loop inside for loop or something like that and if break statement is present inside the innermost loop, in that case control will be come out of that innermost loop only. All the for loops or all the nested loops will not be terminated, only the innermost loop will be terminated inside the if the break statement is inside the innermost loop. We will write the same code. Now instead of print, I will write another condition called as if, if I double equal to phi, then break, otherwise else it is print. We will check the output, execute this file. So output is 1, 2, 3, 4. Initially execution starts from I value that is 1. So 1 will be printed, initially value of I is 1, I double equal to phi, 1 double equal to phi will be false. So it will execute else part. So it will be else part is print. So it will print 1, then incremented value of I, I values 2, again it will be printed, I value will be 3, it will be printed, I value will be 4, it will be printed and when I value will become phi, then phi equal to phi that condition is true. So break statement will be executed and control will be come out of this complete for loop. If another statement is there, suppose this is outside for loop, so that will be executed after the break statement. If you check this, so I will be running from I to 1 to 4 and then when I value will reaches to phi, so this condition will be true and control will be out of this for loop and this statement will be printed. So this is about break statement, which will terminate the in almost loop. Similarly, break statement will be there inside a while loop also, this is an example of break. So check out this example for val in print, so print is string in this case. If val double equal to I, then break, otherwise print val, print the end. So what will be the output, initially val value is P, then print it will be printed P, then next time val value will be R, it will be printed R, then val value will be I, if val or I double equal to I condition is true, so break will be executed after break, it will be come out of this for loop and the value will be printed as the end. So output for this program is P, R and the end. Next statement is continuous statement, it is opposite of generally opposite of break statement. So continuous statement in python returns the control to the beginning of while loop or any loop, not while loop, any loop. The continuous statement rejects all the statements in the current iteration of the loop and moves to control back to the top of the loop. Control statement is opposite of that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop. Now in break, it will break the execution of the current loop. In continue, if continuous statement is executed, so all the statements after continue will be skipped and control will again move back to the loop. You will see an example, same example I will use for I in range 0 to I, if I double equal to 5, so I will write continuous statement. Else print. If I remove this else, doesn't matter, this is print, execute this, so check out the output now. When I value is 1, I double equal to 5 condition is false, so it will print 1, similarly I value will become 2, so condition if condition is false, so it will print 2 value, similarly I value is 3, so this condition is false, it will print 3, I value will be 4, it will be printed. When I value will become 5, so this condition is true, if condition is true, continue will be executed, meaning of continue is all the statements after continue will be skipped inside the loop. So print statement will be skipped and control will jump to the for loop, with the next value. So next value of I is 6, 6 will be printed, 7 will be printed, 8 will be printed, 9 will be printed. So after value of 9, I value will become 10, so for loop will be terminated and control will be out of this for loop and this statement will be executed. So output it will be print 1, 2, 3, 4, 5 will be skipped, then again 6, 7, 8, 9 and then outside for loop. So this is a continuous statement, generally it is opposite of break statement. So break statement is actually breaking the loop and continuous statement is actually skipping all the statements after the continuous statement. So this is about continue now, this is an example for I in range 1 to 11, if I w equal to 6 continue else print I, so output will be 1 to 10 except 6. In this case the for loop will run from 1 to 11. You can pause the video and you can answer this question, what will be the output of following code? So code is for later in python, if later w equal to p then continue, otherwise print later. You can pause the video and you can answer, answer for this question is y, t, h, o, n. Initially later we will point to a p, if later w equal to p condition is true continue means it will skip the next statement which is print. So it will not print p, next time later we will have value y, so this condition is false no continuous statement will be executed, so next statement will be executed that is print statement, so it will be printed y, it will print y value. Similarly later we will have t value, it will print t. Next time later we will have h value it will print h, next time later we will have o value it will print o, next time later will have n value it will print n. So, except p it will print all the letters of Python. Next is else statement. So, normally we have generally if else condition, else can be there with a loop also. We will see that if else statement is used with the for loop or while loop then else statement is executed when loop is exhausted or terminated. When you write a loop when condition is false or when that loop is terminated in that case else statement will be executed. If else statement is used with the while loop then else statement is executed when condition becomes false. So, after the loop generally else statement is executed. This is an example for i in range 1 to 4 print i else print no break. So, output is 1, 2, 3 and no break. So, 1, 2, 3 is actually values printed by this for loop, 2 statements of for loop. So, for loop it starts with value 1, it will end with value 3, 4 will be term exclusive. So, this is not included in the for loop. So, value it printed for loop will print is 1, 2 and 3 then when value of i will become 4 it will come out of this for loop. It will execute else part else it is printing no break. So, output of this program is 1, 2, 3 and no break. These are the references I have used for creating this video. Thank you.