Relational Operators

The course is part of this learning path

Start course
Overview
Difficulty
Beginner
Duration
2h 17m
Students
37
Ratings
5/5
starstarstarstarstar
Description

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++

Prerequisites

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

 

Transcript

The previous lecture introduced us to our first operators in this course. Specifically, these operators were used to perform arithmetic on compatible variables and values. In this lecture, we will explore operators that will help us when our programs need to make decisions based on conditions that are either true or false. As you probably recall, the data type that can contain either true or false values, is the Boolean data type in C++ keyword bool. So, let's create a new project called RelationalFun. Create a new project, Empty project, and then we will call it RelationalFun. I will create a new main file. Right click, 'Add', New Item, main and, of course, let's give it the skeleton. All right. Now, in the source code for this project, we're going to be doing simple comparisons among values and variables. First, let me write out the relational operators in the comments. So, I'm going to write some comments that we've learned about recently. And what we have here is the > greater than, >= the greater than or equal to, < the less than, <= the less than or equal to operator. And we also have the specific and special subset of equality operator. We have the equal to or equal-to operator, equality operator. And then, we have the != or not-equals-to equality operator or not-equal-to rather. It's a type of equality. Now, the equal-to operator and the not- equal-to operator are also further classified again as equality operators within this large umbrella term relational operators. So, make a mental note that to compare two values to determine if they're equal, you don't use the single equal symbol because that is the assignment operator, which we've seen many times before. We have to use two equal symbols. So, if you put just one and you say like a = 5 and you use one and you're intending to compare the elements, you're doing it wrong. That's actually going to assign the value 5 to the variable a. But if you say a == 5, you're asking is a equal to five? All right. So, let's test some of these out. We're going to first set the boolalpha stream manipulator in main. So, we get some nice true and false. Some people prefer writing this thing out with every cout line that they do, but it's really not necessary. It's called a sticky operator. So it actually sticks around if you just type it and it'll be good until you write noble alpha. So it may not always put true and false rather than zeros and ones. Alright. So, let's also create two integers again a and b. And set their values to 15 and 20 will say, respectively. So a and then b. And now we can do a comparison using the relational operators inside parentheses in one of the cout statements. So, we'll do this, a < b just to keep it nice and clean. And let's run the code and see what happens. So, Debug, Start Without Debugging. There we go. And it says true. Excellent. So just as expected, this tells us that a is indeed less than b. In fact, a couple interesting features of all the relational operators are that, their operands just have to be comparable values of some sort in C++ and the value they return is always a Boolean value. Let's do another quick example. This time we will use the equal to operator. Since a and b are not equal, this operator should return false right? Also, this time instead of directly calculating the value in parentheses in the cout statement, we will store the result in a Boolean variable and then print the value of that Boolean variable. So, bool areEqual = a == b; Now you could put that in parentheses but it's not really necessary. And then, we're going to do at the bottom, print out areEqual. So, let's run this and see what happens. Debug, Start without debugging and you see that even though our a < b is true, they're not equal. So, we get a false for are equal. Right? So, that was expected. That's what we wanted. So now it's that time again, you guessed it. It's time for a short challenge. I want you to create a variable named age and set it to your actual age. Then, in a cout statement, use the appropriate relational operators to determine if your age >= 21. You can use the same project if you'd like. So pause the video and come back when you're done or if you need some help. Alrighty there. Did you get everything working? Let's do the challenge together to make sure that you're confident in your skills with relational operators. So, I'm going to write my code just using the same project here. I'm going to say, int age = 36; and then I'm going to print my age compared to. Actually, I'm 37 now, I had a birthday. See this is what happens when you get old. You keep forgetting that you've had birthdays. So, cout we're going to say, cout << (age >= 21) << endl; and we'll see what results we get for that. So we'll say, maybe we can even print in front of what we'll say, "Age >= 21?" And then a couple of spaces and then another stream insertion operator. So let's run this and see what we get. Debug, Start without debugging. Alright. There we go. It says, is that age >= to 21? And the answer is true. 37 is in fact >= 21. So, let's test it against a different value just to see what happens. So, let's say 16 and run it again. And as you can see, is the age >= 21? 16 is not greater than equal to 21. So, we get false. Once again, exactly what we expect. With the greater than or equal to operator. Also, if the variables value is 21 exactly, it will return true because of the equal symbol being included. We don't have a single symbol for greater than or equal to like you would in Math, you just draw an underline. But in C++ and in typing, we have to put greater than and then followed by an equal to. So, in this lecture we've learned about the fundamental relational operators that are available in C++. We tested out some of them and saw that the values that are returned are always Boolean values. That is, either true or false. We will use these operators a lot more as we move through the course. In the next lecture, we will deal with logical operators which not only return Boolean values but must have Boolean operands as well. Put your thinking caps on and I'll see you in the next lecture.

 

About the Author
Students
316
Courses
20
Learning Paths
4

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