Learn how to compare values in C++. In C++ and most programming languages, it is fairly easy to compare numbers. You use the standard less than, greater than, less than or equal, etc.
- [Narrator] How to compare values in C++. In C++, like many other programming language, it's fairly easy to compare numbers and even characters. We use the standard less than symbol, greater than, less than or equal to, greater than or equal to, and then, because we already use the equal sign to assign values, we needed another way to compare two items to see if they're equal. So we use equals equals. So to compare values, we're going to introduce an if statement.
If something is true, then we execute a block of code. Let's take a look at an example. Here, I have int temp equals 30. Then I say, if temp is less than 32, then C out, freezing, else. So that means that temp is not less than 32. It's going to execute the statement that says above freezing. We can also use nested ifs if we need to check a complicated comparison. So let's take a look at this example where I'm checking some grades.
So I have int grade as equal to 75, but what I want is I want to know if they got a B, a C, an A, or something less than a C. So in this case, I have int grade equals 75, and I'm going to start by checking to see if the grade is greater than 90. If it is, I'm going to print out A. At that point, I'm done. So I won't go to the else statement. But if it's not greater than 90, then I'm going to go to the else that says else if grade is greater than 80.
If that's the case, that means that wasn't greater than 90, but it was greater than 80, so that means they get a B. Then I say else if grade is greater than 70, they get a C. And finally, the last else is what we call a dangling else. It kind of grabs everything else. Meaning that, if the grade was not greater than 70, it could be anything from zero, even negative, all the way up to 69. In that case, they got less than a C. When we compare values, we can use the compare operators with numbers and even characters.
Remember, characters are actually stored as integers using the ASCII conversion set. Floating point numbers, though, are a different story. They're not stored precisely. So if you're going to store, let's say the value .7, what happens is C++ actually stores it as .69999999, and I'm not sure how many nines. But it doesn't store it precisely. And that could be a problem if we're trying to compare values. So to compare floating point numbers, you want to use a tolerance.
So, I wanted to show you how this works. So let's switch over to our Dev-C++ IDE. And let's create a new project. And we'll make it a console application. And we'll call it comparison. We'll click ok. We'll put it in our C++ essential training library, and we'll click save. Okay, so in this program, I want to start by adding my using namespace std.
Alright, and in my main function here, I'm going to go ahead and declare a variable of type float. I'll say float X, and I'm going to give it the value of .7. Now, in order to show you how this happens, I'm going to say if the variable X equals equals. 'Cause I should be able to compare two numbers. If it equals equals .7, then I want to print out a message that says they are the same.
If it's not equal to .7, I want to know that as well. So I'm going to print out a different message that says the numbers are different. And when they are different, let's see what happens if I print out X. So let's do that as well. So let's do C out less than less than X. Notice, I want to print out the two statements. The numbers are different on line 11, and the C out X on line 12. But I only want to do that if X is not equal to .7.
Since there's more than one statement, I need to use curly brackets to indicate this is the start and end of the block of code that I want to execute. So I'm going to put an open curly bracket before these two statements, and I'm going to put a closing curly bracket after those two statements. That way, I won't always print out the X value. I'll only print it when they're different. Let's go ahead and run the program and see what happens.
And this is what's really odd, is that's the numbers are different, but it prints out .7. And that's 'cause behind the scenes, what's happening is the float value really is .6999999 et cetera. Okay, so how can we fix this? Well, one of the things we can do is we can use a tolerance, and say that if the difference between the two values is less than some value, then they're basically the same. Now, I want to make sure I account for positive and negative. So I'm going to add another library up here.
I'm going to add the math library. We haven't talked about this yet. But if I add cmath as my header up here, I can now use the absolute value function. So I won't have to worry about whether or not the X on the left is bigger than the one of the right, and vice versa. Alright, so now what I'm going to do is instead of checking to see if X is equal equal to 7, I'm going to say if the absolute value of X minus .7. So I'm basically going to check and see if the difference between those two is less than, let's say point, let's do 001.
Okay, meaning that they are so close to the same that they really are the same. So in this case, it'll be .69999 something minus .7, so it should be a very, very small difference. And if that's true, then it'll print out they are the same. Alright, let's give this a try, and see if this helps resolve our problem with floats. And it did, so this time I got they are the same. So remember that you can use the equals equals on a lot of, especially integers, you can use it there, and you can use it on characters.
But, be careful when you're trying to use the equals equals and you're trying to compare numbers that were stored as a float. Because they might not be stored exactly as you would expect. But go ahead and use a tolerance to check to see if they're the same.
- Compiling and running C++ programs
- Creating variables in C++
- Choosing the correct data type to represent variables
- Creating assignment expressions
- Changing data types in C++
- Comparing values in C++
- Using logical operators
- Comparing strings in C++
- Defining and calling a function
- Using arrays in C++