 Hello and welcome back. In this lecture, we are going to look at conditional execution of statements in C++ programs. Here is a quick recap of some topics we have studied earlier and that are relevant to this lecture. We have seen the structure of a simple C++ program. Within that, we have seen how variables and types are declared. We have seen how assignment statements are written and how arithmetic and logical expressions are used in assignment statements and we have also discussed how statements are executed sequentially in a C++ program. In today's lecture, we are going to look at conditional execution of statements in C++. Specifically, we are going to look at the if-else construct or statement and its different variants. Now, here are some facts that we have already studied. I would like to recall them here before we get going with today's lecture. We all know that a program is a sequence of compiler directives, declarations and instructions and normally, a computer or Mr. Dumbo executes instructions in the same order in which they appear in a program. So, if you have written the instructions from top to bottom separated by semicolon, the computer will execute the instructions as it encounters them top to bottom in the program or if you have written the instructions left to right separated by semicolon, the computer will execute them as it encounters them left to right separated by semicolon. Now, let us look at a simple problem and why executing instructions exactly in the same linear sequence may be a problem or may be not the best way to solve a problem that we are going to consider now. So, suppose I want to divide the integer a by the integer b and output the quotient q. Now, the sound simple and here is a program that could do this. You declare three integer variables a, b and q. You get the inputs a and b after printing a message saying give a and b. Then you compute the quotient and then you output the quotient and then you return 0. So, there does not seem to be anything complicated about this program except that what would happen if b was 0? When we do this division, then we would effectively be doing a division by 0 and that is not a very well defined operation to do. So, do we really want to divide if b is 0? And if we want to not divide if b is 0, then we need to reformulate or re-articulate our problem in the following manner by saying that divide a by b and output the quotient q if b is non-zero. Otherwise, that is if b is 0, we can simply output the string bad inputs because then the division would lead to dividing a by 0 which is not a very well defined operation. So, as you can see that we need conditional execution of instructions or blocks of instructions in order to solve a simple problem like this. The different blocks of instructions that I might need to use to solve this problem are reading inputs a and b, dividing a by b and outputting the quotient q, outputting the string bad inputs and returning control to the caller or the operating system. Note that although I have written the instructions in this manner one after the other, I do not intend to say that they will be executed in this manner. All I am trying to depict here is that these are the different blocks of instructions that I might need in order to solve this problem as specified here. Now, reading of the inputs a and b must be unconditionally executed and this must be the first thing that we must be doing. However, dividing a by b and outputting the quotient q should be a conditional execution. We should execute this next after reading a and b only if b is non-zero. However, if b is 0, then we should be executing this and not the previous instruction. And finally, returning control to the caller or the operating system has to be unconditionally executed at the end. So, what you see here is that there are some instructions that have to be unconditionally executed, whereas there are some other instructions that have to be conditionally executed depending on the value of b in this specific problem. So, now b is non-zero, then the sequence of execution that we should have is the following. Read inputs a and b, this of course is unconditionally executed first. Now, since b is non-zero, we should then execute this instruction, divide a by b and output the quotient q and we should not be executing the other instruction. And finally, we should be returning control to the operating system. On the other hand, if b is 0, then the sequence of execution should look like follows. We should unconditionally execute the first instruction and read inputs a and b, not divide a by b and output the quotient q, but instead output the string bad inputs and then return control back to the operating system. Now, this form of conditional execution is very nicely and appealingly represented through what we call a flow chart representation. So, flow chart is basically a visual representation of how control flows in a program. So, here is a simple flow chart for the program that we just saw. We read inputs a and b, groups of statements are often put in rectangular blocks and this means that all the statements here are sequentially executed. Then we do a check is b equal to 0. If the answer to that is yes, then we execute this block of statements where we output bad inputs and then come here and return control to the caller. In our case, it is the operating system. If on the other hand, b is not equal to 0, we execute this alternative block of statements where we compute the quotient of dividing a by b, we output the quotient and then we come back and execute the same sequence of instructions to return control back to the caller. So, these rectangular blocks as I mentioned represent block of instructions. They need not be one instruction. They could be multiple instructions which are executed in sequence, whereas these diamonds denote conditional flow of control. When control reaches here, we check whether the condition here is true or false. If it is true, control flows along the true branch, otherwise control flows along the false branch. In this picture, it is depicted as the yes branch and the no branch. Now, here is a heroic animation of how the control might flow in a particular execution of the program if after reading the input b, b was found to be nonzero. And here is another animation saying how the control would flow if the input b was found to be 0. So, what you observe is that control can only flow through one of these two blocks but not through both. And this is a salient feature of conditional execution. Depending on the condition, one block or the other is executed not both. In C++ programs, such conditional execution is conveniently represented using if else statements. So, we have denoted little program here which basically does the functionality that we required in our original problem. If b is 0, then we are going to output bad inputs. Otherwise, we are going to compute the quotient and output the quotient. Of course, this is not a complete program. I have not shown the variable declarations, other compiler directives, etcetera. But this is just to focus on the if else construct or if else statement in C++ programs. If and else are keywords which are used to specify a conditional execution, the condition that you put, the condition based on which control would either flow through the yes branch or the no branch has to be a logical expression and it has to evaluate to true or false. In this particular case, it is a comparison operator which evaluates to true or false. And then in the yes branch, we could have a block of statements grouped by two braces. And similarly, in the no branch, we could have another block of statements grouped by braces. We have already seen that statements can be grouped by braces and this is just an application of that. Now, the blocks in an if else statement could be any sequence of C++ statements and in fact, they could be another if else statement itself. So, for example, look at this program. Here, if b is 0, then this statement is to be executed. Otherwise, another if else statement is to be executed. So, inside this else branch or the no branch, I have a statement which is itself another if else statement. And C++ allows arbitrary nesting of if else statements by means of which you can write programs with fairly complicated control flow. C++ also allows you to write if without the else. The else is indeed optional, but basically, when you write an if statement without else, this is equivalent to saying it is the if statement and there is an else branch in which you do nothing. So, this is really a succinct way to write programs when nothing needs to be done in the else branch. Parantheses have to be very carefully used when you are writing if else statements. For example, consider this program. Here, perhaps the intent of the programmer was to say that if b is 0, then we should see the message hello world with 3 exclamation marks. However, if the program is written like this, this block would be would be construed as an if statement without an else statement and this would be unconditionally executed. So, therefore, if b is 0, then this would be interpreted as an if statement without an else statement and hello would be printed and then because this is unconditionally executed, world would also be printed. However, if b was not 0, then hello would not be printed, but just world would be printed. Now, if else statements can also be combined with other statements and in this respect, they are just similar to assignment and input output statements. We can also have return statements in an if block or an else block. So, for example, here is a program. With conditional execution that solves the problem that we were trying to solve and here in this program, we have a return statement within an if block. So, if you look at this program, this has the compiler directives, it has some comments, some variable declarations, a message asking for the values of a and b, reading the values of a and b and if b is 0, it outputs this message and returns straight away from here and in this case, it returns minus 1 saying that something bad has happened. So, this minus 1 could indicate to the caller that something bad has happened. Otherwise, it computes the quotient, outputs the quotient says be happy and then returns 0 and this 0 might indicate to the caller that everything was fine. So, in summary, we looked at conditional execution of statements in C++ programs. Specifically, we looked at the if else statement and its usage. We looked at nesting of if else statements and we also look at an example program with conditional execution to see how if else statement works. Thank you.