Logical Operators
Start course
2h 17m

This course provides you with a solid understanding of the fundamentals of C++. We will take a look at the components of the programming language and then put these into practice through a couple of projects that we will run through at the end of the course.

Learning Objectives

  • Learn how to store different types of data in main memory
  • Understand how to manipulate and perform operations on that data, including performing arithmetic on numbers
  • Understand how programs make decisions
  • Learn how you can write your programs to communicate with users

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • Experienced C++ programmers who want to stay sharp!
  • College students and anyone studying C++


This is a beginner-level course and so no prior knowledge of C++ is necessary.



So, what have we learned so far? In this section, we have learned how to declare and initialize variables with values and that variables and values both have data types. We explored some of the very common data types in C++ including int, double, char, string, and bool. We've also explored various operations that we can perform on data. Specifically, we explored arithmetic operators and just finished discussing relational operators. One of the key features of relational operators is that they perform a comparison of two comparable values or variables and always return a Boolean value. In this lecture, we'll look at three operators that are used to combine or change Boolean variables or literal values. The way that I'm going to introduce the logical operators is to use this Excel Workbook briefly. Now we're not going to do any math in Excel or use formulas or functions, I'm just using it to easily display some information in tabular format. We're going to learn the three truth tables, each table corresponding to each of the three logical operators. The names of the logical operators are Logical AND, Logical OR, and Logical NOT. Before creating these truth tables, I want to mention one other characteristic we use for describing operators. We've already learned that we can classify operators by what they do. That is, arithmetic, relational, and logical operators all have different purposes. However, we can also classify operators by how many operands they take. If an operator like the addition operator, the multiplication operator, the greater than operator and many others take two operands then these operators are called binary operators. If they take only one operand like the increment and decrement or as we're about to see the Logical NOT operator, then the single operand operators are called unary operators. There's also one operator that takes three operands called the conditional operator which is the only ternary operator which means it takes three operands in C++. We won't talk about that operator here but I just want you to be aware of its existence at this point. So, for the logical operators, we have two binary operators the Logical AND and the Logical OR, and one unary operator, the Logical NOT. Let's write out the truth tables for all three of these in Excel. So, I'm going to put TRUE and we're going to have TRUE, that yields TRUE and we'll discuss why this is the case in just a moment. TRUE and FALSE is FALSE. FALSE and TRUE is FALSE. FALSE and FALSE is FALSE. Logical OR, TRUE or TRUE is TRUE. TRUE or FALSE is TRUE. FALSE or TRUE is TRUE. FALSE or FALSE is FALSE. And this one is really easy. TRUE, FALSE, FALSE, TRUE. Now, one thing to note is that with traditional formal logic, we use the symbols p and q right here to represent Boolean values. But you need to know that in practice and programming these don't need to be just values or variables, they can be anything that returns or represents a Boolean value. This would include variables, values, the result of another operation like the relational operations, which of course return Booleans themselves or even a function called that returns a Boolean value. But maybe we're getting a little ahead of ourselves. The Logical AND operator combines Boolean values in such a way that it returns TRUE only if both of its operands are TRUE and returns FALSE otherwise, that's why we have TRUE TRUE equals TRUE but the others return FALSE. If both of them are not TRUE then we get FALSE. The Logical OR operator combines the Boolean values in such a way that it returns TRUE as long as at least one of its operands is TRUE, that is, the only way it returns FALSE is if both of its operands are FALSE. And the final one down here is a unary operator which is why it has less rows here, and this is the Logical NOT operator. It is quite simple in that it simply reverses the truth value of its operand. If its operand is TRUE, NOT TRUE is FALSE and if its operand is FALSE, NOT FALSE is TRUE. That's easy, right? I encourage all my students in any programming class I teach to memorize the truth tables and to understand why the values are what they are. I know sometimes educators speak negatively about memorization but there are times when it is necessary and appropriate. You memorize terms, definitions and in this case a model of the output of operations in the form of truth tables. Understanding them is clearly also key. Let's move on and use these to print out some truth values. First, let's work with actual bool variables, isRaining and isWarm. So, first we'll create a nice little project here. Empty project for C++ and we'll just call this WarmRaining. So, as always I right click source files, add new item, make sure it's a C++ source file and change it to main.cpp for the name. I write my skeleton code right here and that the main function, and for here, I'm going to create a bool isRaining = true, bool isWarm = true. And now we can compare them in a cout statement inside parentheses. Let's use AND and also use OR for combining these and also write one line with notIsRaining to see how Logical NOT works. So, right here, I'm going to say for the And, we get isRaining and isWarm. And then for the Or, isRaining or isWarm. And in case you're not sure of how we got these symbols, this one right here is the ampersand which on a standard English keyboard is 'Shift + 7'. So, the shift key and then 7, we have the ampersands right here, there are two of them and for this one right here, these vertical bars, those are typically found above the enter or return key on the same key as the backslash. You just hold down shift and you get these two vertical bars. So, we'll also do an example of NOT. So, I'm going to put Not isRaining. There we go. Just to test it. So, let's run and see what we get. Start, start without debugging and as long as it compiles with no problem, we should get some output. And we do. Now you notice it's giving 1, 1, and and if you remember how to correct this, it's true, true, and false clearly. So, it's giving us the answers we should expect. But remember we could add iomanip, iomanip for the iostream manipulators and then before all this, we can put boolalpha, right? So, let's run it again and see if we get what we expect. And now it looks a little bit more like English. So, for AND we get true, OR we get true and then NOT we get false. Because since isRaining and isWarm are both true, true && true is true. If we look back at our truth table, we could verify that true and true is indeed true. You'll also notice that true || true is clearly true and then since isRaining is set to true currently, if I put Not isRaining, what that's going to do is it's going to reverse the truth value. And since isRaining is true, Not true is false. Awesome. So, that gets pretty much what we expect. I do have a simple challenge for you, leave the rest of the code alone but change the values of isRaining and isWarm to try all the different permutations. So, we already did true, the values true and true for the isRaining and isWarm but I want you to try the different permutations and then keep running the program to see if you can see all the different results. So, pause the video and come back when you're done or whenever you want to see the solution. Okay, I hope you were able to get that to work. Let's change the code and rerun it for each of the remaining permutations of truth values. Since we initially wrote the code such that isRaining and isWarm were both true, the remaining permutations are isRaining is true isWarm is false, isRaining is false isWarm is true and both isRaining and isWarm are false. So, these are three different runs we need to test. So, isRaining is true, isWarm is false. So, let's run that. It'll recompile it and we'll get some solutions here. And as expected, remember the definition of the Logical AND says both of the operands have to be true for the whole thing to be true and even though isRaining is true, isWarm is not, isWarm is false. So, that means it's yielding false for the whole thing. OR is a lot more forgiving as long as one of the operands is true, the whole statement is true. So, that means that OR is true here and then NOT again still reverses the value of isRaining. So, that's one of the permutations. We could also try false and true. So, I've just swapped them here, isRaining and isWarm. If I run this, I get false for the first one which is just what we did before. It's just they're swapped so both of them would need to be true for the whole thing to be true and that's not the case right now. For OR, even though isRaining is false now it will check the second one and see that that is true and then return true for the whole thing. And then this one is a little different than what we had before because now isRaining is false so Not false is true. So, the last permutation is when both are false. So, I'm going to change both of these to false and then run it. The AND operator yields false because neither of them are true, let alone both, OR yields false this time even though it had been yielding true all the other times because both of its operands are false. So, it needs at least one to be true for the whole thing to be true. And the last one of course since isRaining is false, Not isRaining is true. So, brilliant job. I hope you're learning quite a bit. Up next, we will discuss constants.


About the Author
Learning Paths

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics