 Hello and welcome back. In this lecture, we are going to look at iteration idioms in programming and specifically we are going to have a brief glimpse into iteration constructs in the C++ language. Here is a quick recap of some of the relevant topics. We have already studied the structure of a simple C++ program. Within that, we have seen sequential execution of statements and also conditional execution of statements and we have also seen how to solve some simple problems using C++ programming. In this lecture, we are going to try to understand the need for iteration in programming and we will see that iteration serves multiple purposes. It is a convenient feature to have in a programming language. It sometimes becomes necessary and it also helps us program in tune with our intuition. We will look at the structure of a generic iteration construct and we will have a very brief glimpse into some iteration constructs in C++. So, to begin, let us consider a simple problem. Suppose I am required to write a program which reads the quiz 1 marks of 10 CS11 students and then it is supposed to print the sum of these marks, their average and the maximum and minimum of these marks. So, the question is can we solve this problem using the features of C++ that we have already learned which means assignment statements, input-output statements, arithmetic expressions and sequential and conditional execution of statements. So, here is what an overall strategy might look like. We might want to maintain a running sum, max and min value. So, these are some kinds of aggregates of the values that we are seeing of the different marks that we are seeing and we can initialize these aggregates to appropriate values and then we can read each input that is the marks of a student in quiz 1 of CS101 and we could update these aggregates and after we have done this for all 10 students, we could then compute the average as sum divided by 10 and we could output some average, max and min. So, that looks simple and here is what a simple flow chart for this problem might look like. Here, we initialize the aggregates and then for each student, we ask for the input that is his or her quiz 1 marks. We read the input, update the aggregates, then we do the same thing for the second student, then we do the same thing for the third student all the way up to the 10th student and at the end, we compute the average and print out the average sum, min and max. Well, if I want to translate this flow chart into a C++ program, this is how the program might be built up. We will start off with the main function and it will declare variables. So, here I have marks, sum, min and max as integer variables. Let us assume for our purposes that all marks are integers. However, even if all marks are integers, the average could have fractional part. So, we declare average to be a float data type and we initialize the aggregate sum and average to 0 and now we are going to write some further code which achieves the purpose that the problem requires us to do. So, after we have declared the variables and initialized sum and average, we could print out this message that gives quiz marks of student 1, read the marks, add the marks to the sum. So, note that I am maintaining this running value of sum. I initialized it to 0 and now I have read the marks of the first student and I am adding it to sum and since this is the first student's marks, I could initialize min as well as max with marks itself and then after I have read the marks of the first student and updated the aggregates, I could do this for the second student, I could ask for the quiz marks of the second student, read in the marks, again update sum. So, this is the running sum which is accumulating the marks of all the students and then for min and max, I could use these conditional expressions which we have already studied in an earlier lecture to update the values of min and max. So, here what am I doing? I am checking if the current value of min is already greater than the marks of the second student I have just read. If so, I am setting the value of min to be the value of marks, otherwise the value of min stays unchanged. Similarly, for max, if the current value of max is less than the marks of the second student I have just read in, then I update max to have the value of marks, otherwise max retains its value. Now, after I have done this for students 1 and 2, I could do a very similar thing for student 3, note that almost all the statements are identical as we did for student 2. And after we have done this thing for all the students from the first student till the 10th student and updated the aggregates, then we could compute the average which is sum divided by 10.0, note that sum is an integer, 10.0 is a float. So, therefore, sum divided by 10.0 is a float and that is assigned to this float variable. We could then print out the average sum min and max and return to the operating system with the return value 0. Now, some observations with respect to this previous program are particularly interesting. First of all, almost the same instructions were repeated multiple times and in particular this block of instructions printing out give marks of student numbers such and such, reading in the marks, updating sum, updating min, updating max, this block of statements was executed as is for almost all the students. The only difference was for student 1, we updated min and max by directly assigning the value of marks to it. And intuitively too, there is no difference between the marks of student 1 and the marks of student 2 or the marks of student 3 as far as our program is concerned. So, we would like to execute almost the same instructions for all the students. And now, if I ask you that suppose I give you a construct in the programming language that allowed you to effectively say that repeat the following block of instructions a specified number of times, then could we write a less repetitive program to achieve the same purpose, but without having to write the same set of instructions for every student's marks that we are reading. So, here is how our C++ program might look like with this new repetition construct. So, here is our usual variable declarations and initialization of aggregates. I have kept a separate integer variable called count initialized it to 1. And then I am saying that I want to repeat the following block of code 10 times. So, note that I have just written it within comments here. This is if I just feed this program to a C++ compiler and then run the program, it will basically ignore this comment. So, it will not really execute it 10 times, but this is my intent. I want to do something here which will make the program repeat this block of code 10 times. And what do I do in this block of code? I say give marks of student and then I actually print the value of count. So, the first time this is executed, it will say give marks of student 1. And then I have written the marks of the student, update sum, update min and max appropriately, increment count and then again repeat the same block of code. So, the next time I print the statement, it prints give marks of student 2. And after I have done this 10 times, after I have repeated this block of code 10 times in execution, I can then compute the average and print the average sum min and max. Now, in the previous slide, we said that update min and max appropriately and we have seen that min and max need to be updated in a different way for the first student as compared to the way we updated for the second to the 10th student. So, how could we do this in our program? We could do this by a simple if else statement. So, if count is 1, which means we are reading the marks of the first student, we just copy the value of marks into min and max. Otherwise, we update min and max using the same conditional expressions that we have seen earlier. What is this incrementing of the count doing over here? What purpose does it serve? For the time being, the only purpose it seems to serve is when I am saying give marks of student. So, it says whether it is marks of student 1 or marks of student 2 and so on. So, if you look at this C++ program in which we have not yet indicated the exact construct that will allow us to repeat this block of code 10 times, but we have just indicated our intent through this comment. Now, if we could actually make this block of code repeat 10 times without writing it out 10 times, then this code would be much less repetitive and much closer to our intuition than the previous code where we made 10 copies of these statements. Why is it closer to our intuition? Because we have just written down the statements that we want to execute for each of the 10 students and then we are just saying repeat this 10 times. Yet, in this case, the original problem could have been solved and we have just seen how we could solve it without the repetition construct or the iteration construct, which means that in this case, the repetition construct is handy. It gives us convenience. It allows us to write the program closer to our intuition, but it is not absolutely necessary. We will soon see a situation where the repetition construct becomes absolutely necessary. Now, talking about repetition, repetition in programming is actually not a very desirable thing. It is first of all wasteful because if you can achieve something by coding once, why would you want to code it again? More importantly, it is a potential source of bugs and inconsistencies. Consider our original program where we had 10 copies of the sequence of instructions once for each student. Suppose, as an afterthought, we wanted to say thank you after each marks is read. What would we have to do? We will have to write c out less than less than thank you at 10 different places. And what if in this process, we made a mistake and typed in some different string at one place? So, then the greeting message that would come for one student might differ from the one that comes from another student. And in a more general setting, the situation could be far more dangerous than just a message being printed wrong. You could perform one calculation for one student and a completely different calculation for another student resulting in some wrong averages being printed out finally. In addition, when you write large pieces of code and if you have a lot of repetition, managing that code, maintaining that code becomes difficult because a small change in a replicated code requires you to replicate this change at several places. And this is a recipe for introducing bugs and inconsistencies in large pieces of code. So, as a general rule of thumb, we should reuse as much code as possible, avoid repetitions consciously and the iteration construct in a programming language allows us to do this. Now, in the previous case, we said that we could solve the problem that we were given to solve without using the iteration construct, but the iteration construct gave us some convenience. Now, suppose I told you that instead of repeating this block of code 10 times, it has to be repeated n times where n is user specified. We want to aggregate the marks of n students. Now, clearly in this case, we would not know what n is when writing the program and therefore, we cannot make n copies of this where n is unspecified when we are writing the program. So, in this case, repetition or iteration becomes necessary and the problem cannot be solved without this. So, how might we write our program to solve this problem? We might want to say that first give the number of students in CS 101. So, I have declared another integer variable called num students and then I could read in the value of this number of students in CS 101 and then I have this counter which is initialized to 1 and then I could say repeat the following block of code while the value of this counter is less than or equal to number of students and of course, the counter is incremented every time I repeat this block of statements. So, the point to note here is that instead of saying repeat the code 10 times a predetermined number of times, I am saying repeat it as long as a logical condition is satisfied. In this case, count less than or equal to number of students and this incrementing of the counter is now very important because it affects the logical condition for terminating the loop. So, in general, an iteration construct looks like this, there is a part of the program before iteration. Then there is a part where you initialize values that are going to be used in your iterative construct and then you iterate or repeat as long as a logical condition stays true and what do you iterate? You basically execute a block of statements and you could optionally have some instructions to execute at the end of every iteration. Of course, we could treat this whole thing as one block of statements and after your iteration or repetition is completed, when this logical condition becomes false, you come out of this iteration construct and you execute the remainder part of the program. So, if I just look at this part where things are iterating, this whole thing is also sometimes called loop. This logical condition is called a loop condition and this block of statements along with the optional instructions executed at the end of every iteration are called the loop body. In C++, we have three different iteration constructs while do and for. In each case, we specify the loop condition and the block of statements to iterate over, but it is just that they are done in three different ways. In the for loop case, we can also put some initialization code and the code to execute after each iteration as part of the for loop construction. We will see details of these in the next few lectures. So, in summary, we looked at iteration idioms in programming. We saw that they are necessary in general and they are certainly convenient when we are writing code according to our intuition. They enable code reuse, allowing us to avoid the mistakes of repetition, the mistakes that can get introduced due to repetition. We also got a glimpse of iteration constructs in C++. Thank you.