 Hello and welcome back. In this lecture, we are going to look at the while and do while iteration statements in C++. Here is a quick recap of some of the relevant topics we have already studied. We have looked at iteration idioms in programming. We have seen that they are necessary to solve some problems and they are also convenient in other situations and we have also seen a very brief glimpse of iteration constructs in C++. In this lecture, we are going to look at in more detail the while and do while statements in C++ and we are also going to look at the usage of break statements within while and do while. Now recall this generic iteration construct that we had studied in a previous lecture. So, in general, we have a part of the program that is executed before the iteration and then we have some initialization which is iteration specific and then the actual iteration happens where we iterate or repeat as long as a logical condition stays true and what do we iterate over? A block of statements and in some cases there are some additional instructions that are executed at the end of every iteration. After that, the part of the program that must be executed at the end of this iteration appears. So, we had seen earlier that this part of the code is usually called a loop. This logical condition is also called a loop condition and the block of statements along with instructions to execute at the end of every iteration is often called loop body. Now in C++, we can implement this using the while construct as follows. We could say while loop condition, block of statements and the optional instructions if any that are to be executed at the end of every iteration can be grouped with this block of statements. Similarly, the initialization part can be grouped with the part of the program before iteration. So, we have a part of program before execution of the loop. We have the while loop within which we will call both the block of statements and statements to be executed at the end of every iteration as the body of the while loop and then we have the part of the program after the while loop. As a flow chart, this can be represented in this manner. We have the part of the program before iteration, part of the program after iteration and then we check for this loop condition. If it is true, we go through the loop body and again come and check for the loop condition and at any time the loop condition becomes false. When we are doing this check, we can exit from the loop and execute the part of the program after iteration. So, here is a simple animation of how control might flow through a while loop. This red dot indicates the control flow. So, the control might flow around the loop a few times and then come out of the loop. Now, the points to remember about while loops in C++ are that the loop condition is checked before executing the loop body. So, if the loop condition is false, the very first time we are checking it, the loop body will not be executed at all. If the loop indeed terminates, then the number of times the loop condition is checked is exactly one more than the number of times the loop body is executed and where does this additional one come from? This additional one comes from the last time the loop condition is checked when it evaluates to false and then you exit from this loop without executing the loop body. An important point to remember is that if this loop condition is not changed at all within the loop body, then once you enter the loop and you come back and check the loop condition, it will always evaluate to the same truth value as it was in the previous iteration. So, if you have entered the loop once, the loop condition must have evaluated to true and the loop body if you have not changed the loop condition, when you come back and evaluate it again, it will again evaluate to true and you will again enter the loop body and this will continue forever. You will get effectively what is called an infinite loop or a non-terminating program. So, you must be very careful when you are using while loops to ensure that the loop body does indeed change the loop condition so that you can finally exit from this loop. Of course, there are situations where we might want the program to loop a very large number of times or even infinitely many times, but these are special cases of programs which we will see only later in the course. Now, if you look back at the problem that we were trying to solve when we introduced iteration idioms in programming, we had said something like this that you read the number of students in CS 101, read quiz 1 marks of all of these students and print their some average maximum and minimum. Now, using a while loop, we could solve this and this is kind of obvious to see if I just try to represent the flow of control through this flow chart and if you recall this is exactly how the flow chart for a while loop looks like. So, I am going to read the number of students, initialize aggregates, initialize a counter to 1, check if the value of this counter which denotes how many quiz marks have we processed so far. If it is less than or equal to the number of students, we go back, ask for the marks, read the marks, update aggregates, increment the count and come back and again check whether we have input the number of students, whether we have input marks for all students. And once we have done that, we can exit through this part and compute the average and print aggregates. Now, if you go back and look at the flow chart representation for a while loop, it looks exactly like this. Here is the loop condition, here is the loop body, here is the program before the while loop, here is the program after the while loop. So, since our control flow matches exactly the pattern, flow chart pattern for a while loop, we are going to implement it using a while loop. So, this is how our C++ program with while might look like. We have our main program and then we have some variable declarations. In this particular case, I have declared the variable count to also be of type float and I will soon explain why that is the case. We have initialized some to 0 during the declaration itself. We ask for the number of students, read the number of students, initialize count to 1.0. Count basically keeps track of the number of students whose marks have been processed so far and then we have this while loop. While count is less than or equal to number of students, we are going to ask for the marks of the student, input the marks, update the values of sum, max and min, increment the count and go around the loop until this logical condition is evaluated to false. Now, what does updating sum, max and min mean? We have already seen this in an earlier lecture. To sum, we add the currently read value of marks and if the value of count is 1, we update min and max straight away by the value of marks. Otherwise, we update min and max through conditional expressions. After we are done reading all the students marks and computing the aggregates, then we compute the value of average which is sum divided by count and here you will recall that sum is an integer and since I had declared count to be of float, therefore sum divided by count will be of float data type and therefore I will be able to retain any fractional part in the average when I do this division. So, this was the reason why we had declared count to be of type float. Average is of course of type float and once we have computed the average, we can print the average sum min and max and then return control back to the operating system. Now, if you look at this loop, what is happening inside this loop is that we are reading marks for each student and then these marks were being provided one after the other, they were not being provided all together. So, this is also sometimes called streaming inputs, but the important point is that we did not remember all the inputs that we have seen so far. We only remembered some aggregates of these inputs, in particular sum, max and min. So, what are aggregates? In general, these are some kinds of summary of the streaming inputs that we have seen so far such that we do not need to remember the actual values of the inputs, but we can just use these summary values to compute the final result. And in fact, I want to highlight this point here that accumulation or aggregation is key to programming with loops for streaming inputs. You cannot possibly remember all the inputs that are streaming in. So, you have to accumulate or aggregate them and using these aggregates, you have to compute whatever you want to compute. Now, let us look at a variant of our problem. Earlier, we had said that we are going to provide the number of students in the class as an input. Now, suppose somebody said that, well, I cannot give that as an input. So, what you are required to do is you are required to read off the marks of CS 101 students one at a time. And then if at any point of time, I provide minus 1000 as the input for marks, then you must stop reading and you must then print out the number of marks entered, there is some average minimum and maximum. Now, what is the difference of this problem from the earlier version? We do not know a priori how many marks will be entered. Earlier, we knew exactly how many students were there, and so we could iterate the while loop that many times. In this case, the end of inputs is indicated by a special marks, which is minus 1000. And we will know when to stop only after we read this special marks. So, how could we modify our earlier C++ program to solve this problem? So, once again, we declare marks, some min, max, declare, average, count. And then we have our little while loop over here. But in this while loop, I do not know when I am going to come out of this while loop until I have actually read the marks. So, I am going to put while true here. Remember, here we can put any logical expression. True is also a logical constant and hence a logical expression. So, I can put while true, but then notice that what this means is that this condition will never evaluate to fall. So, the loop condition will be always true, which means this looks like an infinite loop. So, this is an interesting case where we might want to have an infinite loop. And then we will see how we will actually come out of this loop. So, we say while true because we do not know how many student marks to read. And then we say give marks of student, read in the marks. And if the marks is minus 1000, then I want to exit the loop by some means. Otherwise, I want to update some max and min like I had done earlier increment count and then loop back again. And finally, I have to calculate the average. In this case, it is sum divided by count minus 1. Because note that when I exit from this loop by reading minus 1000 as the marks, I have already incremented the value of count at the end of the previous iteration. So, even if I do not execute this loop even once, the value of count is 1. If I execute this loop once, the value of count is 2 and so on. And therefore, I need to divide by count minus 1 when I am computing the average. And finally, I print all of the desired quantities. Now, how do we exit from this loop when marks is equal to minus 1000s? It turns out that C++ provides an easy way to do this. In fact, that is called a break statement. So, I could simply say that if marks is minus 1000, then break. And you would recall that we had also used the break statement when we discussed switch case statements in an earlier lecture. Now, can we really do without breaks or are breaks absolutely essential? So, it turns out that we can do without breaks. For example, I could say that let me use an exit flag which is false to begin with. And then I am going to iterate as many times as the exit flag remains false. And then if marks becomes minus 1000, I will set this exit flag to true. So, the next time when I go back to check this condition, this condition which is negation of exit flag evaluates to false. However, note that this incrementing of count must now appear within the else part. Because if I had a break here, then when I exit it from the loop, count would not have been incremented. But if I use an exit flag and if this incrementing of count is outside this else block, then it would be incremented even after exit flag is set to true. Count would be incremented and only then would I exit this loop. So, if I want to preserve behavior with respect to the previous code, I have to include else within the, I have to include break within the else block. And using break provides us the convenience of avoiding such annoying complications of putting code that in any case I want to execute before the next iteration is executed. I can now put this code outside the else block if I used a break statement over here. To recap, this was our wild statement in C++, there is a very similar construct that we can use in C++ called do wild. Now, in a do wild statement, it is basically the same as a wild statement except that this block of statements is executed at least once. So, the flow chart for a do wild statement looks like this. We have the program before iteration, the program after iteration, the loop body, there is a loop condition and then the loop, the same loop body again. So, the control flows like this. So, we execute the loop body as many times as is needed, but at least once. And then when the condition evaluates to false, we come out of the iteration. Now, what is the relation between a wild and a do wild? If I give you a wild loop, it can be easily converted to a do wild by basically checking the loop condition first. And if the loop condition is true, then I can put the do wild inside. And similarly, if I give you a do wild loop, I can easily implement it as a wild loop by making sure the loop body is executed at least once. And break statements can of course be used in a do wild in the same manner as they are used in a wild statement. So, what is the comparison between wild and do wild? They are almost the same and it is really left to the programmer's choice. We prefer to use do wild when we know that we are going to execute the loop body at least once, otherwise, we prefer to use wild. So, in summary, we looked at the wild statement in C++, and we also looked at the use of break statements. Thank you.