Relational operators are a fundamental part of programming logic, as they allow programmers to make comparisons between variables, constants, and expressions. These comparisons are essential for building conditional and repeating structures, which are the cornerstones of programming logic. In this chapter, we'll explore relational operators from basic to advanced.

There are six basic relational operators that are common in most programming languages: equal (==), not equal (!=), greater than (>), less than (<), greater than or equal (>=), and less than or equals (<=). These operators are used to compare two values ​​and return a boolean value (true or false) depending on the result of the comparison.

The equality operator (==) compares whether two values ​​are equal. For example, the expression (5 == 5) will return true, while the expression (5 == 4) will return false. It is important to note that the equality operator is different from the assignment operator (=), which is used to assign a value to a variable.

The inequality operator (!=) compares whether two values ​​are different. For example, the expression (5 != 4) will return true, while the expression (5 != 5) will return false.

The greater than operator (>) and less than operator (<) compare whether one value is greater or less than the other, respectively. For example, the expression (5 > 4) will return true, while the expression (4 > 5) will return false. Likewise, the expression (4 < 5) will return true, while the expression (5 < 4) will return false.

The greater than or equal to operator (>=) and the less than or equal to operator (<=) work similarly to the greater than and less than operators, but they also return true if the two compared values ​​are equal. For example, the expression (5 >= 5) will return true, while the expression (4 >= 5) will return false. Likewise, the expression (5 <= 5) will return true, while the expression (5 <= 4) will return false.

Relational operators are often used in combination with logical operators (such as AND, OR, and NOT) to create more complex expressions. For example, the expression ((5 > 4) AND (3 < 4)) will return true, because both conditions are true. Likewise, the expression ((5 > 4) OR (5 < 4)) will also return true, because at least one of the conditions is true. The NOT expression (5 > 4) will return false, because the condition (5 > 4) is true.

Relational operators are also essential for building conditional structures (such as IF, ELSE, and SWITCH) and repeating structures (such as FOR, WHILE, and DO WHILE). These frameworks allow programmers to control the flow of program execution by causing certain blocks of code to execute only when certain conditions are met.

For example, the following IF structure in pseudocode:

IF (5 > 4) THEN
  PRINT "5 is greater than 4"
ENDIF

will print "5 is greater than 4" because the condition (5 > 4) is true. Likewise, the following WHILE structure in pseudocode:

WHILE (i < 5) DO
  PRINT i
  i = i + 1
ENDWHILE

will print the numbers 0 to 4, because the code block inside the WHILE will be repeated as long as the condition (i < 5) is true.

In short, relational operators are a powerful tool in logic programming, allowing programmers to make comparisons between values ​​and control the flow of program execution. They are the basis for building conditional and repeating structures, which are essential for creating complex and functional programs.

Now answer the exercise about the content:

What are the six basic relational operators common in most programming languages ​​and what is their function?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Flow control structures: sequence 10

Next page of the Free Ebook:

Flow control structures: sequence

Estimated reading time: 2 minutes

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.

+ 9 million
students

Free and Valid
Certificate

60 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video and ebooks