 Hello and welcome back. In this lecture, we are going to look at the use of logical expressions in assignment statements in C++. Here is a quick recap of some relevant topics. We have seen the structure of a simple C++ program. We have seen how variables and types are declared, how variables and functions are named and we have also looked at the assignment statement and the use of arithmetic expressions in assignment statements. In this lecture, we are going to look at logical expressions in assignment statements. Now, let us quickly have a recap of the assignment statement that we have seen earlier. The general form of the statement is we have a destination, then the equal sign and then the expression and this is to be read as the value of the expression is assigned to destination. So, the expression has to be evaluated to a value and it has to be stored in the destination. And we have also seen that since the expression evaluates to a value and since every value must have a type, so the expression must have a type. Now, let us try to deal with logical expressions in an assignment statement. To see this, let us look at our original program which had an arithmetic expression a plus b and this program if you recall was reading two numbers, adding them up and printing out the sum. So, this is a program with arithmetic expression. Now, here is an example of a program with a logical expression. Now, if you look carefully at this program, what is this program doing? It has three integer variables declared a, b, c just like your previous program, but it also is a Boolean variable declared called is a largest. So, note that I have named this variable such that it conveys the purpose of this variable. Most likely, this variable should be set to true if a is the largest of the three values a, b, c and then we say give three numbers, this message is output on the screen and then we read three numbers and then we compute the value of this Boolean variable is a largest in this manner and this is what we are going to call a logical expression. So, we will look at the details of this in a few minutes in this lecture, but let us for the time being identify this as the counterpart of an arithmetic expression that we had in our earlier program. This is a logical expression it will evaluate to a Boolean value true or false and that will be assigned to this Boolean variable is a largest and finally, we want to print this value out. So, we want to say is a the largest and we want to print out the value of is a largest and we will see in this lecture what happens when we print out a Boolean value and finally, of course, the function executes the main function executes return 0 to hand over control back to the operating system. Now, in C plus plus the simplest way of constructing a logical expression is to compare two int or float values. For example, in our program we had a b c as three int variables. So, I could compare them by saying a greater than b a less than c b greater than or equal to c a less than or equal to c these will all constitute logical expressions note when I write greater and then follow it with the equal sign it means one comparison operator these two symbols together constitute one comparison operator called greater than or equal to similarly when I write less than and follow it immediately with an equal symbol these two symbols together constitute a comparison operator called less than or equal to. So, as I said all of these are logical expressions they have data type bool and given integer values of a and b each of these expressions evaluates to either true or false if the value of a is greater than the value of b a greater than b evaluates to true otherwise it evaluates to false similarly for the other comparison operators. We also have equality based logical comparisons this is trying to check if a and b are equal this is trying to check if the values of b and c are not equal. So, this operator note that there are two equal symbols here this checks if a is equal to b and this operator which is the exclamation symbol followed by equality checks whether b is not equal to c the important thing to note here is that I have used two equality symbols when I am writing this comparison is a equal to b. Now, we have already seen the use of the equality symbol in assignment statement. So, if I write a single equals b I am denoting an assignment to the variable a of the value of the variable b a equals b says compute the value of b or take the value of b and assign it to the variable a whereas a equals equals b means it is an equality comparison where I am comparing the values of a and b and asking if they are equal. Now, here when we use these comparison operators we use variables as their arguments I could also use arithmetic expressions for their arguments for example, I could check if a plus b star c is less than c minus a percent b you already know the meanings of these symbols from our previous lecture on arithmetic expressions. Now, there are also logical operators that are used in c plus plus for example, we use 2 ampersand symbols to denote the binary logical operator and for example, I could say a greater than equal to b and a greater than equal to c. I could use 2 vertical bars to denote the binary logical operator or for example, a greater than b or a greater than c and I could use one exclamation symbol to denote the unary logical not operation or the negation operation. For example, I could say negation of a greater than equal to b and a greater than equal to c. The truth value of this is of course, going to be exactly the opposite a greater than equal to b and a greater than equal to c and we can build complex logical expressions using parentheses. So, for example, here I am saying a greater than equal to b and a greater than equal to c or a equal to 1 I can do this comparison between a variable and a constant as well and b not equal to a plus c where I can I am doing this comparison between a variable and an arithmetic expression and I can use parentheses to indicate exactly the order in which the different comparison operators should be evaluated and the different logical operators should be evaluated. And note that all logical expressions whether they are the simple ones involving comparison operators or whether they are more complicated ones involving logical operators they always evaluate to a Boolean value. Now, just like we had operator precedences for arithmetic operators. So, we also have operator precedences for logical operators and for comparison operators less than less than or equal to greater than greater than or equal to these have the same precedence and their precedence is lower than that of the logical negation operator and they are all left associative we have seen what left associative means in our discussion on arithmetic expressions equals and not equals they have the same precedence, but their precedence is lower than that of less than less than or equal to greater than greater than or equal to and they are also left associative. So, for example, if I write an expression like this what it means is that the operators with higher precedence greater than equal to and less than must be evaluated first and then the operator with lower precedence must be evaluated which is the not equal to operator here. We can also have logical operators and precedences between them for example, among the logical operators negation has the highest precedence then is binary and and then is binary or binary and is left associative and so is binary or. So, for example, if I write this logical expression the way to interpret it is first evaluate the operator with the highest precedence. So, it is not flag 1 not flag 3 then evaluate the operator with the next higher precedence which is the binary logical operator and so it is flag 2 and not flag 3 and finally, evaluate the operator with the lowest precedence which is all and needless to say just like for arithmetic expressions the best practice in order to avoid confusion is to use parentheses to specify meaning unambiguously. Now, what happens when we print logical expressions we know that all logical expressions evaluate to the value true or false and we have already seen in an earlier lecture that the internal representation of Boolean values could be 1 byte or could be 4 bytes as well this is not specified explicitly by the C++ standard but it is some kind of integer representation inside the computer false must be represented as 0 true can be represented as any nonzero quantity not necessarily 1. However, the C++ standard does specify that when you are printing a logical value true must be printed as 1 and false must be printed as 0 and we could also print the true false strings if we use this manipulator called Boole alpha. So, for example, if I say C out less than less than Boole alpha less than less than a greater than b and if it turns out that the value of a is indeed greater than the value of b then this is going to print out the string true on the other hand if I did not have this Boole alpha here and if I just printed out C less than less than a greater than b and if the value of a was greater than the value of b it would print out 1. And now going back to the program that I showed right at the beginning of this lecture in this program now if you look at it I am basically checking whether a is greater than equal to b and a is greater than equal to c and if that is the case this Boolean variable gets set to the value true and when I execute this statement it prints the value 1. So, in summary we saw in this lecture logical expressions in C plus plus how we can construct simple expressions with comparison operators how we can combine these simple expressions into more complicated expressions using logical operators and parentheses and we also saw what happens when we print logical values. Thank you.