 Good morning. Today, we are going to discuss another important control structure which permits us to repeatedly execute statements in a program. To begin with, however, we would look at a few facilities in C plus class which permit us to write sort of abbreviations for instructions which would otherwise become longer. It is not necessary that you use these abbreviations. You can use the full form as well, but this is just to introduce you with the facilities. In the process, we will also look at the complete table of precedences that C plus plus follows while evaluating expressions and also the associative rules for operators at the same precedence level if they occur multiple. We will of course end with some important announcements at the end. So, these are some shortcuts which are available in C plus plus. All that they do is they make instructions simpler to write, but they should be used with care because sometimes the abbreviations are so powerful that you could make a mess of whatever you write and not write semantically correct programs. For example, when you make an assignment operation which actually changes the value of the left hand side object, x is equal to x plus something, k is equal to k multiplied by something, m is equal to m minus 1. So, observe that in each of these statements, whatever is appearing on the left hand side, its value is being modified either by multiplying it with something or by adding something to it or by subtracting something. In all such cases, C plus plus provides for an abbreviated assignment operation which can be written as what is shown in the last three lines of the slide. So, when you say x plus equal to 3.75 star y, it actually means x is equal to x plus 3.75 star y. Is that clear? It is an abbreviation. You need not use the abbreviation, but it is sometimes found even in professional programs. So, at least you should be able to understand what abbreviation as it stands for. There is a special abbreviation when you want to increment or decrement the value of an object by 1, by 1 not by 5 or 7 or 24, 2.75, but by 1. So, observe that you have to write m is equal to m plus 1. What will that statement do? It will take the current value of m, add 1 to 8, find out a new value and assign it back to m. It is called the increment operation. We will soon see beginning with these lectures now when we discuss the for and while control structures that such increment and decrement operations are very frequently required in our programs. For that C plus plus has special provision of abbreviation. These are called post increment and pre-increment operators. So, you have plus plus m, plus plus m is same as m is equal to m plus 1, minus minus n. This is same as n is equal to n minus 1. Is that clear? It is an abbreviation. However, there is possible confusion because C plus plus also permits us to write the same operators in a post form which is i plus plus j minus minus. Curiously, i plus plus also means i is equal to i plus. j minus minus will also mean as j equal to j minus. So, how do you distinguish? For example, both these statements amount to the same thing. This amounts to saying j is equal to j plus 1 and this amounts to saying j is equal to j plus 1. So, you see there is no difference apparently in terms of the intent. You want to take the present value of the object j and add one to it. However, since these two forms are provided, there must be some difference. So, as you can see here as standalone statement, you just use this statement semicolon. This is exactly what it means. There is absolutely no difference. However, there is a difference because these operators can also appear in a larger expression. When they appear in a larger expression, the meaning could be different. The post increment operator means that the original value of the object will actually participate in the expression evaluation. The expression evaluation will be completed and then the effect will be given to the incremented value of that object. So, for example, you have m n j and j is equal to 5 n equal to 2, some arbitrary value. Suppose, I write now an expression m is equal to j plus plus star n. This is a varied expression. As we shall see, plus plus minus minus operators have higher precedence than star slash etcetera and they are called unary operator. You know what is a unary operator and a binary operator? A binary operator is one which requires two objects to operate upon, a plus b. So, that plus is a binary operator. b star c. So, star is a binary operator. Not in the binary number system, but it requires two operators. Any operator produces a value of finite. A binary operator operates upon two operands and produces a value. Unlike that, there are unary operators which require only one operand, j plus plus. j is the only operand and the effect is to increase the value of the error. Minus p. Minus p is also a valid expression. Here, the minus operates as a unary operator. It only changes the value of p to negative of whatever. So, unary binary operators could be there and there is a precedence rule which will make it clear. Coming back to this, then I write this expression m is equal to j plus plus star n. Observe the semantics or the meaning. The meaning says m is equal to j star n. j is equal to j plus 1. That means, first whatever is the current value of j is utilized to evaluate this expression. Result is assigned to m and after that, j is increased by 1. So, effect of j will occur after the expression is evaluated. That is why it is called post-input. On the other hand, if I use the same declarations and same initial values of j equal to 5 and 2 and say m is equal to n star plus plus j. Now, observe that this is a pre-incremented. So, this in effect is equal to or is same as j is equal to j plus 1 followed by m is equal to j star. So, let us look at. So, what will be the value of n in the first instance? I of course, must also tell you what is the value of j. Otherwise, it will be meaningless. Let us say j is equal to n. So, n will be equal to 10. At the end of this, n will be 10 and what will be the value of j? 3. In this case, n will be 15 because j would have increased before taking part in this expression evaluation and j of course, would be. Is this clear? However, if you ask me why it is perfectly all right to use j plus plus or plus plus j as a standalone operation, wherever you want to increment or decrement the value of an object by one, it is not very useful and clear to use such things inside larger expression. So, I have discussed this merely to tell you what the rules are and if you ever see such a construction in any expression in a program, should be able to understand what they mean. That is why you are concerned. I would suggest avoid using such things because there is no reason to confuse yourself and to confuse others who read your programs. Is that ok? That is why the word avoid is written in bold. Here is the table of all precedences. You need not copy this down. I have given a source. There is a internet source where this table is given. Most of the C plus plus books will give this detailed table. Other tables in the books may not be as exhaustive. There is indeed one operator here which we have not yet seen, which is called the question mark operator. Observe at number 9, question mark polar. Who is this fellow? We do not know. We will not bother about it right now. At an appropriate point, we will discuss all the other precedences. The 0th precedence of course is a pair of parenthesis. Any sub-expression you put in parenthesis, that must be evaluated first. Common sense rule. This is followed by the post increment and decrement operators first and this is followed by not plus plus minus minus that is pre-increment, unary plus unary minus. They are all at the same precedence. Then are our favorite multiplication, division and modular operators which appear at a precedence higher than addition and subtraction. I hope you understand what is meant by the associativity rule which is written on the right. What do you mean by left to right, right to left, left to right? Any idea? So what is associativity? Associativity rules come into picture when you have a sub-expression or expression which has operators which are all at the same precedence level. Remember I said that c plus plus is a very exact language. Now, in this particular case m plus n minus 4, you will say it should not make any difference. Whether you subtract 4 from n plus and then add the result to m or you add m and n and then subtract 4 should make a difference. Indeed, it would not make a difference in this case, but in case you have operands which are of different type, you know the rules of c plus plus, it will change integer to floating point if operands are different type. If operands are of the same type, it will actually execute the integer operation etcetera. And therefore, it could actually make a difference depending upon which part of the expression is evaluated first. So, the rule of c plus plus is that if there are operators at the same precedence level, then within those operations the associativity rule will take care of evaluation. So, in this case the associativity is left to right. What it means very specifically is that first this will be evaluated, some value will be found out and then this will be evaluated to find the result. Is that clear? That is what associativity means is very simple. But this is strictly followed, no confusion about it. Just to mention that addition and subtraction listed at number 4 appears after number 3, which is as per our convention of precedence. Within the comparison operators there is a small distinction. All comparison operators are not at the same level, less than equal to greater than greater than equal to are at one level of precedence. So, obviously if there are multiple operators like this, you know how to handle that left to right. The equality and inequality comparison is at the next level of precedence. Remember equality is represented by equal equal and exclamation mark equal for not equal. This is followed by the Boolean operators, logical operators which is logical and logical all. You have to write it as and and or that is vertical bar, vertical bar. Question mark we do not know. The last one is what we just saw, special abbreviations for the assignment operation. So, equal to plus equal, minus equal, star equal, slash equal, modulo equal. These are all these special forms of assignment. That is clear? No, no confusion. Yeah, the pre increment plus plus j. No, that is j plus plus, post increment. What you write? What you see that? So, post increment has a higher precedence than pre increment. So, you know what the modulo operation is? If value of m is 13, m modulo 7 will be how much? 13 modulo 7, 7 ones are 7. The result is 6. This result will be assigned to m. So, this value 13 will become 6, right? All that this statement says is, I can say m modulo equal to 7. Clearly, the right hand side should turn out to be an integer value. Otherwise, the modulo operation would not make any definitions. We shall see that example in one of the things today. Very simply, if I have to combine several different comparisons to generate a truth value. In fact, we might as well discuss this example now because in all our conditional executions and in some of the expressions which we will use for condition definition in our iterative examples, we will need to understand. You remember the bus travel problem where we said the age of a person, if it is greater than 12, but less than 60, then the person should pay whatever. Such an expression, a condition could be written as age greater than equal to 12 and age less than equal to 60. So, we are looking at the age which is given as such a value. If age is 53, it will satisfy this condition and it will satisfy this. So, you will observe that anybody whose age is as per these two composite conditions has to pay the regular. Here what we are doing? There are two separate comparisons. Each one will result in a value true or false and this and and merely says true and true is true, false and true is false, true and false is false, false and false is false. So, this logical and operator does this. These are all binary logical operators, the and and all. So, at one time they will operate upon two operands which are on either side of this. If you have to combine three conditions, you can say this and and this and and this. In fact, you can write a logical expression as complicated as a numerical expression. This and and this, all are this and and not of or or whatever whatever. Only thing is you have to be very careful on the meaning of that expression that you write. But yes, it is possible and all of this will evaluate to what? All of this. This will be evaluated to true or false. This will be evaluated to true or false and this will also be evaluated to true or false. So, finally the value that this logical thing gives you is either called true or called false and we had discussed this. C plus plus actually allocates what value to true and what value to false? 1 for true and 0. Also, inside a bracket, if I write an arithmetic expression t star 7.5, where I should have written a condition, the computer will actually evaluate t star 7.5. Suppose t is 2 or 3, let us say. So, if it is 3, what value you will get? 22.5. Now, this is neither 0 nor 1. So, when C plus plus has interpreted a numerical value as a logical value, then the interpretation is if it is 0, it is false. Anything else true? So, 22.5 will be true. 73.78142 is true. Minus 20,000 is true. Anything 0 is false. With this, we now go back to our problem of finding out maximum of 5 given numbers. You remember this program? We had discussed this last time and we had said that if we had to develop that tree-like structure for comparison, even for 3 values a, b and c, we got into a bit of a mess. If a greater than b, then if a greater than c, then max is a, s max is c. Else, again b and c comparison, etc. And we observed that if we had to do it that way for even whole objects, we would be completely confused. But we saw an alternate way of doing things and it turns out that this way is extendable. It is in this context that we had seen that if I have to find out the maximum of 5 numbers, the best thing is to read a number, assign it to be the current maximum. So, we say max is equal to a and then keep comparing with this max every other number. So, the next instruction says is b greater than max, set max to b. Is c greater than max, set max to c, etc. In terms of actual operations, this particular program may end up doing additional assignment operations which may not be very easy. Consider this for example, the values a, b, c, d, e that you have given are actually in the increasing order. Now, what this program will do? It will assume a to be maximum first. Then it will get b and find out b is greater than maximum. So, it will say max is equal to b. Next number is greater than b. So, that means it will end up doing every time an assignment which will be completely erased by the next assignment and so on. Point is you will get correct answer. The problem is this. Many problems will have large number of values. So, take this case. You have to write a separate instruction for comparison with every new number. So, suppose instead of 5 there are 10 values, what will you do? In a, b, c, d, e, f, g, h, i, j and then you will write those many instructions. Suppose you have 100 values a, b, c, d, z, then a, a, a, b, a, c, a, d. Of course, you can construct very many names, but you will have to write first read the first number, assign it to max and then 99 different instructions. If you have to compare 1000 numbers, the number of instructions that you write in a program is a major, not only of the length of the program that you write, but generally of the complexity of the program. Larger the program, it is expected that it is solving a relatively more complex problem. The major use in professional programming parlance is called LOC, not line of control, but line of control. So, when people say this programming system, not just a program, but maybe 100, has 50,000 lines of control, respectable amount. This programming system has 200,000 lines. A typical banking application could have 2 million lines of control. The more complex applications such as those which handles spacecraft controls, those which handle very complex what we call data mining problems, could run into multiple millions of lines. And in general, when you tell somebody that I have written a program these many lines, the amount of complexity is expected to be reflected by that. Now, suppose you meet some professional from PCS or Infosys and you tell him I have written 1000 lines program. He will be impressed, right? Oh, 1000 lines. What does it do? It finds maximum of 1000 numbers. So, you see, there is some problem here. We cannot afford our program to become as big as the number of values it handles. The program has to be shorter, but then shorter program will execute only one, two, three, four instructions that is not sufficient, which is very obvious that if 1000 numbers are given and maximum is to be found out, 1000 comparisons have to be made, 999 or whatever. So, somehow you have to figure out a way that you give one instruction to the machine, say compare, and you want to give another instruction to machine saying keep on doing this, keep on doing this for all that. That is called the repetitive control. And for that, not only C++ but every programming language has instruction. The requirement to use such instructions, we do not know what those instructions are, but whatever they are, the requirement first is to figure out a pattern of instructions which can be repeatedly executed. A pattern of instruction, somehow each execution of that block of instructions should handle different value, but the instructions themselves must be same because otherwise what do you say repeat? Let's go back to the previous slide and see. There is nothing that is repeatable here. Every instruction is unique. The first comparison instruction compares max with B, the next one compares to C, next one compares to D, etc. These are not actually usable directly by us. However, so in short, we want to write an instruction where the same location can be used at different execution times to represent different values. We shall see how we will lead to that. This was our original program. Now we will slowly modify this program to come to discovery of a repeatable pattern. So the first thing that we do is rewrite the program to take an input only when it is required. What is the difference? Earlier we were reading all values first and then evaluating them against the maximum comparison. Now when I read all values first, I have to keep them in separate identities necessarily. All that I am saying is instead of reading all values first, if I read a value when it is needed, then how will my program look like? It will look like. Do you understand what we have learned? We are first saying read A, that is okay. Assign it to max. Now I need to compare next number. So I will say read B now. Compare it with max. If it is greater than max, assign max to that B. Assign that B to max. Same thing with C, same thing with D, same thing with C. So whether I have 100 or 1000 numbers, every time I need a number I read it, examine it, take a decision on the value of max and go ahead. How do I know whether things are proceeding well or not? I will do one more modification. I will add an output statement after every comparison. We were here. So this is clear that we will read a number only when it is required. Now we observe something very important. We observe that while each instruction is actually different, I am looking at the set of those four comparisons. Each instruction is doing the same job, getting a number from outside, comparing it with max and reassigning max if required. In which case, why is it necessary to use B in one case, C in another case, D is another case? We presume that we are interested in finding out only the maximum of given numbers. We are not interested in individual identity of numbers. If that is so, then I do not need to use different names for these. So I further modify that. Now look at how the program reads. The first instruction says C in A max is equal to A, perfectly fine. Now subsequently, I have got input A if A greater than max, max is equal to A. Again I have got input A, A greater than max, max is A. Does this make sense? Will it work? Let us examine this. Let me write here the two locations. This is A and this is max. And let me hand execute the program. Assume that you are the C++ machine, C++. You are executing this. So let us go through this program. It has been compiled and therefore, int A and max, that first definition instruction, has resulted in these two locations being created. Now we start the program execution. C in greater, greater A. What will happen here? A will get what value? Please note that the input values are 3, 9, 2, 17, 5, return of the top. So we expect those values to be given in that signal. The value 3 will come and sit inside A. We will also write it here that A becomes 3 at this state. What is the value of max? Currently none. But when you execute max equal to A, it will get the value 3 and max will also become 3. Next statement. C greater, greater A. What happens? The next value is 9. This value 3 will vanish and 9 will come here instead. So A becomes 9. If A greater than max, is A greater than max? Yes, 9 is greater than 3. So what should happen? Max is equal to A. This will go and you will have 9. Next statement. Again C in greater, greater A. What happens? Value in A is 2. So 9 goes away. This becomes 2. Is 2 greater than 9? No. So the max continues to be 9. Next statement. Again input A, 17. Is A greater than max? Is 17 greater than 9? Yes. So what will happen? This will now go and become 17. I have this here as 17 and this as 7. Last instruction. Input A. What is the value? 5. Is 5 greater than 17? No. So this will remain 17. A will become 5. Is that clear? We actually have a very dumb way of doing things. But I did spend time to go through this because I want you to understand the importance of such a hand execution. Particularly when you write complex programs or when you are writing for the first time some new instruction structure in C++ which permits conditional or repetitive execution of statements. It is useful to hand execute a program with some simple value to know whether your logic is correct or not. In this case the logic is correct. It has been proved. But now we have got a fantastic deal. We have now written a program where there is a series of instructions which are identical. The instruction itself is same. I write 4 instructions like this and I end up getting maximum of 5. If I have to find out the maximum of 100 numbers I have to write exactly the same instruction 99 times more. Observe that the instruction is same. But every time an instruction is executed it is operating upon a different value. Because I am ensuring that that group of 3 instructions actually start with a new value every. This is the crux of our identification. Now identify a block which can be executed repeatedly. In this block of 3 instructions I add a fourth instruction now. An output statement which has been kept after the assignment to max. So observe how the statements are written. C is equal to a if a greater than max max is equal to a c out less less max. What will this program do now? It will do the same thing, find out the maximum of 5 numbers. But every time it executes a comparison it will give you what is the current maximum value. What is the current maximum value? Since our job we started off by saying that we want to identify a group of statements which can be repeatedly executed. I have rewritten these in a slightly different form. C a b max c in greater than a max equal to a this is sort of the initial rig model. After that what is written here? C in greater greater a if a greater than max max is equal to a oh god. Is this correct? Just compare this statement. This says if a greater than max max is equal to a c out less less max. This statement says if a greater than max max is equal to a c out less less max. But what is the logical difference? Are they meaning wise same? Whether the condition is true or not? Does everybody get it? In this case the value of max will be printed only if the new value a which was compared with max was greater than the max. Only if it changes it will change. In the earlier case whether it changed or not the max value would have been printed. Since we want to repeat this block of statements and we do want the maximum value currently maximum to be printed every time. What should we do? So in fact I should write this as if a greater than max I could have written it by the correction that I have indicated here. But this is the better way of writing because indentation remember indentation of my lines is supposed to indicate to me the block structure. And the fact that c out less less max is at the same indentation level as if it means that only when you finish the block of if and come out you have to do this. Same mistake here. What I have done? Why I have put that in a blue mistake? Because these are the set of instructions which I want to repeatedly execute. Now do you understand the power that I am seeking from c plus plus? Today I don't have that power. If I have to find out the maximum of 100 numbers I have to write this 99 times that same. But if I could somehow tell the machine look Baba I have got these statements. I want you to keep on executing them till I keep giving you value. This is called an infinite way. If there was an instruction in c plus plus we said keep on doing this. Then I will set up an iteration which will keep on doing this. But do I want it to be done till eternity? No, I want to stop after my values are over. I want to stop when a certain condition is met. In this case the condition is that if there is no more input I want you to stop. I would therefore like a structure which looks like this. I want to set up an iteration. You can see this control going back. But I don't want unconditionally to keep on repeating the block. I want to set up a condition at the top like a doorkeeper. That doorkeeper will first examine that condition. Is that condition satisfied? Is the value true? Yes. I will say go ahead and execute this block and come back again. So anybody who gets in through my door and goes into this house called repeated block cannot escape by the back door. The back door again leads to the front door and the same gatekeeper is sitting. Now you will agree that if I have such a control available then I could use it to set up a repetition or iteration as it is called that I can repeatedly execute the same set up. This structure I would like to have a simple wording for it. Just like for conditional execution. If condition do this, else do that. I have a structure. Similarly I want to have a condition, I want to have a statement that while this condition is true, if repeatedly executing this block the moment this condition becomes false, get out and go to the next step. Such is the structure available, the fundamental iterative control structure which is called the vibe to reflect exactly the meaning that we have in mind. The vibe structure syntax is something like as shown here. There will be some statements before the block. The block itself is written here. This is the block. So for example our statements C in greater greater A, if A greater than max etc, they form the block. All that I am saying by this statement is while this condition is true keep repeatedly executing the block. Every time you execute the block, come back and again check the condition. If the condition is true, go back again and do it. When the condition becomes false, get out and go to the next step. Can you see the power of expression here? So the semantics of the meaning is given at the end. The condition is evaluated at the beginning of each iteration. The block is executed if the value is true. Else the program will exit the loop and go to the next step. So it will jump out. How to set the condition? You have to be careful when you set that condition because you are designing that condition. The condition is going to be tested every time the repetitive block is to be executed because the meaning is while the condition is true, do it repeatedly. After every repetition condition is going to be checked. It should evaluate to true for continuation of the loop and if it evaluates to false, it will exit and jump out. Now some value, some component in that condition must change after every iteration. If it doesn't, if for example I write a condition inside the block, I have a statement like x is equal to something, y is equal to something, z is equal to something. Suppose these are exactly three assignment statements in the block. Now can you imagine what will happen? First time when I come to this file, whatever is the value of p, that will be tested against five. If suppose value of p is minus three. Well the condition is false right at the beginning. You don't get into the iterative block at all, you get out. But suppose p is say seven. Now what will the c plus plus do? Seven is greater than five, condition is true. Get inside. What it will do inside? Find out something for x, find out something for y, find out something for z and then the gatekeeper says come back to me again. So I come back to him again. What condition I will check? Nothing to do with x, y or z. Is p greater than seven? Of course it is greater than seven because it was greater than seven. Yesterday it will be today, it will be day after tomorrow. It till eternity p will remain seven. What I have got in spite of writing a condition, I have got an infinite loop because that condition is not changed. If I want that condition to change, then some component of that condition, here the only variable component of that condition is p. Therefore somewhere p equal to something must also happen. If it does not happen inside, I have got a wrong. This is stated here in words. If components of the conditions do not change in any iteration, it will always evaluate whether true or false. If it evaluates to true, the control will keep repeating the loop in final. But if it evaluates to false, it will jump out without evaluating the branch even once. Is this clear? This is both the power and the care that you have. I have written a program now in A max. Max is equal to zero while one, C in greater greater a, if a greater than max, max is equal to a, C out less less max, closing branch returns zero. This program I think is written. This is what it tries to do. It has this iterative loop. This will be executed repeatedly. If the condition is true, this is the entire problem. This program by the way has the corrected syntax for C out. Let us read this program carefully. Int main, int a, max, max is equal to zero. So far, good. While one, we will discuss that. While a, this means I am specifying repeated execution. The block to be repeated is from here up to here. What is stated inside the block? C out enters the number, so I am getting one number. C in greater greater a, that is input the number. If a greater than max, max is equal to a. So max gets reassigned if a is larger. Output max is whatever the current max is. And then go back. In fact, the arrow should not be here, the arrow should be here, saying that this is the place where I go back. But after going back, what will I do? I will again examine the condition. If the condition is true, I will carry on. Otherwise I will get out. Now what does while one mean? We just saw that some time ago. Instead of writing a condition, I am writing a constant. And that constant is one. If you interpret one as truth value, was it false or true? True. So whatever I have written, I have written an infinite loop. While one do means till eternity keep on doing, I will come back. When will the return zero statement executed in this program? Can someone tell me? I have said return zero. I want to go back to operating system. Because only when I go back to the operating system, I will get my favorite dollar where I can give a new command. So when will I go to the operating system? Anybody? When number? Less than zero. Why? No, statement will never be false. While one, that one is translated to a constant one during the translation phase. Nothing in the world can change that value. It is an infinite iteration. When you execute this program, this is what will happen. Enter the number three. Max is three. Enter the number nine. Max is nine. Enter number two. Max is still nine. Four. Still nine. Five. Six. Still nine. Eight. Still nine. Ten. Max is ten. Twelve. Sorry, 23. Max is 23. Twelve. Max is 23. But at the end, do you see a dollar anywhere? It says enter the number. It has become a booth. It will keep on asking you for numbers endlessly. Do you understand that this could create a problem? But do you also understand the power that you have now? You can actually make the machine keep on working infinitely. As we shall later see, there are other mechanisms of getting out of infinite loops. And it is not uncommon in some cases to find that people actually deliberately design an infinite loop but design other mechanisms of getting out. To give you a hint, we had seen the switch statement. In that switch statement, you had a statement called break. At that time, we interpreted break to mean get out of the entire switch block. Break is actually such a powerful statement. When you use it inside any loop, not necessarily infinite loop, it takes you out of the loop. But that we shall discuss later. Our objective today is not to see that. Our objective today is to rather emphasize that in most of the problems that we solve, we should not write infinite loop because that is dangerous. But if I don't want to write infinite loops, then what do we write? We have to indicate end of input. How do we indicate? Here is a modified problem. I change the problem itself. I say the problem is to find the maximum in a set of positive integers given as input one after another. And now I add, stop when value zero is given. And the problem says after reading each value, output the current maximum value. Same program as earlier. But now what is the difference? I am saying I will keep on giving values three. But all these will be positive integers. The moment I give zero, which is not a positive integer, terminate, stop. You see now this problem statement invites me to immediately come to the conclusion that the condition for continuity should be what? The number is non-zero. As long as the number is non-zero, I will keep continuing. The moment zero number comes, I should terminate. This then is the problem. What is the difference between this program and the previous program? The only difference is the while condition. The while condition says a not equal to zero, keep on doing it. While a is not equal to zero, keep on doing this, keep on doing this, keep on doing this. Every time what it will do? It will read a number, check it with maximum, replace maximum lesser. Read another number like the previous. But this is not a boot program. The moment I am tired, my lap time is over or I am hungry, I will give zero and get up. And the last max printed would be the maximum value that is there. Now can you see how many lines are there in this program? One, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, including closing brass, etc. How many executable instructions have you written? Start with c, n, n greater than a, max equal to a, while a not equal to zero if a greater than max, whatever. Eight, ten lines program. But now do you understand that legitimately you can say I have used the power of c plus plus to write just a ten line program which can find maximum of million numbers. It doesn't matter how many. Million positive integers, this particular. Is this clear? Because this is the crux of the repetitive instruction handling mechanism of c plus. If you understood this, you will be able to understand various different variations which we shall be seeing very shortly. Here is another program. Instead of finding out the maximum, it says find the sum of a set of positive integers given as input one after another. Stop when value zero is given. Do you notice the similarity between this program and the other program? Earlier we were finding out maximum of given number. Now I have to find the sum of given number. But I know exactly how to handle it. I can start with some zero value as sum because sum could be zero if there is nothing. And then I can use the same logic. Read a number, add it to some. Read a number, add it to some. As an added attraction, I don't even require any if statement to compare that number with anything because I have to keep adding that. And when do I stop? The moment somebody gives me zero, that is the indication to me saying my numbers are over. Consequently, this is the program to finding the sum. I have to go back to what we had done except that it says int a, sum equal to zero semicolon. Observe that max equal to a had to be written as a statement after reading the first number. But initial zero value to sum can be given as a part of my declaration itself because it's an initial value. So int a, sum equal to zero semicolon means set two locations a and sum and put zero value in sum. Now I read the first number while a not equal to zero nu sum is equal to sum plus a c out enter the next number c in greater greater a Observe that the condition is while a not equal to zero keep doing it and observe that every iteration executes an instruction which reads a new value for it. That means there is a chance for this condition to become false and therefore this is not an infinite I can get out. Let's just quickly execute this program. Let's assume some input. Can somebody give me a values? Let's say these are the input values okay. So what will happen now? Yes first value a first int a I don't know what is there is a question mark and when I said sum equal to zero is a value zero. This will happen before the program execution begins because this is part of my definition int a comma sum equal to zero to reiterate what we had once said that this is a instruction to the compiler the compiler while not direct compiler directive but the compiler while translating the program will reserve two locations one for a one for some and because I have said sum equal to zero it will put zero. I have not said anything about a a will continue to contain some and define that. Now the program execution starts see in this is the first statement that is being executed see in greater greater a what will happen at this stage the value 28 will be and 28 will go here eliminating the question while a not equal to zero is a not equal to zero yes 28 is not equal to zero I will enter the iteration here I will execute this statement sum is equal to sum plus a so what will sum become now next statement says enter the next number see in a so what will happen now a will get another number this 28 will go 4 will come here with this value 4 I come back now and when I come back I don't blindly enter the iteration I again check the condition is a not equal to zero because 4 is also not equal to zero I will get down again add this a value to sum what will sum become now 32 get another number for a which is 3 so this 4 will now become 3 and after becoming 3 I will come back again is a not equal to zero yes a still not equal to zero I will go inside add this 3 to sum sum will become 35 read the next number 8 which is I get zero here go back here this time the watchman will say sorry you got a number zero stop get out and when you get out you will say sum is sum what is the value of sum 35 will be free again I reiterate it's a good practice particularly when you are learning new construction experimenting because you know you already have a problem you have to design a set of instructions it can be repeatedly executed you have to design a condition which will facilitate repeated execution but it will also permit you to get out so it is possible that you might make some logical error it is useful to execute your programs like this and confirm things are working ok yeah here is a question is it possible to define a range of numbers like I want a to be between this and this I will add to this question what if I want to add three arbitrary numbers which are given as 13, 0 and 7 this program will never add at the moment it says 0 I am gone it will take 13 sum is 13 end of the matter if 0 has to be included I don't know what to do this program also does not permit me to add conventional fractional values 13.5 17.289 what if I want to add minus 21 luckily this program will work because in this program we are not checking whether the value is less than equal to 0 we are checking whether it is equal to 0 or not actually it will take minus 21 and it will add that and finish however if somebody has said that I will give you positive integers then it is your duty to want that person that look friend what value you are given is invalid as per your problem statement so ideally you have every right to say if I see minus 21 shout shout shout and get out I am not supposed to do if you want to do any such thing let's solve the problem of a range bound numbers suppose there are numbers in a range and I want to add them how could I do that is this problem fairly clear somebody wants to add various numbers which are in the range minus 74.5 to 128.9 and I want you to write a program how would I write the program forget the hash input something something using this thing we start writing the program can I say int a no because the numbers are fractional so I will have to say what float a sum can I initialize it to 0 yes of course I can but it is better to write 0.1 just to make it simpler now I can't write while because to kickstart while I require something what is that something is going to be let us write down the condition a greater than equal to minus 74.5 and and a less than equal to 128.9 we agree how powerful the while is one short I have taken care of these conditions if this is true I want to do what sum is equal to sum plus a here is a problem what is the value of a to begin with we don't know what the value is and we can't leave it undefined so I must read the first value before I begin the iterative thing so remember the word kickstart like a scooter you have to kickstart a while iteration has to be kickstarted otherwise the scooter will not start it will fall out of the loop or you will continue to execute the loop in less we can't leave it to charts that means the first number has to be given explicitly by a separate instruction that will kickstart the while once you come inside it will add some now what should I do can I say end of the thing no because then it will keep adding the same a without changing anything I have to read the next number simple iteration read the first number if it is within the range go down add it to some read the next number come back to the watchman get the new number examined the range again go down add it read the third number keep on doing this till you get a number which is outside this range when you come out here you can say see out here is an assumption that we have made any number which is outside the range prescribed for the valid values will terminate the iteration should write a comment any input invalid input terminates program whether this is fair this is correct we don't know it's an interpretation suppose I gave you this problem and said I want you to add numbers between this range and they are large numbers you write this program and I ask my assistant to execute this now while recording my experimental results somewhere some value in this range actually if such a invalid value comes what will be an interpretation of a scientist discard that and go ahead but you are not permitting me to do that the moment I make a mistake and give a wrong value say thank you so much go home is that fair I would like an indication about end of input value which is not based on a specific value for example suppose I give you a program I give you a question saying write a program which will add any numbers of numbers that I give any number of numbers that I give whenever I give numbers you add now every legal number is a valid input so you can't base your termination criteria on that value often you still ask are you sure you want every number he says no the numbers are generally between 10 to the power minus 9 some such now while number is greater than 1.0 10 e 9 or 10 e minus order there could be other mechanism of informing the machine that input has ended later on when we discuss file processing we will discuss various mechanisms which will actually tell us that you can terminate the input and indicate to your program if you press control D for example is considered as termination of input in the unix operating system the operating system can give a signal to that program please remember when you give a value you assume that that value is directly going to a program nothing goes directly to the program the operating system dumbo is the big boss all symbols that you send all symbols that come out will come out through the operating system operating system is capable of informing incidentally if you have an infinite loop okay there is a way to directly approach operating system dumbo so when you see on the screen perpetually things are happening you got into an infinite loop press a special key called control C so keep the control key pressed and see that control C is an SOS signal to operating system there sir my program is misbehaving please take me out of this the system will terminate that program for civilian combat there are such mechanisms which we shall see at a later time but currently we understand that we will have to decide on setting up of a proper condition for this iterative execution such that the execution can terminate after solving the problem here is another construct here is another construct in C++ do something while condition is true then it look very similar to why something keep doing something it look similar the difference is as follow this is actually my block of statements to be repeatedly executed the logic here is like this I come here and straight away enter the block so all the statements that I want to repeatedly execute are straight away enter however after completing the block I check the condition if the condition is true I go back and repeat the block if the condition is false I follow so what is the difference between this logic and the logic of while condition block the difference is that here the condition is checked after one execution of the block and after every execution in short my watchman is not sitting in the front of the house he says you enter the house finish your task but when you get out I will examine the condition and I will decide whether you go back again to the front to repeat or you get out in plain English it means that if I use a do something while condition construct then that something will be executed at least once always at least once always whereas while condition something it may not execute at all does this give you a slightly simplified way of finding some of given numbers let us again look at some of given numbers which are let's say positive integers or non-zero number or any range so let us quickly write down that program so how will I write this is what I want to write do agree do something while a not equal to 0 now earlier while a not equal to 0 was being examined at the top so I had to actually read the first input directly before going in I don't need to do that I know that when I say do something it will always be done at least once so I can actually enter this loop I can now say c in greater greater a so I take a number then check is it 0 if it is 0 I should get out notice that in this case even the last number 0 which I gave will actually get added to the sum and then only I am checking the condition in this particular case it does not matter because adding 0 to any sum does not change the value of the sum can that normally be true suppose I was finding I was able to find the product of given numbers I will start with product p equal to 1 and given numbers are non-zero and if I did it like this instead of sum equal to sum plus a I said p is equal to p star a so I will meticulously compile numbers given 2, 5, 7, 3 etc etc I will keep accumulating the product finally a 0 will come and the product will become 0 and I will terminate that is not correct so you have to be careful what problem are you solving which construct do you use should you use while something do or should you use do while something this is a matter of choice the same logic can be implemented using either this or that structure just as if then else as I mentioned to you could be used to implement the same logic in different ways similarly this is also we stop here I have just indicated some additional problem instead of saying add numbers till you get 0 I have a problem I know how many numbers I want to have let's say there are 468 students in CSW to find the average I will give exactly 468 marks and find the average I know that number can you write a program which will read a value of N and say add N number find factorial L I just want to think about it in the next lecture on Friday we will actually be discussing these and several other interesting problems ok thank you