 Welcome to the second lecture sequence of the third week of the NPTEL course on an introduction to programming through C++. I am Abhiram Ranade and the reading for this lecture sequence is chapter 7 of the textbook. So let me begin with a problem. The problem is that of averaging marks. So you are to read, your program is to read the marks of students from the keyboard type by the user from the keyboard and then print out their average. Now there are some conditions. So the number of students is not given explicitly. Instead if a negative number is entered as mark, then it is a signal that all marks have been entered and you may assume that at least one positive number is given. If no numbers are given, then the average is undefined and therefore you have to assume that at least one positive number is given. So let me first construct some test cases or examples of input output. So for example, the input might be 98, 96 minus 1. So this indicates that the first two numbers 98 and 96 are student marks and minus 1 is the indication that no more input is going to be given. So in that case there are two marks 98, 96, so their average is 97. Another example, 98, 70, 60 minus 1. So again, the actual student marks in this case are 98, 70, 60 and clearly the average is 75 which is supposed to be output. Now this cannot be done using what you know so far. So far you only know one single repetition statement which is the repeat. And that statement repeats a fixed number of times. So it is not useful here because here you are going to repeat as many times as there are students but how many students there are is not given to you beforehand. So we need something new and there are a number of C++ statements that can do what is needed over here. So these statements are the vile statement, the do vile statement and the for statement. And these statements as well as the repeat statement are sometimes called looping statements. So loop is just another term for repetition. So loop is doing something in a circular manner, so repetition basically. So the outline of this lecture sequence is that I am going to first discuss the vile statement. I will talk about a simple example and then I will get to the mark averaging problem. Then I will talk about the break statement, then a statement called the continue statement, then I will mention the do vile statement but I am not going to discuss it in great detail, it is described in the book. Then I will talk about the for statement and then I will talk about loop invariance and this is going to be discussed with the Euclid's algorithm for the greatest comment advisor as an example. So let us look at the vile statement. The form of the vile statement is vile, then in parenthesis a condition and then the body. So the way this works is we are going to first evaluate the condition. The condition is simply something which evaluates to true and false. So we studied this last time. Then if the condition is false then the execution of the vile statement ends. If the condition is true then the body is executed. Body can now be a single statement or a block anything is possible but if it is a block then all the statements in the body will be executed. After that, after the execution of the body, the execution again resumes from step one. So again the condition is evaluated, again if the condition is false then the statement ends. So the statement only ends when the condition turns out to be false. So otherwise until the condition turns out to be false, the body is executed as many times as needed for the condition to become false. Now the condition should become false in a well written program, otherwise the program is not going to halt and not halting is not an acceptable outcome. We want our programs to produce an answer and stop execution as quickly as possible or certainly in finite time. So this means that if the condition is true originally then the value of some variable which appears in the condition must change during the execution of the body. And only in this case can eventually the condition become false. Just like what we said for repeats, each execution of the body is also called an iteration. So the vile turns out to be a slightly complicated statement, so it is nice to look at its flow chart. So on the left we have the vile statement as a part of a hypothetical program. So there is the previous statement, then there is the vile statement and then there is the next statement. So how is this going to execute? So that is given here on the right. So the previous statement in the program is executed, after that the condition stated in the vile is executed. So this statement over here, this condition over here, if this condition is false then you go off and execute the next statement over here. If the condition is true however you execute the body, as many statements there might be in the body you execute them one after another and after that you again go back and evaluate the condition. And you keep on doing this until eventually at some point the condition is false at which case you can go on to the next statement. So that is how a vile executes. Let me begin with a silly example. So here is the main program in which we have x equal to 2 to begin with and then there is a vile loop and after the vile loop a message is printed. So let us see how this executes. So first as the code executes from the top this statement will get executed. So x will be set to 2. After that we look at the body. So we encounter the vile statement and as you might remember you have to check the body first. So you check is x bigger than 0. So since x is 2 it is bigger than 0 and therefore the body is going to be entered. So when the body is entered the first statement in the body is x minus minus or you want x to be decremented. So this means x will be decremented and the value will become 1. After that you have a see out statement or the current value of x is printed. The current value of x is 1 so that will get printed. So 1 is being printed over here. Now that ends one iteration of the vile loop or one execution of the entire body. But at this point the statement does not end. So at this point so we are going to go back to the top of the loop and we are going to recheck the condition. So at this point x has the value 1 because we decremented it in the first iteration x has the value 1. So 1 is still bigger than 0 so the body is going to be entered. Now again there is a decrement statement. So x will be decremented and its value will become 0. So after that there is the print statement. So the current value of x which is 0 is going to get printed. So that is what has happened over here. So in this statement itself x has become 0 and the vile condition seems to say that do this only vile x is greater than 0. But it does not mean that we are checking before every statement, we are only checking at the beginning. So even though x is actually not greater than 0 we will still execute this statement and a 0 will be printed. After that we are going to go back to the top that is what vile tells us to do and again we are going to check the condition. So this time however x is 0 and 0 is not bigger than 0 and therefore the body is not entered and in fact at this point the execution of the vile ends and you move on to the statement following which is this print statement. As a result of which done is going to be printed. So this was a silly example but it tells us it shows exactly how a vile statement executes. Now it is a good idea to think about a comparison between the vile and the repeat. So let me first observe that anything you can do using repeat can be done using vile. Why is that? So suppose you have this repeat statement. So n is some value x, x, x are some group of statements. So I show you how you will write a program, how you will translate a program which contains this statement and replace it by an equivalent to vile statement. So here is the translation. So instead of that single repeat statement you will have these two statements. So first you will say you will have a variable i which you are going to assign to n. So that same value whatever many times you wanted to repeat is now going to go into i. Now you are going to check is i greater than 0? So vile i is greater than 0 you are going to execute x, x, x but you are going to decrement the value of i in each iteration. So if, so whatever value this had, so whatever integer value so if it had say it was 10 then this loop would be executed 10 times because you would have to decrement this i 10 times only after which it would become 0 and at that point this condition would fail and then you would go on to the next statement. Now this we created a new name just for doing this translation but if this name i is used elsewhere in the program then this might produce an error. So what I should write over here or what I should advise you is that pick a variable name which does not appear in your program and then just use that. So it must be the same variable name that you use over here as you are using over here. So if i is being used then pick a different name. So let me give an exercise. So here x is 306, y is 77, z is 0 and we are doing this loop while x is greater than 0. So we are adding z, adding to z y and we are decrementing x. So I would like you to tell me what is printed at the end. So I am not expecting you to actually execute this by hand but rather what I am hoping you will do is you will maybe do one iteration or two iterations and then you will guess the pattern and you will say that oh at the end this number or this expression, the value of this expression will be printed as z. Then I would also like you to translate this familiar code fragment which uses repeat into an equivalent code fragment which uses vile. So what did we discuss? So we looked at the vile statement and we said that the vile statement says that we need to iterate while a condition that we specify holds. And then in comparison to repeat the vile statement does not need to know how many iterations are there before the execution starts. So in the vile statement a condition has to be specified and the iterations happen so long as that condition holds. Then we also saw that whatever we can do using repeat can be done using vile and of course the converse is not true because in the repeat you need to know beforehand how many times the loop body has to be iterated. So we will take a break here.