 Hello and welcome back. In this lecture, we are going to try to put together all the different things that we have learnt about how to write iteration in programs. And we will try to use these things to write a couple of programs to solve some small interesting problems. So, here is a quick recap of what you have already learnt so far. You have learnt about iteration idioms in programming. Specifically, you have learnt about while, do while and for loops in C plus plus. You have learnt about the usage of assignment expressions and their variants in loops, particularly in for loops. And you have also looked at how break statements can be used in loops to jump out from somewhere in the middle of the loop body. In this lecture, we will try to put all these things together to write programs with iteration to solve some small interesting problems. And we will also see a new kind of statement that comes in handy when we are trying to write programs. And we will sort of motivate the usage of this statement by one of the problems that we are going to try to solve today. And this new kind of statement is called continue statement and it is kind of like the dual or the counterpart of the break statement. And we will see its usage and how it closely relates to the break statement in the programming problem that we are going to try to solve. Now, as our first example, let us consider this problem. Suppose you are given positive integer inputs m and n and you are required to compute 3 raised to the minimum of m and n and 2 raised to the maximum of m and n and you are supposed to print their values. So, how would one go about solving it? It is quite straightforward. One could say let us just take the values of m and n. Let us find out what the maximum of m and n is, what the minimum of m and n is. And then let us have a loop in which we keep multiplying 3 for min m and n times and let us have another loop in which we keep multiplying 2 max m and n times. Well, so if you had to solve problem by a program of this type, you would have 2 loops in the program. The first loop would be iterating min m and n times and computing 3 raised to min m and n. The second loop would be iterating max m n times and would be computing 2 raised to max m n. An interesting question is can we write a program that actually calculates both 3 raised to min m n and 2 raised to max m n, but using just one loop in C plus plus. So, that sounds interesting and let us see how we can go about doing that. So, let us look at how a C plus plus program for this min max example might look like. I have grayed out some parts of the program that I am not going to focus in the current slide and I have written in black the parts of the program that I am going to focus on in the current slide. So, we start off with our main function which returns a value of type int. We declare some variables. So, here I have a variable called min of m n, max of m n both of which are integers initialized to 0. I also have 2 variables 2 raised to max and 3 raised to min both of which are initialized to 1. I of course have variables for the inputs m and n and I have 2 counters called i and j also of integer type. So, I first ask the user to give me the value of m and n and I read in the values of m and n and then I create one single for loop in which in the initialization part I have used 2 assignment expressions separated by a comma recall that this becomes one compound expression in which the component expressions are evaluated from left to right and when these assignment expressions are evaluated as their side effect the value of m gets assigned to the variable i the value of n gets assigned to the variable j. So, as a result of evaluating this compound expression I have effectively initialized the values of i and j to those of m and n through side effects. What is the update expression? It is the post decrement operator applied on i and j once again separated by comma. So, once again this is a compound update expression composed of 2 component expressions. The way this compound expression is going to be evaluated is that the component expressions will be evaluated from left to right and these post decrement expressions when they are evaluated as a side effect they will effectively decrease the values of i and j. So, I am going to start with the values of i and j set to m and n and I am going to decrement them in every iteration and I am going to check if the value of i is greater than equal to 1 or the value of j is greater than equal to 1. Clearly this loop is going to iterate max m n times because as long as 1 of i or j which have been initialized to m and n stays greater than equal to 1 I am going to iterate in this loop. Now, since this loop is going to iterate max m n times I might as well increment the value of max m n every time I iterate through this loop. So, that at the end I do get the value of max m n and since I have got to raise 2 to the power of max m n I could multiply the current value of 2 raised max with 2 every time I iterate through the loop. Recall that this is one of those assignment operators and the effect of executing this statement using this assignment operator is that 2 raised to max is multiplied by 2 and that same value is assigned to 2 raised to max again. So, if this loop is executed max m n times then 2 raised to max will have the value 2 raised to the power of max m n exactly what we want. Now, how do we calculate min m n? So, this is the interesting part. So, I introduce this if statement inside the force statement where I say that if both i and j are greater than equal to 1 then I am going to increment min m n and I am going to raise and I am going to multiply 3 raised to min by 2 using this special assignment operator again. Now, note that if both i and j are greater than equal to 1 where I have initialized i and j to m and n and I am decrementing them at the end of every iteration it means that the total number of times that this statement will get executed the increment of min m n is exactly the minimum of m and n. The moment 1 of i or j becomes 0 this condition evaluates to false this statement is no longer executed and therefore, the value of min m n does not change. So, here you see I have used one for loop, but using an if statement I am basically making these two statements be repeatedly executed only min m n times although the entire loop is being iterated max m n times. And of course, after I have done this I could print out the value of 2 raised to max and 3 raised to min and then return 0. So, this is our first example now here is how the entire program looks like I have just rewritten what we have already discussed inserted some additional comments here for better readability of the program. Now, let us look at a second example our favorite quiz marks problem, but this is a slight variant of the quiz marks problem we have seen earlier here we are required to read quiz 1 marks of CS 101 students one at a time any negative marks other than minus 1000 will be considered as invalid and must be ignored. On the other hand if somebody enters minus 1000 is the marks then that denotes the end of the input and once that happened we want to compute the number of valid marks some average minimum and maximum and print them out. So, we have seen a program that does almost this except this part that when I read something other than minus 1000, but a negative marks as input then I should treat it as invalid and ignore it except this we have seen the rest of it. So, let us try to see how we might write as C plus plus program to do this. So, let us look at how the C plus plus program for this problem might look like since we have seen programs to solve various versions of this problem several times in earlier lectures I have put most of the program in black font and there is only a little part that I have grayed out. So, let us see what the different parts are doing the main function returns a value of type int I have several variables that are declared here mark some count min and max the average could be a floating point number. So, it is declared to be of type float and now I have this one for loop and as the initialization expression I use this compound expression which is made up of two assignment expressions separated by a comma. So, when these two assignment expressions are evaluated as a side effect some gets initialized to 0 count gets initialized to 1 the loop condition is empty which basically means that it is true and the update expression is an post increment expression which as a side effect increments the value of count inside the loop body I ask for the marks of a student read the marks and if the marks is negative I have got to do something otherwise I can update the different aggregates some min and max and then go back and iterate in the loop again and once I am done once I have exited from the loop then I can compute the average print out the count of valid marks some average min and max the one thing to note in the computation of this average is that I have divided some by count plus 0.0 why did I do that some is of type integer count is of type integer if I just said some divided by count that entire expression would be of type integer and I would lose the fractional part of the average count plus 0.0 is of type float because count is of type integer and 0.0 is a floating point constant therefore, this denominator becomes of type float and now when I divide some which is an integer divided by this floating point value whose value is the same as that of count I get a floating point average now let us see what happens when the marks is less than 0 if the marks is not equal to minus 1000 then I want to output invalid marks and I want to somehow go back and repeat this loop because I have got to read more marks otherwise if the marks is minus 1000 then I know that all the marks have been read so I can exit the loop compute the average and print out the different metrics. So the usage of the break statement has already been covered in an earlier lecture this basically skips all the instructions in the body of the loop in this iteration and directly exits the loop and comes out to the next statement after the loop. Now how do we repeat the loop here so it turns out that in C plus plus there is a facility to do this called a continue statement. So just like the break statement it skips all instructions in the body of the loop in this iteration but instead of exiting the loop it goes back to the start of the loop and it starts the next iteration of the loop. So here you see that break and continue can be conveniently used inside this if else to ignore invalid marks and also to jump out of the loop when we have read all the marks and of course how do we update some min and max we have already covered this in a previous lecture but here is a recap again we add marks to some and if this is the first marks I am reading then I update min and max with marks otherwise I update min and max with conditional expressions that ensure that min is the minimum marks have seen so far max is the maximum marks have seen so far. So that is how our entire program looks like. So in summary we saw examples of two C plus plus programming problems where we used iterative constructs with assignment expressions and we also saw the usage of a new kind of statement called continue statement which is the counterpart of the break statement. Thank you.