 in the previous segment, we discussed the while statement and compared it to the repeat statement. Now, we are going to look at how to do the mark averaging problem. So, let us begin with the manual algorithm for mark averaging. So, here it is. So, we are going to keep two variables. So, one is the variable sum into which we are going to get the sum of all the marks and then there is going to be a variable called count and in this variable we are going to keep track of how many marks we have read. So, we are going to start by making these variables 0. Then we are going to read the next value and the next value is going to be read and let us say we read it into a variable called next mark. Well, in a manual algorithm we will not really have variables of course, but since you now know variables we might as well bring them in just to make our discussion clearer. Then we are going to check is the mark value that we just read which is in next mark is it less than 0? If it is less than 0 then we know that we need to terminate. So, we need to at that point print the average and stop. So, therefore we are going to go to step 7 and step 7 indeed prints the sum divided by count which is going to be the average. Well, if next mark is greater than or equal to 0, then what do we do? Well, then we just continue. So, we go to the next step which is the step 4 and now whatever mark we just read which is a valid mark now we know and we add it to sum. But we also read one more mark and therefore we increment count by one as well and then we have to repeat the process. So, we again go back to step 2 which is the starting point of the loop. Now, let us first draw a flow chart of this just to make sure that we understand exactly the control flow that is what happens after fought. So, here is the flow chart. So, we are going to start off by setting sum and count to 0. Then we are going to read in next mark. Then we are going to check whether next mark is greater than 0. If it is false or it is less than 0 then we are going to print calculate and print the average. Otherwise, we are going to add next mark to sum. We are going to add one to count and then we are going to repeat again from this statement. Now, our goal is to implement this using the vile loop and therefore it might be instructive to compare the structure or the connections in this flow chart with the connections in a vile loop. So, here also there is this condition here also there are some statements which are being executed. But there is an important difference between these two pictures. What is the difference? After the body is executed, the control goes back to the condition checking. Over here, if this is the body then after that body is executed the control does not go back to the condition checking but it goes back to an additional statement before the condition checking happens. So, because of this difference it is not clear how we can use the vile statement because if this is the condition and this is the body what is this? That is not very clear. We cannot directly use this because the structures do not match. So, we are going to then do something to the flow chart. So, we are going to have a different flow chart for mark averaging. In this slide, we have given two flow charts for the mark averaging program and I am going to observe that they are really doing the same thing. So, what is it that we wanted? We wanted that this path should join not at this point but it should join directly at the condition check. But we cannot just take this path and join it over here. If we do that, then along this path, we would be doing something different. So, if we are going to make any changes, we should make sure that along every path the same operations happen, maybe they happen before the merge in one case or in another case they happen after the merge. So, if we do not like that this block is present between the point of joining and this and this condition check, what we could do is we could push this block above on this path. But we cannot just push it on one side on one of the paths because then if we come down this straight path and if the block is not present, then we would be doing something different along this straight path. So, what we can do is that we can take this condition, this statement and we push it back on both paths. So, what happens now is that if we come down for the first time, then the operations that we perform are still going to be the same, so we are going to execute this statement, then this statement, then the condition check. Similarly, over here, we are going to execute this statement, this statement, and the condition check on a subsequent iteration as well. After the condition check, we are going to execute this statement, and we are going to execute to this statement and the condition check again. Even here after the condition check we are going to execute this statement exactly the same as before then we are going to execute this statement exactly the same as before and then we are going to execute the condition check exactly the same as before. So the general claim is that if a certain block is executed after two parts merge say as over here this block is executed then we might as well execute it separately on the two parts before they merge and then we do not have to execute it after they merge because we just executed them before. So essentially what happens over here is exactly the same thing that happens over here. Now why is this helpful for us? Well now we can make this be the condition and we will make this followed by this as the body of the loop. So I can sort of bring this back, I can just bring it back and sort of blend it into b. So then this will be my condition check and this followed by this will be my body. So here is what the program is going to look like. So I am going to declare the variables next mark and sum and sum is going to be 0, I am also going to declare count and count is also going to be 0. So I am at this point I have executed this statement and this state, sorry I have executed this statement. Now I need to execute this statement and this statement is not within any loop or anything. So I have that. So this is this block A which I have marked here just to indicate the correspondence. Now after this I come to the condition check of the vile. So I am going to have a vile and that is going to be my condition check. So the C block over here is going to be this statement. After that I am going to have the body. So the body first has this statement from B, it then has the second statement from B but as it executes it has to execute this statement from A again. So that is also going to be a part of the body and this is a copy of A so that is what I have indicated over here in the comment. So at this point the body execution ends. So we have this and then again the control will go back and start from this statement which is exactly what happens in the flow chart over here. So if the condition is false then I want the average to be calculated and printed. Similarly over here if the condition is false it will come and execute the statement over here and so we will put the average calculation and printing statement over here and that is what our main program, our program is going to be. So this program now uses the while statement and it directly implements this flow chart but we know that this flow chart is really the same as this flow chart. So here is an exercise for you. Write a turtle controller which we saw last time but which this time has an extra command. So if from keyboard you type F then the turtle should move forward 100 pixels as before. If you type R then the turtle should turn right 90 degrees but now if you type X the turtle should finish execution. You will see that you will have to pretty much have the same structure as before. So maybe repeat some part of the code before entering the loop and so on. So what have we discussed in this segment? So we said that in while loops the first step is check whether we need to execute the next iteration. In some loops that we want to write the first step may not be the condition check. We might have to do something before we check the condition. In this case we can modify the loops or we can modify the flow charts associated with the loops by making a copy of the steps that we need to do before the condition check and they have to be performed before the entry into the loop as well as at the end of the body of the loop. So this is one way of dealing with such slightly tricky loops but there is another way which we will see in the next segment. So we will take a break here.