 Hello, welcome to the NPTEL course on an introduction to programming through C++. I am Abhiram Ranade, today's lecture is about conditional execution and the reading for it is chapter 6 of the book. So let me begin with an example, suppose we want to calculate income tax. So let us say we want to write a program which reads the income and prints the income tax. And the rules are as follows. So if the income is less than 180,000 or 180,000 then tax must be 0, so 0 should be printed. If the income is between 180,000 and 500,000 then the tax should be 10% of the income over and above 180,000. If the income is between 500,000 and 800,000 then the tax should be 32,000 plus 20% of the income above 500,000. And if the income is above 800,000 or 8 lakhs then the tax should be 92,000 plus 30% of whatever income is there above 8 lakhs. Now you will realize that you cannot write this program using what you already know. So we need to know something more. So here is what we are going to do in this lecture. I am going to talk about a new statement called an IF statement which will be useful in writing this tax calculation program and several other programs. So we will begin by looking at the basic form of this IF statement. We will use that basic form to solve a simplified tax calculation problem. Well actually we are not going to be calculating tax. We will see this simple problem in a minute. After that we will see the second form of the IF statement or the so called IF-ELS statement. Using this we will be able to write a better program to solve the simplified problem. Finally we will study the most general IF statement form and using this we will be able to do the simple, our full tax calculation program. After that we will write, we will see how to express compound conditions. So I have written complex over here but I really mean compound. After that we will do a case study. We will have a somewhat larger problem that we will solve and this will be about controlling the turtle in a different manner. Then we will learn about the switch statement and we will see yet another way of controlling the turtle. Finally we will study something called logical data. So let us start with the basic IF statement. So the form of this is IF then in parenthesis condition and then the consequent. Here condition is Boolean expression and I will explain what this means in a minute. Condition is something that a Boolean expression is something that evaluates to true or false. We will see this. Consequent is a C++ statement. For example it could be an assignment statement and consequent could also be a block. Again what I mean by a block is something enclosed, a set of statements enclosed inside parenthesis. Now the way this condition executes is as follows. So we evaluate the condition and if it evaluates to true then the consequent is executed. If condition evaluates to false then the consequent is ignored. I have not told you what exactly the condition is and what it means to evaluate it. I will do so next. So a condition is as follows. So I am going to first tell you what a simple condition is. A simple condition looks like some expression followed by something called a relational operator followed by another expression. So a relational operator can be the less than symbol, the less than followed by equal to characters which together constitute the less than or equal operator or two equal to characters. So these two equal twos together constitute the relational operator equal. Notice that if I just write the equality then in C++ this means the assignment operator. So since we have already used the equality for the assignment operator now we have to use something different and C++ designers decided that we will write equal to equal to denote the relational operator which compares to expressions. Similarly, we will have the greater than as a relational operator, greater than or equal to as a relational operator and not equal. So not equal is the exclamation mark followed by the equality character. So remember that there should not be any spaces between the exclamation mark and the equality and similarly between greater than equal, greater than or equal and so on. So the condition is considered true if expression 1 relates to expression 2 as per the specified relational operator realm. So let me take this through examples. So suppose we have x equal to 5, y equal to 10, z equal to 100 then if we write x greater than or equal to y then that is going to be false because 5 is not greater than or equal to 10. If we could write x squared greater than y, now x is 5, x squared is 25, 25 is greater than y because y is 10. So therefore this expression, this condition x squared greater than y is going to evaluate to true. So another example I could write x times y minus z equal to equal to 10. So what is x times y? x times y is 50, 50 minus z is 100, 50 minus z which is 100, x times y is 50 minus z minus 100, the whole thing is minus 50 and minus 50 is not equal to 10 and therefore this entire condition is false. So what I have told you over here is I have told you what conditions are and what does it mean to evaluate them. So now I have told you everything that you need to know in order to understand the if statement. Now it is customary to describe the if statement as a flow chart and what is a flow chart? It is a pictorial representation of a program or a statement. So in this statements or even parts of statements are put inside boxes and if box C will possibly be executed after some box B then we put an arrow from B to C. So this allows us to understand how control flows in between the statements. So the arrows indicate how the control flows and therefore such a chart is called a flow chart. Now this is especially convenient for showing conditional execution because in conditional execution there can be more than one next statement. So if I have a condition, if something consequent then I may execute the consequent after checking condition but I may not, I may directly go on to the next statement. So this is possible to be showed very clearly using a flow chart and assignment statements are usually put in rectangular boxes, diamond shaped boxes are used for condition checks. So we will see this in a minute. So if I have a statement if condition consequent then its flow chart is as shown in this picture. So this statement will be preceded in the program by a previous statement. So its flow chart will come over here. This statement will be succeeded in the program by a next statement and its flow chart will come over here. So the idea is that the previous statement will get executed followed by after that is executed then the control will use this arrow to enter the if statement and now the condition is going to be evaluated. Now this diamond indicates the condition and if the condition comes out to be true then the control moves out in this direction where we have put true. So if the condition is true then after that the consequent will get evaluated. After the consequent is evaluated the control will come out and it will go on to the next statement. If on the other hand the condition was false then this branch is taken out of the diamond and it will directly go to the next statement. So what I have told you using this flow chart is not really different from what I told you earlier. However, I believe that this is likely to be perhaps clearer or more easy to see it is probably more easier to see what is going on over here rather than when I wrote down that text where I gave you that text description. So now we can write the code to just determine if any tax is owed. So this is a simplified problem our original problem was calculate the tax actually. Now we are saying let us do something simple first just determine if any tax is owed. So we are going to read in the income but having read in the income we are just going to print a message saying yes tax is owed or no tax is owed. So here is what the program looks like you probably will be able to write it so I am not going to make a big deal of explaining it and let us just jump into it. So first I am going to have a variable in which we are going to store the income and maybe we will have a variable tax in this case the variable tax is useless but it does not matter if we have it then we are going to read in the income. So we are going to read in the income from the keyboard we could have put in the message saying see out give your income but that is okay. So let us say that is understood when the user invokes our program the user will type that message anyway without receiving any prompt for it. If now we check if the income is less than 180,000 you remember from the rules that in that case there is no tax. So what do we do? So then we are going to see out no tax owed. However if income is bigger than 180,000 then we are going to print out UO tax. So we read the income then we compare that with 180,000 so this is expression 1 this is the relational operator and this is expression 2. If the income is actually less than 180,000 we execute the consequent which is this and the consequent simply requires us to print this message. So if we execute this and if it comes out to be true we execute this and we come on to this statement. If this comes out to be false that is income is bigger then we directly come to this statement without printing anything so far. So in any case after executing this statement we come over here control comes over here and we check is income bigger than 180,000. If it is bigger than 180,000 then we are going to print a message saying UO tax otherwise we are going to directly go to the next statement. So the next statement is not there so we terminate the program so very simple. Now from the description that I just gave you will observe that I am going to check both these conditions on every execution. So we check this condition and maybe I will print out this message or maybe I do not print out this message but I again so I subsequently check this condition. So the program is correct it does what we want to do but you may observe and you may think it surprising that the program needs to check both of these conditions because after all if the first condition is true then we know that the second condition must be false. So why should we check it? So we will see that this can be remedied and we can write a slightly better program for this. So anyway to summarize this is the perfectly fine program it does the job we want except that it checks a condition 2 times and you would like to know if we can avoid checking twice and indeed we can. So for this we will need another form of if statement. So this form is if condition consequent else alternate. So in this the condition is first evaluated if it is true then consequent is executed. If condition is false then alternate is executed and alternate as well as the consequent can both be blocks. So what does it mean to have a block over here? So it means that if condition is true all these statements in the block will be executed and likewise if the condition is false all these statements in the block over here can be executed will be executed. So here is if else as a flow chart. So after executing the previous statement before the if else statement we enter the flow chart of the if else and the action that we perform is we evaluate the condition. If the condition is true we proceed out of this branch and then we execute the consequent. After executing the consequent we go on to the next statement in the program if the condition is false we proceed out of this branch we execute the alternate and we after that we go to the next statement. So you will see that this statement was sort of designed to improve our tax calculation program. So here it is we are we have the same part so far but now instead of checking whether income is greater than 180,000 since we already know we can just say uo tax because the else is going to be executed exactly if 180,000 income is greater than 180,000. So the else part or the alternate part will be executed only if income is bigger than 180,000 in which case uo tax will be printed. So in other words what we have done is we are checking the condition only once only one condition is being checked and so in that sense this program is a little bit more efficient than the previous program and also it is a little less verbose. So by writing else you immediately understand what is going on otherwise if you write the whole condition again then you have to carefully observe that oh this condition is really the complement of this condition by else you know much more clearly that this is going to be executed or this is going to be executed one of the two is going to be executed. So an exercise write a program that reads in a number and prints its square root. If the number is positive it should use the square root function if your program should use the square root function if the number is negative your program should invoke the square root function on the negative of the number. So that will be a positive quantity therefore you can invoke square root on it and so you should print the result followed by the letter I to indicate that the result is imaginary. So what have we discussed? We have discussed two forms of the F statement. Next we will discuss the more general form but before that let us take a quick break.