 here is an alternate version of the program. Now, there are complicated three cases, but here also I am using the if else ladder, but in a different way. How is the ladder being used now? I am first starting with the lowest state. So, I first examine if age is less than graph and if it is yes, I do something. If it is no, this is where I will oh sorry, I will check another condition again yes. So, I am constructing a ladder, but with reversal of the you can verify by hand execution of these statements, whether things work correctly or not. What is the hand execution? I will remind you again given any program you can of course make the C plus plus number to the hodagiri and run the program, but to and to verify whether you have written the program correctly or not. You should give some sample input and behave as if you are C plus plus execute the program write down the values that different objects will have and confirm whether program works correctly and you have to do it with two or three alternate inputs to cross verify. You can indeed check that it works correctly. Here is a question. Could I have exchanged these two conditions in the first two parts of the ladder? Could I have said if age less than sixty ticket cost is 25.50, else if age less than sixty because why not? You see some people have figured it out mentally, but this is something I would suggest all of you should do. Try to exchange these conditions and try to execute the algorithm with two or three sample values. Typical sample values is age given is five years, age given is say 24 years, age given is 63 years. These are the three partitions in which the logic is divided and you can check whether in each case you get the correct value of ticket or not. If you do your logic is correct, as I said you can implement the logic in multiplicity of ways. To come back to our friend's question, these are the two versions I have just put them side by side to indicate just two different ways of writing the same algorithm. I will be putting up these slides in the moodle by tonight's we can look at them, but I think you get the point. Here is another question. Given two numbers, find out the maximum of two numbers. Let us say a and b are two numbers. First I read them to find out the maximum. Now, typically the maximum is found out by a comparison is a greater than b. The answer is yes or no. On the yes side I will say something called max equal to a. If the answer is no, I will say something called max equal to b. In either case, I will come out here. First of all, do you agree that this flow chart will work correctly and it will not? Why? Suppose I give two values as input. First value is 5, second value is also 5, which is the maximum of 3 is 2, 5. Is it the first 5 or the second 5? It is a meaningless question. If the two numbers are same, it does not matter which of them gets assigned to max. It will still be the correct mathematical maximum of the two. That is a different question. The problem is not that given two integer numbers a and b, find the maximum of these. No, then there is something else. Suppose I want to find out which one of them is maximum. This question is not trivial by the way. Sooner or later rather than sooner when we discuss arrays, we will come across this particular issue. To give you an example, suppose I have these 500 students and the mid-sem marks are recorded against the roll numbers. So roll numbers are stored, corresponding marks are stored. I want to know who scored the maximum, but I would also like to know who scored that maximum. I would like to know where exactly in my collection of values this maximum. So the point that he makes is correct. However, right now the problem is already just finding out the maximum value. That is all. You had a question somewhere here? No. Here is a program to solve that problem in a, b, max input a, b, sorry, input c in greater, greater a, greater, greater b. Now, my favorite if statement, if a greater than b, max is equal to a, else max is equal to a. Output max. Solve the problem? Alternate logic. You remember I did something fancy while illustrating the possible way of calculating the bus ticket when there were only two conditions. I can do something similar here. I can say input a and b, then I can say max is equal to a arbitrarily. I do not know who is the maximum. Now, I will check. Is a greater than b? On the s side, I have to do nothing. On the no side, I have to do what? But here is an additional speaker. Why should I say a greater than b? After all, currently I have assumed a is maximum. So the max contains the maximum value. Why not compare max with the next number? This logic will tell me that I will say max is equal to a. Then I will check is b greater than max? If yes, I will say max is equal to b. Otherwise, I will simplify. Both are correct. They will work correctly. However, this second alternative logic is better suited for extending this logic later as we shall see when we study iteration. I can use a very simple program like this to find out maximum of 200 numbers, 5000 numbers, 20 numbers. Here are the two versions. Just to illustrate, everybody got this? Version 1, version 2. Very straightforward. If I were to extend the logic of version 1, to find out maximum of say 3 numbers, and if I were to extend the logic of version 2 to find out the maximum of 3 numbers, I get completely different algorithms. In the first case, I will get a slightly complicated algorithm. I have now 3 numbers. I have input values of a, b and c. Now, I will check as usual is a greater than b. Yes? Am I clear whether max is a? Not yet. I have to now check the third fellow c. So, I will have to check is a greater than c? If yes, now I know a is the big box. So, I will say max is a. Otherwise, what I have to say? Max is c. On the other hand if a is not greater than b, can I simply say b is max? No. Now, I have to compare b and c. If b is greater than c, I now know max is equal to b. Otherwise, so many arrows. But I hope you appreciate that this logic will work correctly. Just imagine. Now, if I say given 4 numbers a, b, c and d, find out which is the max. If you have to implement this logic, what will you say? Input a, b, c, all right. If a greater than b, then if a greater than c, max equal to this, else max. Else, if b greater than c, etcetera, this is that logic. Notice that I have been putting the 3 numbers a, b and c. If a greater than b, the next statement is not a normal statement. It is another brass opening brass. Inside, if b greater than c, now max is equal to a. Else, if a greater than c, max is equal to a, else max is equal to c. Else, if b greater than c, max is equal to b, else max is equal to c. Is this correct? Not correct. This is not certainly the logic that we had shown in the previous graph. Here, I had, is a greater than c. Yes, this, else this. So, there is, this is not what it does. This program will be on the model. Just check, what does this logic do? Find out whether I am unnecessarily and excessively checking some condition. Find out whether in the other case, I am checking the condition correctly or not and check what the result is. The point to be made here is the following fact. If I have to implement this logic, I will have to write what is called nested ifs, if within if. If I have to find out the maximum of 4 values, the nested if will increase one more and we will all go mad if somebody says find out the maximum of 10 numbers. Then, we will be writing down half or half an hour and matching brackets and so on. Meanwhile, another friend of his, a high school student, not even a IIT student would have said, give me those 10 numbers. He would have written them by hand on the paper and said, this is the maximum and then he will laugh at all of us. The alternate logic is what I, what I told you earlier. Input is all right. Now, I start by saying max is equal to a, I check. What do I check now? Is b greater than max? If yes, what will happen? Max is equal to b, otherwise nothing. I come out here, I check. Is c greater than max? If true, what will happen? So, what I am doing is, I have converted the whole logic into a problem of writing a series of simple x. I start with the assumption that the first number is maximum. I check whether second number is greater than that. If so, I replace the current value of maximum. I check the third number. At any point in time if a number is smaller than any one of the earlier numbers, max will not be replaced by the new value. Do you see the intrinsic simplicity and the possible power of the second version? Indeed, whenever you think of finding out maximum of three numbers, you should automatically think of the second logic, because that logic is extendable. Consider this problem, find maximum of fine numbers. Those of you are very courageous and go home and try to write this in that complicated logic. If a greater than b, then less than greater than b, then if a is greater than c, then if a is greater than b, then if a is greater than e, then max is e, else something, else something, else something and madness. That is why I said those of you are very courageous and try that. I am a very tame fellow. I like this and it works. You agree that it works? It has also another great advantage of a possible generalization. Later on when we discuss arrays, where a, b, c, d, e will not be different names, but different indices indicating array elements. We will find that we can iteratively execute the algorithm to find out the maximum of thousands of numbers. So, is this clear? This is the power of a comparison will always ensure that a condition is evaluated and it evaluates to true or false. There is a special type in c plus plus called Boolean. It is called Boole actually. Remember the word Boole I had used while showing various types. That is this type. Technically the value is true or false. Practically c plus plus implements these values as a numerical value. Why I have an unnecessary jamella when I have only two possible values? I actually require one bit to represent that value. Since my smallest storage allocation is one byte, I might keep a true value or a false value somewhere in one byte location. But by convention, c plus plus says that a false value will be always stored as a numerical zero. True value will always be stored as a numerical one, not necessarily stored, but converted into zero or one in case you wish to use the computational result of a condition evaluation in any arithmetic operation. It is possible to do. It is possible to multiply m by a condition written in brackets n greater than 5. If n is greater than 5, the result of m multiplied by n greater than 5 will be m greater than 5 will evaluate to 1, otherwise it will evaluate to 0. We will not complicate our life by looking into what happens when condition is treated as a numerical value, but we just record for our understanding that internally conditions are actually equivalent to numeric. Technically they are true or false. Now the complication which my friend suggested. Can I not test conditions such as age greater than 12, age greater than 16 and age less than 16. This requires combining two conditions. Compound conditions are permitted and the compound conditions are implemented using the Boolean operators and or and not. You are familiar with and or not? Fine. Exactly the same logic is done. However, the and operator is twice ampersand and and. The all operator is vertical bar twice. Negation is a exclamation mark. It is also interesting to see the basic comparison operators greater than less than etcetera. You will all understand, but you notice that there is nothing like equal to anywhere here. Why? Equal to symbol is used by c plus plus to indicate that assignment operation. It cannot simultaneously indicate a comparison of. So, if you want to say is age equal to 13, you have to say if age equal equal 13. It is an interesting thing to see what happens versus if you write the second statement is wrong. Unfortunately, it is not wrong. Oops sorry, two equal. The first one is a valid condition. It compares whether age is equal to 13 or not. The second one is a valid assignment operation. What will do that operation? You might have written it in bracket where a condition should have been written, but as far as c plus plus is concerned, it is an assignment operation. It will assign the value 13 to age independent of what you have given as input. And the value of this expression as we have seen is what? 13. Now, there is a problem. Is 13 true or false? In terms of interpreting numerical values as Boolean values, the other way round. The Boolean value is converted to 0 or 1 to represent false or true. But even numerical value is by chance happens to be examined as a Boolean value, it will treat any non-zero value as true, 0 as false. So, this will be true. Anyway, these are side effects. As far as we are concerned, we neglect all of this. This is exactly how we will write operators inside or condition. Wherever a condition is written, we should always use equal equal to make equality comparison. Rest of it is straight forward. There are examples of these conditions. I will not go through those, but there are handouts which will be distributed. So, while we discuss the last segment of this course, I will distribute these handouts. There are seven problems in the handout. Some of them involve the older issue that we have discussed, protein point representation, etcetera. And some of them involve what we have discussed today. Please try to solve these problems at home. Some of these problems will appear in the lab that you will do next week. And you will have to… In the lab, of course, we do not expect you to write 50 line programs as an assignment. So, we will probably give you pre-written programs for solving the problems. You can actually compare those with whatever you have written. And we will ask you to modify those programs to do something more. That will be the essence of the lab from next week onwards. The switch statement is the last statement I wish to discuss. This is a very special form of if and is written in a completely different syntax. You have seen a menu anytime. Have you gone to a hotel? So, in the hotel that fellow says, this is my menu. On the left hand side, items are listed 1, 2, 3, 4, 5. You actually name the item when you order, but you can also say get me item number 4, get me item number 5. Menu is nothing but a list of choices. And you have to make a choice from given choices. In computer applications, it is not uncommon to find a screen in front of you when you go to an application. For example, banking application. So, you have a bank account. You log into that banking system and you say number 1, transfer money, number 2, withdraw cash, number 3, apply for a loan, number 4, apply for a checkbook. So, when you type 1, suddenly the program will go to that function, which will execute the operations for one. In such cases, you are taking an input a single value, which is a choice 1, 2, 3, 4, 5, 6. If the choice is 1, you have to do that and get out. If the choice is 2, you have to do that and get out. If the choice is 3, you have to do something else and get out. So, you require an if, but you do not require to complicate your life with nested ifs and on. Your choices are rather simple. In such cases, you may want a very special kind of if, which is a branch. So, you need some kind of an if, which has let us say and here you do not want a single answer yes or no. They are actually examining the value of if. If it is equal to 1, then you want to do something and get out. If it is equal to 2, you want to do something. Understand 1, do this, this, get out. 2, do this, this, get out. 3, do this, this, get out. Suppose you have given in your menu only 7 choices and now I type 8. 8, there is no action that you have written. So, there is something called default, meaning none of these you come here and get out. In each of these branches, you can specify actions. So, this is a very special kind of if. You can imagine this conditional box, which is not a conventional condition, because I am not writing a condition there. You can interpret this conditional box to be like a switch, a multipolar switch. Depending on the value, you put the switch in direction of 1, 2, 3, 4, 5. 1 means you will go there, 2 means you will go there, 3 means you will go there. It is like a routing algorithm. There are 5 rows going out. Somebody comes in saying where should I go? I say what is your value? 3. You go to 3. 2, you go here. Get the point. If I want to implement this, there is a logic available and this is the switch statement, which is available in C plus. So, this statement is written with a keyword switch. Inside the condition, you write an expression, not a condition. Sorry, inside the brackets, you do not write a condition. You write an expression. The value of that expression is evaluated. For example, if I want to examine the value of m, I can just write m. m is an expression, standalone. Based on the values, I can write a series of cases, case constant 1. That constant 1 means the value for which I want to execute that. So, here I will write an example. What I have written is a funny program. The funny program says that if the value of m is 1, n should be 2. If the value of m is 2, n should be minus 2. If there is any other value of m, n should be 0. I do not know why anybody would like to implement such an idiotic logic, but suppose there is a program, there is a problem like this. This is how it can be implemented very easily. It does not make sense if you have just one or two simple rules and simple values, but it does make sense when you have to do something like 10 actions here, 20 actions here, 5 actions here, 7 actions there. Then this becomes an extremely easy way of tabulating. First option, this second option, this third option, this fourth option. There are variations in this which is written, which you can very easily see. So, we will end the normal lecture here, but I still need 5 minutes from you. Has everybody got a copy of the… Oh, you stop distributing, is it? No, no, no, you should never stop. So, while these are being distributed, let me discuss a few things which are important. We have got a big confusion because on the Moodle, the Moodle still shows about 120 extra students in this course. I am going to take it up with the head of the metallurgy department to clean up the mess, but it is our desire that from next week when you go to the labs, there would be specific tiers allocated to a specific group of students. Second, yeah, yeah, yeah, please, please. Good question. This question I will reiterate. The Moodle operator, which gives remainder, does it work only for positive values? I will ask one more question. Does it work for floating point values? Have you checked it? When I said does it work for floating point values, he immediately said no. When I asked him, have you checked it, he says no, but he firmly believes and rightly so that Moodle operator has no meaning in the context of fractional numbers. The Moodle operator is defined for integers, whether it works for negative numbers or not. So, let me ask the general opinion. What do you feel? Should it work or should it not work? The Moodle operation as defined in arithmetic, is it defined only for positive integers? Yes, somebody is saying yes, somebody is saying no. Whatever is the definition, does C plus plus honor it? What does it implement? The correct answer at this stage is you do not know as it does not. And the correct place to find out is to experiment and find out. No, no, no, you are talking of a different thing. He is saying that he gave a negative value for that 31st December question and he got something else. He is therefore concluding that the Moodle operator does not work. That is incorrect conclusion. The conclusion is incorrect because that program is doing variety of other things. The correct way to test whether modulo operation works correctly on negative numbers or not is to write a simple three line program. Int M, input M, C out, M modulo 3, whatever, and now give different values to M and find out what happened. That and only that will confirm how C plus plus 1. I like this point because it gave me an opportunity to elaborate that once again I repeat what I said earlier. Normally in all your educational life you are used to either reading and understanding from the book or somebody explaining it to you what this means. And you are also used to whenever you have doubts to ask someone and get the doubts clarified. By all means continue to do that. That is a good practice. But I am saying now that you come to IIT, now you come to Indian education, you must also learn an additional dimensional learning, experimenting. So whenever you have doubt to experiment but let that experiment be focused, our friend did experiment. He did give a negative number in response to running a program. But he wrongly concluded that there is a problem with modulo. His conclusion may be right but it is based on wrong experiment. The experiment must be I do exactly what I have doubt about and see what C plus plus does. In addition I must also read more about that particular operation in C plus plus standards which are available on the net. So I do not know whether you have tried to say C plus plus tutorial on internet. You will get an extremely good cpp.com site which gives the complete tutorial. It has a 131 page PDF document. Unfortunately they have not opened source yet. It says you can make a copy for your own reference but you cannot distribute. Many of us who do not have easy access to internet therefore are denied access to that model. That is why our effort is to create our own nodes, keep them in open source and distribute. But those of you who have access to internet search these, examine these, discuss it with your friends. That is another part of the dimension of experiment. Whenever you experiment something share not necessarily only in the class. Share it with your friends. Share it with anybody. That is our knowledge and fitness. Yes. Not necessarily. You do not know what that garbage is. What? Correct. Yes. You are applying maths there. So now you do the following. You take two variables m and n. Write a program which says thus m by n. Give different values of m. Give different values of n and see what you get. Do that experiment. I would suggest that this is a good point that you made. But I would suggest that whenever you have these doubts did you ask your TA? Ah, very good. So I have to conduct a lecture for that TA. Good. Henceforth I will insist that all TA's do these things themselves for once. Okay. But it is a good point that you made. I would still suggest. Park it in your mind. Don't give up. And find out by more experimentation and more discipline. Anyway, I must end this class because I have promised I will end 5 minutes before. Just two last announcements. Important announcements. Tomorrow, sorry, tomorrow Saturday the Hindi lecture which I conduct in the morning will not be conducted in the morning. Because Professor Narayanan as I mentioned last time there is a group of students who were deficient in English and who have joined CS 101. Some of them will be having a separate class. So this is a reminder to everybody who has joined that program for the English deficient students. We are all meeting at 2 o'clock in the Kamal Reiki building opposite this place in room 301. And the additional Hindi explanations etcetera will be conducted in that way. Tomorrow we do not have an extra lecture. But next Saturday we will have an extra lecture for both the batches. Similarly, these handouts please try out these problems at home before you come to the lab. This time the lab handout will be given to you just before the lab. And there will be an assignment which will not be printed and given. It will appear out of nowhere on the Moodle at precisely one hour after start of the batch type. And within that one hour you will have to solve that problem, write a program and upload that program because all submissions are automatic. There are no hand submissions. Is that ok? Thank you sir.