 In the last lecture, we introduced the notion of conditional execution after looking at the evaluation of expression. Specifically, we looked at the details of data types, how the conversion happens between different data types, if they appear as a part of the expression and they have to be operated upon. There were some shortcut assignment operators which I had introduced, thanks to a particular question which was asked in the class which actually I had formulated as a quiz question. When I tested it, I found out that there is a error in the representation of a composite expression involving pre and post increment. So, today I would like to first discuss that and make those corrections. More specifically, I have written down all the rules of precedence and associativity which apply to C plus plus expressions. There is a site reference which I will give. You can refer to a much larger set of rules because there are a large number of operators which we have not yet introduced ourselves to. We will then continue looking at the problems requiring conditional execution and examine the basic control structures in C plus plus which is essentially if, if then else, if else and if else if. We will specifically look at need for writing our programs properly such that the indented lines indicate very clearly the pairing of ifs if there are multiple ifs or else's. We will look at nested if statement. There are other forms of if statement. One is called the switch statement and the other is a special operator called a question mark operator. We will not examine these things in this class. I have just put this line here because in the precedence order I will be indicating where exactly is the precedence for the question mark operator. Towards the end, we will look at the need for solving problems using iterative programming. So what are iterative solutions? Why do we require iterations? And how exactly a simplest of iteration could be specified in C? At the end, we will make some announcements. So we are revisiting the increment and decrement operators. As I mentioned, if you want to increment a variable by just one or decrement a variable by one, you would write the normal assignment statement of this form. Say m is equal to m plus r. As we all know, this is not an equation. This is an assignment operation and this will increase the current value of m by r. C plus plus provides special increment operators and decrement operators which are plus plus and minus minus. When they appear before a variable, the net effect is exactly same as if you had written say m is equal to m plus 1 in this case or n is equal to n minus 1 in this case. The increment and decrement operators come in two flavors. One is known as the pre-increment operator such as this. The other is known as post-increment decrement operators written like this. This is again same as i is equal to i plus 1 or j is equal to j minus 1. So if you write such statements as plus plus m semicolon or m plus plus semicolon, the net effect is identical. There is no difference whatsoever. The value of m is increased by r. However, if such representation is used as a part of larger expression, then the impact within the calculation of the value of that expression is different. So as standalone statements I repeat, both forms achieve exactly the same result namely increment or decrement the variable value. As we shall see, there could be tremendous confusion if you use these operations as part of larger expression. We will briefly examine what is the impact, but the net conclusion that we will draw is we will never do so in our programs. If at all we need them, we will use them as standalone operations for increment and decrement as a shorthand operation. So when such usage appears in larger expression, the value that is used while calculating the larger expression is different in the cases of these two representations. If it is the post-increment that is say m plus plus or j plus plus, then what happens is that the original value of m or j is utilized while evaluating the expression and only after the expression evaluation is complete, the value of the concerned variable is incremented or decrement. On the other hand, if I use pre-increment form plus plus j or minus minus j, then the increment is first carried out even before evaluating the larger expression and it is the incremented value which is used to evaluate the expression. So here are two examples to illustrate that. Consider m n j as integers and j is given arbitrary value 5 and n is given value 2. If I write m is equal to j plus plus star n, which is a valid legal expression in C, this will be interpreted as m is equal to j star n semicolon j plus plus, meaning thereby that when this expression is evaluated, the original value of j is used to multiply it by n. Since j is 5, n is 2, in this case 5 into 2 is 10, m will get the value 10. It is only after this assignment operation is completed, expression evaluation and assignment operation is completed, j will be incremented to j plus plus. As a matter of fact, if you look at the exact sequence internally, in all probability, the moment this value is substituted for this larger expression, j may independently be incremented and kept aside, because it does not participate in this expression anywhere else. So, whether this exact increment happens just before the actual assignment takes place or it takes place afterwards, it is of no consequence because at the end of execution of this entire chain, you will have m as 10 and j as 6 because j was 5 original. On the other hand, consider this alternative example where the values are same as before, but the expression is written as n star plus plus j. Now, n star plus plus j is same as plus plus j, m is equal to j star n. That means the j is incremented first. So, j will become from 5 to 6 and it is this incremented value 6, which will participate in the remainder expression, which means n is 2, 6 into 2 is 12. So, in both cases, at the end of the completion of the execution, j will become 6 anyway, but in one case m will be assigned value 10, in the other case m will be assigned value 12. So, effect of j will occur before the expression is evaluated. To very clearly understand when we write an expression, how exactly the expression will be evaluated, which operations will be done first, etcetera, etcetera, there is actually a rule book, which defines the precedence of various operators and within that precedence, it defines the associativity. That is if you have multiple operators at the same precedence, which one is done first, which one is done later. We have seen some example last time. I hope you will remember that if I have operators such as multiply, divide, modulo, all these operators are considered to be at the same level of precedence. In which case, if I write a star b slash c, what will be the order of execution of these? First, a star b and then the result will be divided by c. On the other hand, if I were to write a plus b star c divided by d minus q, how will this expression be evaluated? The plus is at a lower priority, the minus is at a lower priority. All these operators are at higher priority. Therefore, all of this will be executed first. Within these two operators, star is coming at the left because within the same priority, these operations are executed left to right. So, this will be executed first and then the result will be used to divide it by d. After that, you get some result here. So, let us call it r. It is a temporary internal value. Now, what you have is a plus r minus q. So, it will a plus r will be calculated first because between plus and minus again, left to right associativity overrides anything else. You will calculate this, then you subtract this and you will calculate this. I hope you remember the most important observation that we made. Namely, we have considered two types of numerical values. One int, which represents integers and the other float. If both operands are of the same type, then the operation is carried out as if it is a int operation or float operation. So, please remember that if b by c, suppose b is 2 and let us say c is 5, then c is 5. So, if b is 5, then ordinarily 2 by 5 you will write as what? 4 by 10, which is equal to 0.4. However, if b is declared as int and if c is declared as int, the operation is carried out strictly as integer operation. So, 2 by 5 will result in 0. Very, very important to remember that. If you wanted this operation to be carried out as a real normal evaluation to 0.4, as at least one of these operands must be floating point. The rule is that if there are two operands and if one of them is float, then the other is converted to float before the operation is carried out and then only calculations are carried out in the float. But if both operands are int, the operation is integer and the result will be integer. You remember all that? Why? So, we come back to this discussion on pre and post increment and this is what would happen in case of pre increment or post increment appears as a part of larger expression. And finally, the point I will repeat, which I made some time ago. I think it is patently silly to write an expression of this kind. All that we are trying to show off is that we know some tricks of the trade-in-sea programming. This program is highly unreadable and therefore, if at all I want to do first increment j and then multiply it by n, then I will probably write it like this. And when I write as two statements instead of one, notice that whether I write plus plus j or j plus plus, it does not matter. That is a standalone expression. It will be evaluated to increment the value of j by one anyway and that value will be. In general, in all our programs, we shall explicitly state that we will never use increment operators, post or pre as a part of the expression unless it actually helps significantly reduce either the programming effort or whatever. In fact, one could go back to the previous slide and say, why do I write this at all? Why not m is equal to m plus 1? Any speculation on that? After all, both are equivalent. Whether I write plus plus m or m plus plus or whether I write m is equal to m plus 1, both are equivalent. Plus plus m will require less memory is the answer. The answer is not correctly because memory required is exactly same. There is one location allocated to m. So, that is the memory that has been allocated. Oh, you are talking about the line in our program. Please remember that when your program is executed, it is not those lines which you write are executed. They are first translated into machine language by C plus plus compiler. So, you are possibly talking about the length of the instructions in the translated program. In some sense, you are right, but it is not to do with the length of the instructions in the translated program. It is the instruction type which is generated. You remember when we looked at Dumbo, we commented upon the registers that Dumbo uses for temporary calculation. Whenever we give a statement like m is equal to m plus 1, invariably, Dumbo will have to go to the location m, get the value from memory into local register, add one to it. So, it is a normal addition operation and then put the result back. The addition operation will take certain processor cycles. Let us say 200 nanoseconds. However, in most machines, there is an instruction to directly increment the value in a memory location by one. That operation is carried out by adding one bit to the right most bit. That is addition of one. It is an increment operation. It is often done in hardware. So, as a result, this instruction may execute in as much as half or one fourth time it takes to execute this instruction. The meaning is same. So, consequently, if I write it like this, it permits compiler to generate more efficient code, more efficient instructions. This is not at all relevant when we do computational problem solving using our PCs or larger machines, but when you write programs in embedded systems, let us say in your algorithm you have to increment something as a part of the control of a washing machine. Then every nanosecond may count because you might want to execute some million instructions before something happens. So, when you are embedded software, you would probably be more required to use this. However, nowadays it has become a standard practice that when you want to increment or decrement a variable, you use this in program. So, most professional program that you will see will use this. The usage of this kind shown in the next slide will be rarely seen anywhere except in the textbooks as tricky examples. The only purpose to discuss this was to ensure that you understand that such usage is possible. It is technically correct, but programming ethics wise it is bad program. This slide shows the precedence and associativity rules. I have quoted here a site reference. Notice the word name of the site cppreference.com. So, this is a site which gives you reference of all the rules of C++ programming as defined in the C++ programming standards. In fact, what I have quoted here from the wiki on that site is the operator precedence pages. As a matter of fact, if I printed those pages, there will be some two pages, both sides kind of because there are so many operators in C++. We have not seen most of them. So, if you do look at this site, please avoid any confusion because you will see operators which do not make sense. For example, we know that and and stands for logical and that is how we have defined the logical and operator, but you will find an operator called just and. Now, that and is actually a bit wise operator. Since we have not studied the value representation in bits, we do not know what those operators are. Consequently, do look at this page, but try to look at what I have done is I have extracted the operators which we have visited, we have discussed and I have only listed their precedence and the associative rules associated with that precedence. So, for example, if you look at comparison operators, they all stand at fifth precedence less than less than equal to greater than greater than equal to and they have an associative left to right. That is if for any reason I use two comparison operators in an expression, then this is how they will be evaluated. On the other hand, equal equal and not equal in comparison are lower than five and their associativity is left to right. In general, you will notice that associativity is always left to right within the operators of the same precedence except for the not and pre increment operators and unary plus or minus. And similarly, for the question mark operator which we have not yet seen. For the question mark operator, it is unlikely that you will ever have two question mark operators in succession in any expression and therefore this associativity is of academic interest only. However, this associativity in a complex expression might have to remember that it is done from right to left. That is you evaluate Ulta the expression when you have multiple operators at the same precedence. I would like you to study this carefully. These slides will be on the web so you can look at it. But this reference you should be able to access and just for the sake of curiosity, you look at this in detail. Notice that the post increment and decrement are the highest priority. The pre increment and the unary plus or minus are the next higher priority. All our arithmetic operations that we are familiar with, multiplication, division and module operator, they are all at the same priority third and they are evaluated on either side. Please remember that the module operator will take two integer operands on either side because module of 2.5, module of 7.3 does not make sense. Addition and subtraction is defined at the next higher level. All comparison operators except equal and not equal are at this level and the remaining two are here. The logical hand is lower precedence than this. The logical or is lower precedence than that and the assignment operations, all assignment operations including equal to plus equal to so plus equal to minus equal to star equal to question mark equal to percent equal to these are all assignment operations and they all have priority 10. Further parenthesis, any pair of parenthesis has the highest priority. In fact, that is how we ourselves understand what will be done first, what will be done later by putting nested parenthesis. Please note that parenthesis should be heavily used in your large expressions. If you have any doubt about how the compiler will evaluate the expression, it is better to specify what you want to do first and what you want to do later by putting them in appropriate brackets. Here is an example A plus B star C slash D minus M percent N. Just look at this plethora of parenthesis and tell me whether these parenthesis sub expressions represent the correct sequence in which the expression will be evaluated. Paranthesis always work inside to outside. The answer is no. So, where is the mistake? Between plus and minus, the addition side should be evaluated first. According to the brackets, the subtraction sign is being evaluated first. Which sign? The minus sign. So, this is wrong. Let us look at the remainder of the expression. B star will be evaluated first. There is no doubt in anybody's mind because you have star and slash and the associated is left to right. So, B into C will be evaluated first. The result will be divided by D. So far so good. Similarly, in this entire expression, M modulo N, modulo has a higher priority than minus. So, modulo will be evaluated first. Again, no problem. M modulo N being evaluated first. The issue is after this portion is evaluated and this portion is evaluated, let us call this result R1 and the second one R2. The expression will reduce to A plus R1 minus R2 and the point that has been correctly made is because the associativity is left to right, A plus R1 will be calculated first and then the result will be added to R2. Simple? Okay. Here is another example which is similar to what we have seen here. Sum is equal to sum plus x. Please note that I can also write this as sum plus x because I am changing the value by x. However, the point is in this particular case, y does compiler does not take it as sum is equal to sum plus x. Please note that is a valid expression. x is equal to y plus x or x is equal to y plus z. x is equal to y is also an expression. What is the value of such an expression? Whatever is the value which is assigned to the left hand side? Why doesn't compiler then take it to be like this? It is interpreted like this but not as anything else and that is because if you go back to the previous slide, the assignment operator is at the lowest priority and therefore when you have n number of things on the right of assignment operator, all of those will have higher priorities than equal to and therefore this will always be interpreted as this. It will always be interpreted as this. It is mandatory that the left operand of the assignment operator be an L value. L value and R value are technical terms used by the C plus plus compiler standard definer thing. In fact, it is a standard thing in programming language methodology. What does L value mean and what does R value mean? You are all familiar with value. So, you have A, B, C, D, X, Y, Z, whatever variables. Each one of them has a value. You write 35. 35 is a value. Right? Amongst all these values, all these values are they can be examined. That means a program can look at a value. They can be used. For example, in any expression I can use 35. I can use C. I can use D. However, only some values can be altered. For example, if I have value 37, can I alter it in my program? No, 37 is a constant. However, if I have a variable Z, can I alter it in my program? Of course I can because I can say Z equal to something. So, certain objects which are alterable objects, they are called L value. And by definition, it is mandatory that what appears on the left-hand side of the assignment operator anywhere must be an L value. Consequently, it must be an object which is either a variable name, single variable name or as we shall see later, it could be an element of an array or as we can see still later, it could be an object which is part of some class. But the point is on the left-hand side of an assignment operation, exactly one thing can happen, one name can happen and that name must be alterable. This is, there are, I found in the labs, the people still confusing on this issue so let me just give you an example and tell you why otherwise it's not possible. Let's say I write X is equal to Y plus Z. X is a variable, assume Y and Z has some values. Assume in fact that originally X is equal to 5, Y is equal to 10 and Z is equal to 15, assume that. Then what will happen when you execute this? Y and Z will be added, Y is 10, Z is 15, so you will get 25 and X value will now change, where the value of X can be changed. Suppose I wrote this as Y plus Z equal to X. This is what I found some people doing because they think it is equation any which way, it is not an equation and why is this wrong? So let us look at if at all compiler was to execute this, what will it do? This has a higher priority so it will have to evaluate this, it will actually substitute a value for Y which is 10, it will actually substitute a value for Z which is 15, it will add these two, it will come up with a constant value 25. Unfortunately the value 25 is an object which can be examined and used but cannot be altered. It is a fixed value, I cannot change it and since I cannot change it, it cannot occur on the left of an assignment. Now this the compiler figures out while you write this itself, so while translating itself it will say there is some problem here this is not correct and the priority describes that when I write X is equal to Y plus Z, it is never interpreted as X is equal to Y in a bracket. Why could that happen? Because I am perfectly permitted to write an expression of the kind A is equal to X equal to Y plus Z. If I wrote that then somebody could say X is equal to Y is first part, value of Y will be assigned to X, that value will become value of this expression and then that is added to Z to give a value of it. The reason that does not happen is between equal to N plus this has a higher priority. An equal to symbol has the lowest priority so that assignment operation will be done last as a part of any complicated chain of expression that you have. I hope this is amply clear, there should be no confusion on this count. Yeah. Sir a priority of the post increment operation is more than a multiplication and division operator. Yes. So why does it work that M is equal to J plus plus star N is equal to M, J star N, then J plus plus. So J plus plus star N plus plus has a higher priority than star. So it should work before. Plus plus will be executed first. Yeah. Similarly N star plus plus J plus plus has a higher priority than star. So plus plus J will be evaluated first and then the result will be multiplied by N. But in the case of M is equal to J plus plus star N. In that case the plus plus has a higher priority however the post increment implementation of the C compiler is such that if J plus plus appears in any expression the original value of J is used in that expression. So J plus plus will be evaluated but its effect will be made on J after the complete expression is evaluated. The evaluation may internally happen before the multiplication happens but that evaluation is sort of kept in some temporary location. The original value of J is used for multiplication because that is the definition of post increment operation. At the net results at the end J will have an incremented value neither. Good question. And this is the reason precisely why I said why should any sane person use such a complicated expression which confuses oneself forget others. So it should not be done. So is there any use of post increment being the highest priority? No its just a rule. I mean there are so many grammar rules for languages. Great Panini has defined so many rules. I don't even know 80 percent of them but they exist and if I have to write something properly then I must make sure that I follow those rules. So in a programming language which is completely rule bow there is no choice but to follow rules it is better to know the rules. So these rules exist. Let us continue our discussion with the conditional expression. The conditional expressions let us look at this example. It is reading two values of A and B it says enter the value for A and reads the value of A. It says enter the value for B says read the value of B. Instantly please note that in the C out statement the strings that you write enclosed in double quote marks are produced verbatim by the compiler or by the computer when you execute the instruction. If you want to print the value of a variable A or B you have to write it just without the double quote as a simple single entity. So you can either output strings or you can output variable values or in fact you can write expressions and the expressions will be calculated and those values will be put out. However within one less less after one less less you will write exactly one value it should be either one string or one variable or one expression if you want to put something else you have to write a less less symbol. So n l for example which represents end of the line symbol has to be output separate. Fine. Anyway we are looking at the conditional execution. So look at this now if A less than equal to B C out A less than equal to B please note that this is not an expression because the entire thing is enclosed in double quotes this is just a dummy string it is like abarakadabarakad it does not matter compiler does not look at this at all it will just print this string as it is similarly but this however A less than equal to B is an expression this will be evaluated there will be instruction generated so now the meaning of this is that if A is less than or equal to B you print A less than equal to B otherwise you print A greater than B so all that this program is doing is it is reading two values and printing a message you will print either this message or this message this is clear very simple here is an execution plan for that program so what I have done is I asked my associate to execute this program and capture the screen so notice that I will compile my program by saying C++ say prog1.cpp execute it by dot slash A dot out now whenever it says enter a value for A I have type 4 enter a value for B type 5 notice what do I get A less than equal to B this is the message I get which is the correct message if I give 4 and 3 it says A greater than B so far so good but suppose I give 3 and 3 what is the message I get A less than equal to B actually 3 is equal to B so why should I get a message A less than equal to B because that is what I am printing suppose I want to modify my program to say that if these two happen to be equal the message should say A equal equal B that will make more sense please note that we are only talking about printing messages no values are being printed let's go back and examine the program again here we are saying A less than equal to B we are printing this message this is why when A was 3 and B was 3 this condition was true this condition will be true when A is less than B this condition will also be true when A is equal to B and therefore when we are giving values 3 and 3 it was printing this message what we wish to do now if A is less than equal to B instead of printing a message directly I would like to again check if A is equal to B and if A is equal to B I would like to print a different message otherwise different message so here is a modification in the program made again I am reading the values of A and B if A is less than equal to B I am checking if A is less than B if so I am printing this message else I am printing A equal equal B and else I am printing this this is the case of nested if observe that this if statement if A less than B output this else output this is a complete single statement so this entire thing is a single statement you start with if A less than equal to B you have to write something inside you can write any one statement or you can write multiple statements by enclosing them in curly brushes if you write any number of statements in curly brushes they all become one statement equivalent here I have only one if statement so this entire thing is a single if statement if something statement else statement I don't need curly brushes anywhere this is perfectly correct statement so the nested if says if A is less than equal to B do all of this if A is not less than equal to B come to the corresponding else and output this statement so you agree that now the correct statement will be printed the correct conditional will be printed here is another execution of program 2 for example if I give 2 and 3 the value the the string printed is A less than B if I give 3 and 3 it now says correctly A equal equal B please note that this is not an expression or something it is just a string that I have decided to print in response to such value similarly if I give 4 3 it will print A greater now when we write our programs particularly if the programs have nested ifs it is important that we write the programs such that not only compiler understands our programs correctly but when we read them we also understand correctly and that is the reason why you will see that when the program starts here for example immediately all other statements are written 2 spaces removed if I start an if block that is an if statement then what comes inside the if is all removed further by a few spaces if there is a nested if I further remove these this is called indentation just like when you begin a new paragraph it is customary to leave some spaces at the beginning of the first line of that paragraph that is called indentation C programming writing requires certain professional disciplines to follow and an unintended program particularly which has nested structures is completely unreadable by human beings please note that for the C compiler it does not make a difference actually the fact that after this you have pushed an enter button so a new line character has been entered or you could even give blank blank blank blank blank as many and then push an enter button all that is okay for C compiler because you know what C compiler does when it reads all these characters in your program there is just a sequence of bytes each byte representing a character the first thing compiler does is it removes all white spaces all blanks are removed all new line characters are removed all tab characters are removed and then it starts applying the grammar rules of C programming language to whatever program it sees it is able to translate that into machine language appropriate so obviously all the blank spaces what we call white space blank tab and new line character are only meant for human readers imagine what would happen if I wrote a program like include IO stream using name space STD int main int a b et cetera et cetera et cetera nobody will be able to read that so this indentation is important this is indicated in another example here which I call the dangling else example why the name we shall see it in a moment here is a program instead of reading the values for a and b I am writing these values arbitrarily as 25 and 75 because what is being illustrated through this artificial example is what chaos could happen if I make mistakes in writing program the program apparently is correct what it is doing it is checking if a is greater than 100 if a is greater than 100 then it is checking if b is greater than 100 and if b is also greater than 100 then it is printing a exceeds 100 and b exceeds 50 on the other hand if a is not greater than 100 I want to say a is less than or equal to 100 so this else statement is written here when the compiler looks at this program and executes it however what will it produce it will produce second when I said that it examines if a is greater than 100 if so then it examines this if it is so it executes this however if a is not greater than 100 I said you will come here why because I have written else here in line with this if so you all imagine that this else is paired with this if that is incorrect every else is paired with the immediately preceding if if that if block has not ended logically and the only way you end a block logically is you put the entire block in an opening blast and closing blast in this particular case the compiler will while translating will say a this is the condition if this condition is true I have to evaluate this if this condition is true I have to evaluate this if this condition is not true be greater than 50 then I have to evaluate this because this else appears immediately after this if doesn't matter where you write it in your program this if statement has not ended because the else and if are paired if I meant this else to be paired to this if then I will have to write something like this if a greater than 100 then if be greater than 50 see out less less this and now look at this brass this brass is matched with this brass that means this entire thing is a single block of this if condition if this condition is true do all of this if this condition is not true come out of this brass and do this else now this else is paired with this if and when this if statement ends here the compiler concludes that this particular if has no associated as clause because there is an end of block consequently it is advantages if you recall I have commented that whenever you write an if statement this is useful to start a brass write whatever you want to write even if there is a single statement at the end put a closing brass so that there is no confusion in either your mind or the compiler here is an example of the execution of these so dangling else when I compile as some people correctly pointed out nothing will come out nothing will come out because let's go back to that example it's a very artificially constructed example but look at this if B is greater than 50 then you do this if B is not greater than 50 you do this now A equal to A greater than 100 itself is not satisfied so you will never enter this and consequently the else part will also never be entered you simply come out to return 0 in this case you will correctly execute this else so that is why you show that A is less than or equal to 100 comes out yeah in the first case if is written without else right so while compiling the program compiler should give some error no the correct answer is that else statement is not mandatory for every if this is a good point so let me just explain suppose I want to implement this logic if A is less than 5 I want to assign A to B but if A is not less than 5 I don't want to do anything we might have some earlier value then this I will implement by simply saying if A is less than 5 B equal to A there is no else here required if there is no else statement then the interpretation of this if is exactly like this if there is an else statement if some condition do this semicolon else do this semicolon then the interpretation of this is that there is a decision making and there is an action here and there is an action here and either you do this and then come out or you do this and come out so both logics are possible to be implement if I write only this if and then there is some other statement let's say C out whatever whatever z whatever then this statement will be executed only if A less than 5 otherwise this statement will not be executed you will continue with the next statement so therefore there is no question of giving an error say important point that he makes that you must remember that an if statement does not have to mandatoryly possess an else it is not required wherever I have such logic to be implemented in fact I will not have any else otherwise I will have to write a dummy statement else opening brasses closing brasses I don't want to do anything real so that is why it is not made compulsory fine the second pair of brasses wouldn't the program end after the the first pair ends the program does not end till the last brass after return 0 comes the brasses must be matched one on one the brasses don't end program the brasses so where exactly you are you are saying the use of brasses in the program you are talking about this program you look at this brass this brass is coupled with which one this but in that case when we the brasses like the brasses like brackets are always paired with the immediately preceding opening brass just like brackets are paired with the immediately preceding opening bracket exactly the same rule applies here so you will notice that this brass compiler will couple it with this brass and it will therefore take this entire thing to be a block which is part of the if statement if the condition is right if the condition is false it will assume this block is not to be executed it will come out let us now very quickly discuss the need for iterative solutions iteration is one of the most powerful mechanism of making the computer carry out hundreds of thousands of operations without our actually writing hundreds of thousands of instructions in fact it is the iterative capabilities of specification in programming languages which and the capabilities of the computer to iterate around repetitively executable instructions which actually permit programming to become such a powerful tool what is an iteration iteration means repetition so I have executed some instructions I want to repeat the execution of same instructions why would I like to execute the same instruction again and again suppose I have an instruction a is equal to b star c would I ever say a is equal to b star c semicolon a is equal to b star c semicolon even if I write it 5000 times a will only have the same value b star c neither of them is changing so by saying that I would like to repeatedly execute a is equal to b star c does not make sense unless every execution deals with some new value so iterative execution would like to repeat instructions but the instruction must do something new every time if such is the requirement and we shall see in a moment what is the requirement then we would like a short form to specify such repetitive action we continue with our example for finding out the maximum set of a set of positive integers you all remember this example finding out maximum between a and b we wrote an if statement then we said let's find out maximum between a, b and c and we tried to write nested if if a greater than b then if a greater than c then max is a greater than c etc etc and then we looked at an alternative method where we artificially assume the first number to be maximum and we say max is equal to a and simply examine if b is greater than max we reset max to b then we examine if c is greater than max we set max to c and we found that method to be simpler because I could extend it to find the maximum of larger number of variables such as this so this was the example you will recall if I have to find maximum of 5 numbers then I will read all those 5 numbers I will set max to a and then keep examining whether b is greater than max I reset max to b if c is greater than max I reset max to c please note at any time if c is not greater than max then max will continue to remain the previous value whatever it is so what I am doing I will go through these instructions at any stage I am remembering the maximum value amongst all the numbers that I have examined so far if I have examined 3 numbers here the max will replace maximum of the current 3 whenever I come here the maximum will be maximum of all numbers now if I want to find out maximum of 200 numbers or maximum of 100 numbers you remember I commented that I will have to think variable names b c d e f g h etc a 1 a 2 whatever whatever and I will have to write 100 instructions 200 instructions which appears stupid can I now create an instruction which can be repeatedly executed assuming that I have some mechanism of telling c++ to repeatedly execute statements I examine what is different in each of these statements only the variable name is different b c d e and that is because I have originally decided to read 5 different values in 5 different variables now I think again is it necessary to call these variables by different names suppose I said I did not read all these values I read only one value a assign max to a then I read another value a is the second number I am giving series of numbers but I am not reading and storing them in different variable locations I am reading them in one variable location only why because after the value in that location was examined with respect to max and max was determined there is no point in retaining that value after all at this point am I doing anything with a am I doing anything with b am I doing anything with c no I just read those values use them to compare with max if they were larger than max I reset the max consequently what it means is it is not necessary for me to retain all these values if my intention is just to find the maximum ok I could therefore write something like this c in greater greater a I read a value a if a is greater than max max is equal to a and output the value of max so I have given one number 5 5 is the maximum I will print 5 suppose I give second number 7 is 7 greater than 5 yes so a will be said to max max will become 7 it will print 7 suppose next number I give 3 and suppose I repeat these instructions now a will become 3 is 3 greater than 7 no nothing will happen max will remain 7 next I give 4 4 is still less than max max will remain 7 next I give 10 10 is greater than 7 max will be said to 10 I will output 10 I now seem to have created a set of instructions if I could somehow treat them as a block a single block and if I could somehow tell the machine that please execute this block repeatedly again and again and again do you agree that if I give as many as 100 values after every value it will print the current maximum and that will be the correct maximum do you agree I therefore look for a mechanism to instruct my computer in C++ that look I have written a block please execute this again and again and again this mechanism in C is implemented by a variety of instructions one of which we are going to very briefly look at that instruction is called the wild statement just as we have seen if statement we see the wild statement here the wild statement essentially permits me to define what is known as an iterative loop or a repetitive loop the way it is written is there will be some earlier statements after that I write the statement while so just like if after if I write a condition after while also I write a condition just like in a if I write a block of statements after if here also I write a block of statement unlike if there is no else and unlike if which says execute this and get out if the condition is true while says execute this if the condition is true but don't get out come back again come back again come back again so let us go back to the previous slide this is what while implement while checks a condition which is stated outside this block if the condition is true it will execute this block and come back again again it will examine that condition if the condition is true it will execute the block again as long as that condition remains true it will keep on executing this block again and again and again however the moment in any one iteration at the beginning if the condition becomes false then I will come out and go to the next step and that is the meaning of this next step so read this carefully the condition is a relational expression which is evaluated at the beginning of each iteration the block is executed if the value is true else the program exits the loop and goes to next step however if the condition is true the block is executed again and again and again so there is no exit after this brass if after this brass you go to the next statement in while after this brass you always go back here and go back to check this now this is a powerful mechanism and I could possibly use it to find maximum of a large number of numbers so here is a while statement to do this notice what I have written inside while one is one a conditional expression but what is the value it is a numerical expression which has a constant value which is one you remember when we discussed true and false one is interpreted as what true so I am saying why true do this that means still eternity keep doing this every time the iteration is executed I will come back this condition will be evaluated again what is this condition one true so it is like a green light we are not doing this endlessly it is of course this stupidest way of specifying an iteration because you are asking the computer till the end of earth and universe keep running this program that is not how we will prescribe the iteration inside this while bracket we will write a condition some element of which will change during this execution so that at some point in time whenever we want to end the iteration we shall actually have a situation where the condition will turn out to be false but to illustrate the work of why and also to illustrate that this is a perfectly valid statement in c++ you can always write a condition which is perpetually true and you can force the computer to do a perpetually iteration for the purpose of this example it is good enough what this example does this example sets a perpetually iteration let's go back to slides this is what I wanted to do right anytime I read a number if that number is larger than the present maximum I want to set that maximum to new number and print it out if that number is smaller than the present maximum I want to go and read the next number that is what I wanted to do and that is what this while statement will do there will be other statements here there will be other statements here notice the blocking while one c in greater greater a if a greater than max brass max is equal to a c out less less max brass close the if statement ends here there is no else and the if statement and the c in followed by the if statement is the only thing in the while block the while block ends here after this ordinarily I would have executed a return zero but because of the word while after this I will go back after this I will go back and as long as this condition remains true which sadly in this case remains true perpetually I will keep executing this iteration this is the logic this is the flow chart observe what I am doing input a is a greater than max true set max to a output max and go back if a is not greater than max then what nothing leave max aside as it is represents the correct maximum so far go back here and continue iteration observe that this flow chart does not end this flow chart indicates perpetual iteration we will never ever write programs like this but when this program is written what happens when the program starts execution what is the first statement it will encounter I am not talking about a dynamically stable situation where you have executed many iterations first time when you come in you will read input a now you have to evaluate this condition is a greater than max how will computer evaluate this condition a has a value because I have just read a value does max have a value first time it does not have a value subsequently it might have a value consequently any iteration requires initial setting of value you have to kick start an iteration and in this particular case what will be required you have to initialize max to some value now what value you will initialize max to suppose you initialize max to 99000 arbitrary and suppose all the 100 values that you give are 1, 2, 5, 7, etc this program will keep stupidly printing maximum 99000, maximum 99000 is not since we have defined the problem to be finding out maximum amongst positive integers then a value such as 0 is not likely to be given and therefore I can safely set max equal to 0 at the beginning before my wide statement if I set max equal to 0 whatever is the first number that I read is bound to be greater than 0 and in the first time itself this will be true max will be set correctly and max will be printed as the first number itself then iteration automatically when I read subsequent numbers it will work appropriate this then is my C program which will actually implement this logic observe that I am defining in A and max I am setting max equal to 0 this is an artificial setting it is initialization for my loop now I say while 1 this is my artificial condition which will always remain true so I am setting up an infinite iteration in that iteration what is the block to be repeated first catch a number enter the number read A which is the same thing as max less than A then I set max is equal to A so anytime a number is given if it is more than max I reset max please note that I also say c out max and this c out instruction is executed irrespective of whether max is changed or not that means every time I give a number the algorithm will print the current maximum value that way I will work out I will be able to know whether the algorithm is working correctly or not please note that when I come to this brass here I will always go back and this return 0 will never be executed in its lifetime by the program these are the results of execution I compile this program called iterative dot cpp after compiling it I execute it by saying dot slash A dot out what is happening I enter the number 3 it produces max is 3 enter the number 2 sorry 9 it produces max is 9 now see I am giving a series of numbers 2 4 5 6 8 it continues to print 9 that is the largest value but the moment I give value 10 it says max is there because internally A was compared A is 10 A was compared with max which was 9 A was found to be larger so max was set equal to A which is and now 10 is printed so you agree that this program will execute and actually calculate the maximum of 100 200 or even 2000 numbers and the total program size that I have written is just these statements now that is the power of iterative specification of course this is a extremely simplified solution of a problem you will agree that we do not want to put infinite iterations ever after all the numbers that I have in mind are finite I can either say I have 25 numbers please find out a maximum in which case I should be able to casual up a condition which will remain true for 25 times of iteration and then it will become false so that I will come out or I could put some other condition such that if the number is negative get out I can look at variety of ways how to terminate my iteration we will stop here with noting two things A iteration is an extremely powerful mechanism to write few instructions but to force the machine to repeat things multiple times B in every iterative specification I will require some initializations which may be peculiar to that iteration specification and C I must ensure that every iteration ends or terminates sometime because of my design so I must write conditions which will terminate iteration there are multiple ways of specifying iterations in C in the next lecture we will look at those