 Hello and welcome back. In this lecture we are going to continue our discussion of conditional execution of statements in C++ programs. Here is a quick recap of some of the topics we have already seen earlier. We have seen the structure of a simple C++ program, how variables and their types are declared. We have seen the usage of assignment statements and arithmetic and logical expressions in assignment statements. We have discussed how statements are executed sequentially in a program and we have also looked at the usage of if else statements to do to achieve conditional execution of statements in a program. In this lecture we are going to look at conditional execution of statements using a new kind of statement called the switch case statement in C++ programs. And we are also going to look at conditional expressions. These are new kinds of expressions which can be evaluated conditionally. We are going to look at how they are written and how they are used in C++ programs. Now here are some useful facts that we already know. A program is nothing but a sequence of compiler directives, declarations and instructions. Normally the computer executes instructions in the same order in which the instructions appear in the program. However, we have seen that if else statements can be used to allow the computer to conditionally execute instructions not necessarily in a linear order. Now here is a simple programming problem. Several of you may be aware of the fortune program that is available in Unix and Linux based systems. Basically every time you run this program, it prints on the screen one of several predetermined anecdotes or messages. Suppose we want to implement a simple version of the fortune program. So what does the simple version do? It reads the date and time as integers from the user and then depending on the date and time it outputs one of only three predetermined fortune messages. So how do we go about solving this problem? Here is a simple flow chart that one can come up with for implementing a simple fortune program. So in this flow chart we first read inputs date and time. Let us say we read these as integers. Then we compute hash which is another integer as a function of date and time such that as we change date and time the value of hash changes but in any case the value of hash always stays within 0, 1, 2. So clearly this is a many to one function and specifically we are going to look at hash functions in more detail later in the course. But for the time being let us just assume that this is just a function of date and time which takes on values in the set 0, 1, 2. So once I have computed this hash value I can then check where the value of hash is 0. If yes I can print out the first fortune message, if no then I can check where the value of hash is 1. If the value of hash is indeed 1 I can print out the second fortune message. If the value of hash is neither 0 nor 1 then I print out the third fortune message and in all cases after printing out the message I return control back to the caller. So here is a simple animation of what might happen if the value of hash is 1. So as you saw the red circle which denotes the flow of control came down this side and then went this side, printed fortune message 2 and then returned control back to the caller. Here is how we might want to write a C++ program that implements the same flow chart. Here we have three integer variables and then we print out this message asking for the date in date month year format and the time in hour and minutes format. We read date and time as integers and then we compute a value between 0 and 2 using the values of date and time and this is just a simple way of computing this value. This is not a very sophisticated way of computing a value in 0, 1, 2 from date and time but this suffices for our purposes. Now why does this expression always evaluate to a value in 0, 1, 2 because I am computing the remainder on division by 3 and certainly on dividing by 3 the remainder will be 1 of 0, 1 or 2 and then I am checking if the value of hash is 0 then I am going to print out the first message. Let us say our first message is time and tide wait for none. If the value of hash is not 0 then I am going to check if its value is 1. If so I will print out the second message and let us say that the second message in our case is the pen is mightier than the sword and if the value of hash is neither 0 or 1 if it is indeed 2 then we will print out the message where there is a will there is a way. So what you see in this highlighted part is that we basically have nested if else statements and of course after executing this we execute return 0 and return control back to the operating system. Now in C plus plus if I were to write the same program but if I had 10 fortune messages to be printed out then I would have a very deeply nested if sequence of if else statements over here the nesting depth would be 10. I would have to write one if else statement and then inside that another one and then inside that another one and I have to continue that nesting for up to a depth of 10. While that would be a legitimate C plus plus program it is not the most convenient to write neither is it the most convenient to read. So therefore C plus plus basically provides an alternative to deeply nested if else statements. The point to note is that this alternative applies only when all conditions are of a particular form all the conditions in these nested if then else statements are of a particular form specifically they must check whether the same integer expression is equal to different constants. So pictorially this is how it looks like if the nested if then else in all the condition checks it is using the same integer expression and the condition checks are just checking whether that same expression is equal to one constant or the other then such a nested if then else structure can be replaced by an alternative statement in C plus plus which is called a switch case statement. So I want to highlight here that all these expressions are the same integer expression and all these comparisons are equality comparisons with integer constants. Now what does a switch case statement look like here is how it looks like here is our program fragment where we were printing one of three different messages depending on the value of hash I could write the same flow of control I could express the same flow of control using switch case statements as is shown here I am going to explain the different parts of the switch case statement but to begin with let us note that the switch case break and default are C plus plus keywords. The way the switch case statement is written is that the integer expression whose value I was comparing to different constants that integer expression in this particular case the integer variable hash is put as an argument of switch. So we say switch hash basically to denote that if the value of hash is equal to something and what is that something we write it as different cases. So switch hash case 0 is basically saying if hash is equal to 0 then you execute the statement and then we execute this new kind of statement called break I will explain what this means but for the time being let us just ignore it. Similarly if I look at case 1 so switch hash case 1 is basically checking if hash is equal to 1 and if so it is going to print out the next message let us ignore this break for the time being and then finally the default is saying that if the value of hash did not match any of these then this is the default message to be printed out. Now what are those break statements that we saw in the switch case statement what would happen if I did not put any break statements. So let us do that so here was our switch case statement with two breaks over here let us just delete those breaks and suppose I have now reached this point of the program as I am executing it and let us say the value of hash is 0 how would control flow through the program. The computer would first execute this statement denoted by this running icon the value of hash is 0 so it will go and execute whatever the executable statement for case 0 is so it will execute this statement print out the first message after that in the absence of the break statement it will just fall through to the next executable statement even if that statement corresponds to a different case. So it will just execute the next statement and once again it does not see any break so it will simply fall through to the next executable statement even if that corresponds to the default or a different case so it will execute that so basically it will print all three messages in this case and then it will execute return 0 and return control back to the operating system. So this is what is also known as fall through of control and in order to see how fall through of control works in a little bit more detail let us insert some of the break statements that we removed but not all of them so let us say I just introduced this break statement and now the value of hash is again 0 at this point of execution of the program. So the computer executes this statement then it executes this statement because the value of hash is 0 there is no break so it executes this statement after that it executes break and when it executes break it jumps out of the switch case and executes the last return statement. What would happen if the value of hash was 1 so the computer would execute this find that the value of hash is 1 so it will not execute this statement the statement for case 0 it will directly jump the statement for case 1 and it will continue execution from there subsequently until it sees a break statement or until it sees the end of the switch case statement so in this particular case it will see the break statement and jump out. So the point to note here is that depending on the value of hash the starting point for execution of my statements is defined after that it will continue to execute all statements sequentially until it sees a break at which point it can jump out of the loop or until it sees the end of the switch case statement. Now fall through as we have seen can also be used intentionally to serve some of our purposes so suppose I want all the fortune messages to be printed if the value of hash is 0 only two of them to be printed if the value of hash is 1 and only one if the value of hash is 2. So this was our original program in which case depending on the value of hash only one message would get printed but then if I just remove these two break statements we have just seen that if the value of hash is 0 it will print all three because of fall through if the value of hash is 1 it will print these two because of fall through and otherwise it will print this as the default statement. So the default in switch case statement is really similar to the final else branch in nested if else statements if the value of hash in our example does not match any of the case values then the default statements are executed. So that would be the case here if the value of hash is 2 the default statement would be executed. So now we have seen how statements can be executed conditionally in C++ programs it is also appropriate to look at this point expressions that can evaluate conditionally in C++ programs. So what are conditional expressions these are built on top of standard arithmetic expressions like a plus b star c or a plus b divided by c and suppose I wanted to do the following I want the same expression to take on the value of a plus b star c if a particular condition holds and to take on the value of a plus b divided by c otherwise. So the point to note it is the same expression should either have this value or have this value. Now simple way to do this is to write an if else statement. So if c is less than or equal to 0 that was our condition here then expression is a plus b star c otherwise expression is a plus b divided by c. While this is fine C++ provides an alternative way to do this we can write a single assignment statement on the left hand side of the assignment we have expression the variable to which we want to assign and on the right hand side we have this thing highlighted in red which is called a conditional expression. A conditional expression has 3 parts the first part is a condition followed by a question mark just like what we had in the if condition the second part is an expression that would appear in the true branch of the if else statement and the third part is the expression as it would appear in the else branch of the if else statement and these two expressions the one that appears in the true branch and in the false branch are separated by a colon. Now in general conditional expressions look like this there is a logical expression question mark if expression colon else expression the if expression and else expression must be of the same type and that will be the same as the type of the overall conditional expression. If both the if expression and the else expression are logical expressions the overall conditional expression is a logical expression if expression and else expression are arithmetic expressions the overall conditional expression is also an arithmetic expression. So in summary in this lecture we looked at conditional execution of statements in C++ programs, we looked at switch case, we looked at the usage of breaks and fall throughs, we also looked at the default construct and we also saw how conditional expressions can be written in C++. Thank you.