 Hello and welcome back. In this lecture, we are going to look at another iterative construct in C plus plus. This is called the FOR statement in the C plus plus programming language. Here is a quick recap of some of the things we have already studied. We have looked at iteration idioms in programming. We have looked at the fact that they are sometimes necessary and they certainly provide a convenient construction when writing programs. In the context of C plus plus, we have looked at two kinds of statements that allow us to write loops or iterative constructs. These are the while statement and the do while statement. In this lecture, we are going to look at yet another kind of statement in C plus plus that allows us to write iterative constructs or loops. This is the FOR statement that we are going to study today. We are also going to look at the use of break inside FOR loops and we are going to look at a kind of high level comparison of the different iteration constructs in C plus plus. Now, recall from an earlier lecture, we had said that the generic iteration construct looks something like this. There is a part of the program that is executed before the iteration. Then, there is some iteration initialization code, which sets up initial values of different variables that are used inside the iterative loop. Then, we iterate or repeat as long as a logical condition stays true. What do we repeat? We basically execute a block of statements and sometimes we also execute some specific instructions at the end of every iteration. Every time we execute this block of statements, at the end we execute some specific instructions. Then, we go back to the beginning of this block and repeat executing it as long as this logical condition stays true. Once this logical condition becomes false, we come out of this iteration or loop and execute the remainder part of the program. We have seen that this part is usually called a loop. This logical condition is called a loop condition and the block of statements along with any optional instructions to execute at the end of every iteration is often called the loop body. Now, in a for statement, we basically instantiate this template in this manner. We have the part of the program before iteration and the part of the program after iteration. We use the keyword for, then we say whatever this iteration initialization was doing, how these variables are initialized before the iteration begins. We put a semicolon, then we put the loop condition, we put a semicolon and then we write instructions to execute at the end of every iteration and then we give the block of statements which we will also call the loop body. But in this case, note that this block of statements along with these instructions are going to be executed in each iteration. So, I will designate this block of statements that we write within the braces in a for loop as the for loop body. The real loop body is this block of statements plus these instructions executed at the end of every iteration. Now, note the use of semicolons here to separate the three parts of this for construct. The first part is the initialization, the second part is the loop condition and the third part are instructions to execute at the end of every iteration. Now, here semicolons are not being used to denote the end of executable statements unlike what you have seen earlier, but here semicolons are being used to separate the three parts of the for loop. Note also that we do not put a semicolon here because as I said the semicolons are just used to separate the three parts of the for loop. Now, here is a flow chart representation of a for construct in C plus plus. We have the part of the program before iteration and so our control may start there, then it goes to the part of the code that does the initialization for the iteration, then it evaluates the logical expression of the loop condition. If that turns out to be true, it goes through the for loop body and then it is going to execute the instructions that are supposed to be executed at the end of every iteration, comes again evaluates the logical expression of the loop condition and keeps moving as long as the loop condition holds true and when it becomes false, it takes this path and executes the part of the program after the iteration. Now, there are few points to remember about the for construct. So, here is our for construct as we have just seen. Note that the initialization code is going to be executed only once before the first entry in the loop. It is not going to be executed every time we entered the loop. Before the first time you entered the loop, that is the only time this initialization code will be executed. Note also that the loop condition will be checked before executing the for loop body. So, the loop condition evaluates to true the very first time, the for loop body may not be executed at all. Clearly, the number of times the loop condition is executed is 1 plus the number of times the loop body is executed because the last time the loop is executed, if the loop terminates, the loop condition will evaluate to false that last time and the loop body will not be executed that last time and therefore, the number of times the loop condition is checked is the number of times the for body is executed plus this one last time when the loop condition evaluates to false and control comes out of the loop. The loop condition can be changed inside the for loop body or it can also be changed in the instructions that are specified to be executed after every iteration. Now, let us revisit the problem of aggregating quiz one marks of CS 101 students that we have seen in an earlier lecture. So, in this problem we are required to read the number of students in CS 101, then read quiz one marks of all of these students and then print their some average maximum and minimum. Here is how we might plan to solve this problem using a flow chart, we read the number of students initialize the aggregates, here the aggregates are some min and max, then we initialize a counter to 1, check with whether the value of this counter is less than or equal to the number of students, if so we ask for the marks of the next student read the marks update the aggregates, then increment the count at the end of this iteration and then come and check again this condition whether the count is less than or equal to the number of students, keep doing this until count becomes greater than the number of students in which case the condition evaluates to false and we come out and compute the average and print aggregates. Now, the reason I have written out this flow chart in this manner is that once you view this flow chart as I have shown here, you will immediately see its similarity to the flow chart that we had seen a couple of slides back and in fact, this tells you immediately that this problem can now be solved using a for loop. So, here is how the C plus plus program might look like, we have the main function and then we have our usual variable declarations, we have already seen these earlier in a previous lecture, we ask for the number of students and input the number of students and now we have our for loop where we are initializing the value of count, checking the loop condition whether count is less than or equal to number of students, writing that count must be incremented by 1 and then inside the body of the for loop, we ask for the marks of each student read in the marks and update the aggregate sum max and min. Now, if you look at this for loop, you will see that this is the initialization code, this is the loop condition and this is the instruction to execute after every iteration and this is the for loop body. Now, how do we update some max and min, we have already seen this in an earlier lecture, but let me just flash it again to refresh your memory, sum is augmented with the marks that is read in and then if you are reading in the marks of the first student, we update min and max with the value of the marks read in. Otherwise, we use conditional expressions to update the value of min and max, these conditional expressions basically ensure that min is the smallest marks I have seen so far and max is the largest marks I have seen so far. After that, after we come out of the for loop, we can of course compute the average and print the average sum min and max and then return control back to the operating system. Now, just like in while loops, we can also have break statement in for loops and the behavior is exactly the same as in while and do while loops. What is this behavior that we must jump out of the loop immediately on executing break without executing any further instructions inside the loop. Now, recall our variant of the quiz 1 marks problem that we had once again seen earlier. In this variant, we said that we are not going to specify how many students there are in CS 101 to begin with, but we will continue reading marks of CS 101 students one at a time and if at any point of time, we see minus 1000 as the marks, then we know that we must stop reading now and we can then print out the number of marks entered, the sum, average, maximum and minimum. We had seen earlier how to solve this problem using a while loop with a break statement. We are now going to see how to solve this problem using a for loop with a break statement. So, here is how the relevant part of the C plus plus program might look like. So, we have the same for loop as we had before, but now the loop condition I have replaced by true. Earlier, we had a loop condition count is less than or equal to number of students. Now, I do not know how many students there are. So, I have replaced the loop condition by true. Note that this means that I am going to iterate around this indefinitely unless of course, I have a way to break out from the middle of the loop. So, in general having a true here denotes that we are talking of an infinite loop, unless there are break statements in between at which point we can exit the loop. What does the break statement do? It allows us to jump out of this loop immediately on executing break statement. Specifically, when I execute the break statement, the next statement that is executed is the one immediately after the loop. Note that this statement which is normally executed after every iteration completes is not executed after we execute the break statement. So, the break statement really tells the computer jump out of the loop does not matter what other statements are there wherever in any part of the loop. So, it will jump out over here ignoring this statement. Now, we could also have for loops with empty parts. Note that I have used true over here. I could actually just skip the true and skipping the loop condition in a for loop is equivalent to having a true loop condition. In fact, we can do more. So, this is our for loop where we had these three parts initialization, loop condition and the instruction to be executed after every iteration. We have just seen that the true condition can be replaced by an empty condition. In fact, the initialization part can also be skipped if you are willing to move it before the for loop starts and the part that is to be executed after every iteration can also be skipped if you are willing to move it at the end of the loop body. So, now this is a for loop with three completely empty parts and still this is a perfectly legitimate for loop. So, now the previous animation would have given you this idea that really the initialization loop condition and instruction of every iteration can really be moved around and therefore, it should be possible to rewrite a for loop as a while loop and indeed it is possible whatever is there in the initialization. You put it at the beginning of the while loop, the loop condition goes in the while loop and the instruction of every iteration is at the end of the for loop body. Similarly, of course, we can convert a while loop to a for loop where there is nothing to initialize, there is nothing to execute at the end of every iteration. We just have the loop condition and the while loop body here and of course, if I were to transform for loops to do while loops or do while loops to for loops I can easily do that because we have already seen in an earlier lecture that while loops can be translated to and from do while loops and we have just seen how for loops can be translated to and from while loops. Now, which should be used for or while in a particular application it depends on the application. One thing to note is that in a for loop the real computation we want to do is exactly in the for loop body, whereas this other part initializing count, incrementing count, checking the value of count these are all book keeping and they are cleanly isolated. Whereas, in a while loop the real computation that we want to do kind of gets mixed with the book keeping information. However, it is really a programmer's choice that depends on the context which of these constructs we want to use. In general, it is a good idea to separate book keeping from real computations. So, for loops would seem preferable. However, loop conditions may not simply be based on book keeping. Loop conditions may also be changed by the real computation in a loop body in which case we might want to prefer while loops. So, in summary we have looked at for statement in C++ and its variance. We have looked at the use of break statement within for statements and we have seen a very brief comparison of for with while and do while statements. Thank you.