 So, good morning. We will start. We will continue our discussion. There are a few points about the assignment statement that I would like to cover today. So, today we are going to look at a new programming concept called conditional execution of instructions. But before that, I wanted to continue discussion on the assignment operator because there are a few interesting variations that the C C plus plus family of languages provides. After we discuss that, we will take a computational problem which cannot be solved using just sequential execution of our instructions as we have been seeing so far. So, we will look at what does conditional evaluation mean and then we will look at the basic control structures available in C plus plus which will permit us to execute either one group of statements or another group of statements. More specifically, we will look at the if-else and if-else if ladder. I will then make some general announcements including announcements for the next week labs. First, the notion of reassignment. So, this should be very obvious to you. So, if m is equal to 7, then 3 star m plus 1 will be 3 into 7 plus 1 which is 21 plus 1 which is 22. It so incidentally happens that this value of the expression so computed is to be assigned to the memory location reprinted by m. So, please note that m appearing on the right hand side is incidental. The interpretation is first evaluate the expression on the right whatever is the value assign this value to m. What it means is store 22 in the location that was assigned to m and what it also means is that the previous value will be wiped out. So, from this point onwards in our program m will continue to have value 22 unless reassigned again. So, this reassignment is very central to many of the computations that we will do using C or any other programming language. So, in a nutshell such things are not equations just because there is an equality symbol here. We must remember that the equality symbol in C plus plus represents an assignment operation and you evaluate right hand side calculate the value put it on the location. There are special increment and decrement operators which are provided by C C plus plus these are shortcut kind of statements for assignments or reassignments. For example, if I want to increment the value of some variable m by 1 I would ordinarily write a statement like this m is equal to m plus 1. This means that whatever is the current value of m add 1 to it get the resultant value and put it back in m effectively changing the value of original value of m by 1. Now, such increments or decrements by 1 can be written in short hand in C C plus plus using operators plus plus and minus minus. So, for example, if I just say plus plus m just plus plus m this is a complete statement by you and it is an assignment statement. It means the same thing as m is equal to m plus 1. Similarly, minus minus n is an assignment operator and what it means is n is equal to n minus 1. Many people prefer this short hand just because it saves you some typing strokes also because these things then can be used inside expressions. But I might end up writing expressions which may not make immediate sense to an ordinary reader. Consider this example int i comma j, j is assigned a value of 5 and then I have an assignment operation which says i is equal to 7 plus plus plus j. Now, ordinarily we would not write such a thing, but this is a legally correct statement in C plus plus and what it means actually is that plus plus j is considered to be an operation. So, the entire assignment operation becomes i is equal to 7 plus in brackets plus plus j and plus plus j stands for j is equal to j plus 1. So, the normal rules prevail. This assignment operation is first carried out which means j plus 1 is 6 because j was 5 here. So, j plus 1 is 6, j equal to 6 is the assignment operation, j is assigned a value 6 which also becomes a value of this entire expression inside parenthesis and then that 6 is added to 7. So, it becomes starting. So, effectively please note that you have achieved two things by writing this statement i equal to 7 plus plus plus j. You have achieved first an increment to j and then you have achieved an assignment to i which is the modified value of j plus 7. However, if you ask me I would prefer to write it like this. One short end is good enough you will agree that the last statement set is more meaningful than i equal to 7 plus plus plus j although they mean exactly the same thing semantically in C plus plus. There is an additional variation in the increment and decrement operator. Let us go to the previous slide. Here we saw that the increment and decrement operators are written before the name of a variable. This is called pre-increment. They could also be written after the name of a variable. For example, i plus plus. This is also same as i is equal to i plus 1, j minus minus. It is also same as j equal to j minus 1. So, what is the difference? Are there two different notations for exactly identical meaning? Well, as far as the increment and decrement operators are concerned the meaning is same. But when these are used in a larger expression the meaning changes substantially. So, for example, effect on i or j will occur after the expression is evaluated. Consider this. If I write i is equal to 7 star j plus plus. Now, this is same as i is equal to 7 star j semicolon j plus plus. And it is not same as j plus plus followed by i equal to 7 star j. Can you see the difference? So, suppose the value of j was say 5, then i is equal to 7 star j plus plus will mean j will be added to 7 plus. So, 5 will be multiplied by 7. The value of i will be 35. After completing evaluation of this expression the effect of increment of j will take place. So, this is called post-increment. The earlier one is called pre-increment. However, as I said a good programmer would probably always write these as either i equal to 7 star j j plus plus or as j plus plus followed by i equal to 7 star j depending upon what meaning you want to give. Because this is more readily understandable. However, this is a perfectly valid state pregnancy. An additional variation in the assignment operation. Consider a situation where I want to increment the variable not by 1, but I want to increment it by some other value. Or I want to increment it by a value which itself is computed by evaluating an expression. Consider this for example, m is equal to m plus 2 star m. Now, this is clearly a reassignment operation because m is being reassigned a value based on its own previous value plus something else. This can simply be written as m plus equal to 2 star m. So, m plus equal to 2 star n is nothing but m is equal to m plus 2 star m. That is this could be any expression on the right hand side. Its meaning of writing plus equal to is simply m is equal to m plus something. In exactly the same way c plus plus permits use of compound assignment operators in the form of plus equal to minus equal to star equal to slash equal to modulo equal to etcetera. Of course, if you use modulo equal to then it would mean m is equal to m modulo some expression. That expression would be converted to integer and m ought to be an integer in order to get meaningful results. Generally, you will not see such expressions except for plus equal to and minus equal. Again, this is a shortcut provided. I do not see any reason why I cannot write this. This is something I would like to warn you by the way. Most programming languages not just c c plus plus most programming languages actually provide for such artifacts. For some people who designed these features these things appear to be very interesting. These things also appeared must have appeared to be shortcuts so that you have to type less. Then you are writing very large programs and when you have too many increment operations for example, you might resort to this. Somehow these became more or less a standard practice amongst programmers. But as a consequence of that practice, c c plus plus programs are far less readable by other programmers unless they also are expert users of such various. So keep that in mind because you are writing programs not only to solve some problem numerically on the computer but be sure that your programs will be read, understood and rerun by many others. This is a completely different programming paradigm than what existed 40 years ago where or 30 years ago or even 25 years ago where most people wrote programs to solve their own problems. Today programming is a generic exercise where somebody writes a program, somebody else uses it, some third person modifies it, a fourth group takes it and based on the idea develop something else in which case the readability and understandability of your program is of vital importance. It is at least as important as writing correct programs. So keep that in mind. However, for the sake of completeness I have covered these. In general in most c programs you will find the use of operations like i plus plus j plus plus very common and that is okay because that is well understood. It is an increment by one or a decrement by one kind of thing and which will generally happen in a standalone fashion. Very rarely such usage will be made within any larger expression. Next we consider a problem requiring conditional execution. So far what we have seen? We have seen that if we write instructions one after another they will all be executed by c plus plus one after another in exactly the given sequence. However that may not work out. So let's look at this problem. Let's say bus tickets are being sold for some travel from point A to point B and the bus ticket is different depending upon the age of the traveler. So let's say the ticket for an adult passenger is 25 rupees 50 paise and rupees 1275 for a child. Just an example. Now the child has to have an age of less than 12 years. More or less standard practice at many places. But how do you do these computations? For example, if the problem is that given the age of the passenger find the cost of the ticket. Now I might try writing a program like this. Age is integer because fractional ages don't count for this decision making. So I define age as an integer value. I define ticket underscore cost as float. Please note underscore is a valid symbol in names and indeed it is recommended that you use it in order to distinguish different components of a larger name. Next give age of passenger see in greater greater age. I collect the age. Now there are two possible ticket values 25.5 and 12.75. So let's say I write the assignment statements. Ticket cost is 12.75. I immediately write ticket cost is equal to 25.50 and I output the ticket cost. So what will be output? The last assignment. And it does not matter what the age is because in fact in my entire program I am not referring to the value of age anywhere. So we will always get the cost as 25.50 with respect to the value of the age. Consider rewriting it differently. So I write the other statement first. I switch the assignment state. I do the same thing. Then I say ticket cost is 25.50. Ticket cost is 12.75. Very obviously it is wrong because whatever be the age again it is the last assigned value to ticket cost which will prevail and that is what will be printed out. So now this is happening because both the assignment statements are being executed one after another and the last assignment will take hold. Very clearly this is not what we wish to do. So what do we wish to do? We want to instruct the machine in something like this world. If the age is greater than 12 years then calculate the cost of ticket as rupees 25.50 else calculate the cost of the ticket as 12.7. Very clearly the instruction mechanism in CC++ that we have understood or in programming in general we have understood where we write statements one after another and they are executed that process is not anymore adequate for me to solve such problem and you will all agree that in most computational and real life problems you will have to make decisions of this kind based on some value and take either one path or another path. So consequently we wish to rewrite our program such that either the first or the second assignment instruction based on the value of age is executed but never both. And we know that in the program that we have written so far we are reading the value of age but we are not using it anyway. So this is the bottom line we do not want to blindly execute our instructions in the given sequence. We first want the machine to examine the value of age. Then depending upon whether the age is greater than 12 or not we want the computer to execute either the first or the second instruction but never both. And this conditional execution is now possible. There is a mechanism of representing such conditional execution using what is known as a flow chart. So let me just draw a flow chart for you to show how we can depict this situation picture you. When we write instructions like this one after another in a flow chart they are represented by rectangular boxes. So this is one instruction this is called a flow chart and whatever instructions you would write in your program they are represented by rectangular boxes inside which you write those instructions. For example input m okay n is equal to m plus 7. Notice that what I have written inside these boxes is not correct CC plus plus syntax. It is not even a correct syntax in any programming language. The flow chart is meant to be a preliminary step to represent my own thinking about the algorithm or the order or the sequence of execution of the intended instructions. So this could be called as a pseudo code it is not an actual code in a programming language but it is good enough for that. Now you will notice that every program that we have written so far or seen so far can actually be represented in this fashion. When we do not want to execute instructions in a sequence but we want to execute instructions in some kind of a conditional fashion we introduce an additional symbol in the flow chart a diamond. So whenever a flow of control in my program execution comes to a diamond it means that some condition is being evaluated. What condition I want to evaluate in the given particular program is that correct this is what I want to check. Now depending upon whether this condition evaluates to true I want to take certain action if it evaluates to false I want to take some other action what action do I wish to take if age is greater than 12 I will just write Tc as equal to how much was the value and if age is not greater than 12 I forgot the value. Both these paths are supposed to come back and meet each other and then I want to execute the next instruction which will say output Tc all of you understand this the only thing is in my computer program I can write sentences only one after another I can't write something on right hand side something on left hand side and say if this is the if either execute this or execute that whatever I have to say I have to say in a linear fashion because the computer program is a text consequently this particular diagram itself can be solved of linearized what I have done here I have written the condition below that I have written one of the statements below that I have written another statement but the understanding is that even if the statements in my program appear one after another as they must physically the meaning is that this statement is to be executed only if this condition is true and then I have to come out and carry on with the next I have to skip the next one similarly if the condition is false I have to skip the next instruction and execute a subsequent instruction and follow up so this is the way you define conditional execution in any programming language all programming languages languages permit such conditional execution statement or a group of statements by group of statements in this particular numerical example I need to assign I need to execute only one assignment operation ticket cost is this or ticket cost is that but it could be so that in a computational problem if a certain condition is true you want to execute some 20 statements and then go out somewhere on the other hand the condition is false you might want to execute another body of 15 statements and then go out so effectively this box then would represent not just a single instruction it would represent any number of instructions as long as they form a single block consequently in every programming language we need to have a mechanism to represent a block of actions following the two path and another block of actions following the false path you get this so now let's go back to the c programming instructions this is the c++ program to calculate cost of the bus ticket so look at the way the statements are written you have the usual include iostream and using namespace std this is following int main I am declaring h to be an integer and ticket cost to be a float I ask the user to give me the age of the traveler so I read in the value of age now this is the this is the important part the conditional execution is implemented by if statement which is the new statement that we have learned after if you always write a pair of parenthesis and inside this pair of parenthesis you write the condition which needs to be evaluated obviously this condition must evaluate to either true or false right you can understand age greater than 12 this is not a statement but this is a question being posed to c++ what it is saying is that please examine the current value of age at this juncture and find out whether it is greater than 12 or not if it is then execute the statements immediately following this if it is not skip this and go to the else part so if and else form a pair after the word if I write the condition and below it I write the block of statements that I want to execute if the condition is true it so happens that in this example the block consists of a single statement notice the opening brass and closing brass here then I have a single statement these brasses are actually not required if I just write this statement it will still be correctly understood to mean that this is to be executed only if the condition is true however it is a good practice to press the brasses here well because later on if you go figure out that you have to insert some additional statement in the true side of the execution path then you will be able to insert those statements here if the condition does not happen to be true at the time of execution of the program it is not predicted because while I write the program I don't know the age so the company will evaluate this condition and if it is found to be false then it will skip this entire block it does not matter whether this block has ten instructions or ten thousand instructions one opening brass followed by any number of instructions followed by a closing brass is the block for the true side else ticket cost is equal to 12.75 this statement is executed if the condition is false and when I complete this brass or closing brasses that means whenever executed either this block or this block where will I come out and come to the next state please note that the statement after the if else block is not physically marked because there could be many statements here and many statements here and that is the reason why we use while writing our program a notion called indentation that means inside the true path any number of instructions that we have to write we will never write them starting from here we will leave a few spaces typically you use a tab character that is the standard practice in cc++ programs I will not use tab character because if I did and if I have a large number of nested ifs as we shall soon see my program will overflow the end of the slide so I use two or three or four spaces but this indentation any number of statements which are written here will be written like this what's the advantage it is visibly clear to me that this statement forms the if true path and this statement forms the not true path or false path so this is a style although it is not required you could always start the statement anywhere in the line but that is a good practice so is this understood the if statement and the use of if statement what could be the variations possible in this if statement suppose I want to find out let me solve another problem here two numbers a and b find out the maximum so let's say I have defined them as int a b max I am just using a shortcut not giving messages now maximum means the larger of the two so I could write any statement of the type if a greater than b max is equal to a else and then when I output max I will get the maximum of these two numbers however I could also write this part of the code slightly differently suppose I said max is equal to a please note that I am forcing max to have the value a I don't know actually at this state I have just said the value of a and b I am just arbitrarily saying let me assume first value is maximum if it is not then I will change it so what will be my if statement if b is greater than a notice that this variation of if does not have an else statement or does not have a false past at all it is not required why because I am originally assuming that this condition is false and I am writing this assignment statement direct so max is equal to a if however b is greater than a then I reset max to b if b is not greater than a then this statement will never be executed so when I come out I will print the correct value of max is this line better or is this line of writing better as far as finding out maximum is concerned why is a question there could be many reasons I like it the way this is written here or I like it the way this is written somebody may say this is better sorry if both the numbers are equal then second is better the actual correct interpretation when both the numbers are equal is it doesn't matter please note that when I print out the max it is a value that is being printed it does not care whether the value came from a or b it would matter if the values were different if the values are same a is same b is same so how does it matter whether you assign to max the value of a or b so in this particular case it does not matter however if I also wanted to know what was the value which was ultimately printed then that's a different story we shall discuss that when we discuss arrays and sets of numbers etc but there is another stronger reason for using this approach let me exemplify it by saying that I want to find out maximum of fine numbers if I follow the first part I will continue to think in these parts so if a is greater than b I cannot say max is greater is equal to a then I will have to check is a greater than c also if not c may become greater then I have to compare that with d in short my logic could become extremely complicated if I follow this path however if I use this approach I could very simply write an algorithm and a program to find out the maximum do you see the point I start arbitrarily as in the previous case with a being the maximum I just presuppose it of course I may be wrong the first statement will check is b greater than the current maximum if it is max will be reassigned to b if it is not max will continue its old value which was a I am again checking the third number is it greater than max now it does not matter at this stage whether max is set to a or b whatever it is it is the current maximum and if the new number is greater than the current maximum I will reset max to that value you will agree that this is a much cleaner algorithm if you try to implement this using some complex logic you would write a series of nested if statements let me just explain to you what is a nested if statement if some condition is true then the statements which I want to execute will also include another condition evaluation if that is true I want to do something otherwise I want to do something but this else matches with the same so if the original condition itself was false I want to skip all of this and come here and this kind of nesting can be written to any level but as a result the program will get complicated there are computational problems where we have no choice but to write a series of nested if however our attempt should always be to minimize such nested if because that makes for cleaner reading so let me consider the last problem for the day which is an extension of the bus ticket problem let me say that in the bus ticket problem we have said that if the traveler is a child then the charges are whatever 12.75 rupees otherwise an adult the charge is 25 rupees however the bus company decides to show some courtesy to very old people and they would suppose the bus company says however per age is greater than 60 years then let's say the ticket cost is only rupees 20 not as less as a child but not as much as a adult now that's the policy that the bus company has and you and me as programmers have to implement that policy while writing such program I could use what is known as the if else if ladder what is a ladder I have a series of if else if else if else statements but they are not nested ifs for example I want to make 3 different conditions if the age is greater than 60 the ticket cost should be so much else if the age is greater than 12 the ticket cost is so much else if the age is else meaning otherwise the ticket cost is so much I could also argue in reverse I would say if age is less than 12 the cost is so much else if the age is greater than 12 age is less than 60 the cost is so much else something else let me write this and ask you which one is correct and why first I will write this is one way of writing the program the other way is to say both these programs implement similar logic you agree but one of them is wrong it's not adequate to say which one is wrong when I already told you one of them is wrong you have to say why one of them is wrong so let's analyze what happens let's just execute this algorithm for some different values of age let us say I execute this program for somebody whose age is 22 years what will happen is age greater than 60 no so this will not be executed I come here is age greater than 12 yes so TC will be said to this else TC will not be executed so I will come out with the correct reason let's imagine that a child is travelling age is 8 years I come here is age greater than 60 no this will not be executed else is age greater than 12 no this will also not be executed so TC will be 12.75 looks like working correctly but to completely test my program I must execute it for all possible boundary values so let me now take a value which is say 65 an old man is travelling is age greater than 60 yes TC is equal to 20 else is age greater than 12 will I ever come to this else because once this condition is satisfied the else part is never executed please remember this once a condition is satisfied I will be executing only this statement and after executing this statement I will skip out of this F block so where does this else end this else end does not end here because inside this else there is if so this if starts here ends here and after this this else ends so I will simply come out here this is in all three cases I am correctly calculating let us look at this again take a similar example let us take somebody whose age is 25 25 years is another not old not young is age less than 12 no so I will come out here else if age is less than 60 yes TC is equal to this come out let us take an old man 65 years is age less than 12 no come here is age less than 60 no come here don't come here because it's not true so I will come to the else part correctly calculated take another case the third case so why do you say that one of them is right somebody said one of them is right and they were speculating right or left however in your lab you will find examples which will work one way but not work the other way in general when you have nested if conditions you have to be extremely careful in the order in which you are nesting those if statements you make a mistake in that order and you could get into a lot of problems so just remember that yeah if you want you can put an additional brass here it is not required that is why it is called an lcp ladder lcp ladder is when I say lcp this again there could be lcp this lcp this series of statements and you don't require to encompass all statements of the rest in other pair of process it is not necessary so this is the correct interpretation it will work correctly sir if it will end when we will have to know how it will end end is always here the ladder ends here and the ladder is written such that only one of those conditions will be true sir that end braces of last this end is of this else this end is of this if this end therefore is also end of this lcp ladder just count the total number of opening and closing process as long as they match you are safe this is a peculiar variation of if which is called if else if ladder ladder means there are steps however if I were to write if condition then something inside which I have if said so on I will have to write separate blocks starting brass and ending brass you can try writing some statements and you can figure it out the rules are very pretty straight forward yeah so what if in the first one yeah if somebody writes the second statement before the first statement like else if age greater than 12 and the age is given to be 65 then the ticket cost so he has found out a possible problem if I did not write the conditions in this fashion if I wrote if age greater than 12 then tc is equal to 12.75 and then checked other conditions please note that somebody who is 25 years old age will be still greater than 12 somebody 62 years age will still be greater so that will apply to all people and that will obviously be a wrong way of doing things that is exactly what I mentioned when I said that if you have a multiple set of conditions to be checked you better be extremely careful about the order in which you evaluate those conditions because you could get wrong results it is always a practice because later on you will be writing 200-300 line programs and you would have variety of if statements there but whenever you have a ladder like this it is useful to quickly hand execute the rhythm for 2 or 3 different values and just check whether you are getting the correct results or not the other way will be extremely difficult to find errors in a larger program of this kind in general a conditional expression can be written in C in this form a operation b this operator op is a relational operator so this could be either less than greater than equal to greater than equal to you are familiar with these symbols equal to however is not written by a equals b the reason it is not written a equals b should be very obvious to you a equal b is assignment so b will assign to a there is no comparison consequently cc plus plus uses equal equal the same symbol twice so when I say a equal equal b it means compare a with b and if they are equal decide the answer to be true if they are not equal decide the answer to be false people initially make these mistakes many times while writing the if statement because they will simply write if a equals b because in our minds that is how we write equality or equality check also greater than less than equal to etcetera but that is not the right way in program and it is very important to distinguish because a equal to b has a specific meaning which says assign value of b to a what happens in such cases we will discuss the small part in the end but these are the relational operator similarly if you want to check for two values not being equal to each other the negation of equal equal is exclamation mark equality so this is read as not equal consequently you have less than greater than less than equal to greater than equal to equal equal and exclamation mark equal this means is equal to and this means is not equal you are all familiar with boolean logic ok so the conditions can then be combined so I can have complex conditions where I can test is a greater than 0 and is a also less than equal to 9 because that this will be the condition I will impose if I want to check whether a is a positive digit so a is equal to 13 the value is positive but it is not a digit a is equal to minus 7 is not a digit it is not a positive digit so this compound condition otherwise what I will have to write if a greater than 0 in the then part say if a less than equal to 9 so I will have to put nested if it is not required I can write that as a compound condition so and followed by and stands for conjunction which represents logical and and vertical bar followed by vertical bar is disjunction or all the symbolism is slightly odd you will get used to it in no term no time so you can write any number of compound conditions now these compound conditions technically are true or false true and false are two specific values defined in C++ library standard libraries as being Boolean type value you remember I wrote that word bool in the types currently we are not going to bother with it we will take it to mean that a relational condition will evaluate to either true or false and the if statement will work correctly however since relational condition actually forms a relational expression or a conditional expression and every expression in C has a value it is true that a relational expression will have a value true or false but C permits you to use that expression in the part of arithmetic expression also when you do that the true value converts to 1 and false value converts to 0 so if you say a greater than b one way is to use it inside any statement if a greater than b then of course the branching of execution will happen depending upon whether the condition is true or false but you can also say m is equal to a greater than b now that is a very funny way of saying the truth value is being captured in a variable called m but that's a valid statement in C what C will do is it will evaluate a greater than b if it is true it will convert to a number 1 if it is false it will convert to number 0 and assign either 1 or 0 to m alternately you could also put inside an if statement if m then you make sense out of this this is a valid statement and what this means is a less than b is evaluated first it will be either true or false if it is true it is converted to value 1 if it is false it is converted to value 0 consequently m will be assigned a value 0 or 1 I was telling you about the other possibility if I say if p let's say now p is a variable and let's say I have just read in the value of p assume p is integer I could give any value minus 200 plus 27 0 whatever right how would C interpret it as far as interpreting arithmetic values as to or false is concerned the interpretation is if the value is 0 it is treated as false and any non-zero value is treated as true so this is an additional squiggle we will end our discussion here but just remember relational operators constitute an extremely powerful mechanism because they permit you to conditionally execute certain statements in your program either this or that kind of the if statement with an else clause or just if statement without an else clause or if statement with a series of else if else if clauses will permit you to implement any manner of your flowchart in terms of its logical execution sequence and composing the appropriate if statements will be in fact learning of more programming so in the next week's lab and next week's lectures we will consider some more aspects of this statement and we will see some more solutions thank you very much