Relational (comparison) operators in C ++:>, <,> =, <=, == and !=

In this tutorial of our C++ Course, we will learn what they are and how to use relational operators, also known as comparison operators, which are:
  • >    greater than
  • <    less than
  • >=  greater than or equal
  • <=  less than or equal
  • ==  equal
  • !=  different

How to compare things in C++

Although this tutorial is a bit boring and theoretical, it is necessary to better understand the use of the IF and ELSE conditional tests, which we will use in the next tutorials.

What we are going to do now is teach you how to perform the comparison operation, it is done like this:
  • operand1 operator operand1

That is, we will use two operands (data, such as numbers or characters) and one operator (in this case, a relational).

And this operation always returns one of the values:
  • TRUE (true or 1)
  • FALSE (false or 0)

Ever.
Relation operation is always a question that the answer is 1 or 0, true or false.
OK?

Equality operator in C++: ==

Well, let's go.
If we want to know if an x value is equal to a y, we do:
x == y

You see, it's: x == y
And no: x = y

Let's test if 1 equals 1 and then if 2, run the code below:
#include <iostream>
using namespace std;

int main()
{
    cout << (1 == 1) << endl;
    cout << (1 == 2) << endl;

    return 0;
}
The result was:
C++ free online course and tutorials
Actually, 1 equals 1 (1, TRUE, the result of the comparison).
And 1 is not equal to 2 (0, FALSE, the result of the comparison).

Not equal operator in C++: !=

Just as you have one operator that checks for equality, so has another that checks for non-equality.
If two values are different, the comparison operation with != will result in TRUE.

If they are equal, it results in FALSE.

Let's see in practice, compile and run:
#include <iostream>
using namespace std;

int main()
{
    cout << ('a' != 'b') << endl;

    return 0;
}
That is, the character 'a' is different from 'b'.
Now test if 'a' is different from 'A'.
IT'S ?

Greater than operator C++: >

If we want to test if an X value is greater than a Y value, we use the operator: >
X> Y

If X is larger, it returns TRUE (1).
If equal or less, it returns FALSE (0).

Greater than or equal operator in C++: >=

If we want to test if a value is greater than or equal, we use: >=
X >= Y

It returns 1 (true) if X is greater than or equal to Y, and 0 if X is smaller.

For example, to test if a person can already drive, drink, enter the motel ... we should do:
age >= 21

Lessa than and less than or egual to operators C++: < and <=

Similarly, there are the smaller and smaller operators equal.

To test if a person can donate blood, we must test:
age < 65

To find out who is exempt from tax returns, we should test:
value <= 28559.70

Beware of comparisons

A very serious mistake is to confuse:
x = y;

With:
x == y;


In the first case, it is an assignment.
Variable x is receiving the value of variable y.

In the second case is a comparison.
Always read comparison as a question: x is equal to y?

And when you ask C ++ things, it only gives you two answers: 1 or 0
So if you want to store the value of a comparison, declare a boolean, ok?

  • C++ exercise

Make a program that prompts the user for two values (can be a number or a character), then display all comparison operations, as in the example below:

C++ tutorial

Did you try?
Worked?

Our code looks like this:
#include <iostream>
using namespace std;

int main()
{
    int op1, op2;

    cout << "First value  : ";
    cin >> op1;

    cout << "Second value : ";
    cin >> op2;

    cout << op1 << " >  " << op2 <<" : "<<(op1>op2)<<endl;
    cout << op1 << " <  " << op2 <<" : "<<(op1<op2)<<endl;
    cout << op1 << " >= " << op2 <<" : "<<(op1>=op2)<<endl;
    cout << op1 << " <= " << op2 <<" : "<<(op1<=op2)<<endl;
    cout << op1 << " == " << op2 <<" : "<<(op1==op2)<<endl;
    cout << op1 << " != " << op2 <<" : "<<(op1!=op2)<<endl;
    return 0;
}
And yours?

No comments:

Post a Comment