Relational operators

Learn about the 3rd category of programming operators. See how relational operators are used to create conditional expressions.

11/09/2023

youtube-thumb
algol-youtube-play

Before learning about relational operators, it is first recommended that you already know what conditional statements are.

If you still don’t know what conditional statements are, you should certainly read this article in which we explain in detail about the subject.

But let’s get down to business!


What are relational operators?

Relational operators represent the 3rd category of programming operators and, in summary, are used to compare values of variables and create conditional statements.

In this sense, the main programming languages use the following relational operators:

>

which represents greater than.

<

which represents less than.

>=

which represents greater than or equal.

<=

which represents less than or equal.

==

which represents equal.

!=

which represents not equal.

These operators are used in order to create expressions of the true or false type (fundamental to conditional statements).

operadores-relacionais

It is important to note that some programming languages may still have a few more operators. For example, the Javascript language, which also has the following operators:

===

which represents strictly equal.

!==

which represents strictly not equal.

Just for example, note in the code below the use of the relational operator < (less than) in a conditional statement:

1.if(age < 18){
2. println("You cannot access the system! He's underage!");
3.}

In line 1 of the code above, we have a conditional expression that checks if the value of the age variable is less than 18. In such a way that, if true, the print command in line 2 will be executed.

But let’s go a little deeper into the subject!


Conditional expressions

First, a conditional expression is the product generated by using relational operators. In such a way that, when you use relational operators, you create expressions that return two possible values: True or False.

The creation of a conditional expression is given by the structure below:

[left member] RELATIONAL OPERATOR [right member];

Just to illustrate, here are some examples of conditional expressions:

salary < 3500.00;

age >= 18;

name != “John”;

value == 10;

All of these expressions above can be interpreted as questions that result in True or False, and that therefore allow the algorithm to make decisions about whether or not to perform any operations during the execution of a program.


Do not confuse == with =

One of the common programming operators is the assignment operator, represented by the = symbol.

In this sense, it is very common for beginning students to confuse the equality operator == with the assignment operator =.

However, they have different purposes and work quite differently. So don’t confuse them!


You have certainly learned the basics of relational operators. But there is still much more to learn.

Relational operators represent the 3rd of the 5 categories of programming operators. So, continue your learning and read our article on logical operators.

autor

David Santiago

Master in Systems and Computing. Graduated in Information Systems. Professor of Programming Language, Algorithms, Data Structures and Development of Digital Games.

Outros artigos