6.2. Relational Operators: Inequality

Página 24

Relational operators are a fundamental part of programming logic. They are used to compare two values ​​and return a boolean (true or false) value based on the result of that comparison. In this chapter, we will focus on one specific relational operator: the inequality operator.

The inequality operator is represented by the symbols "!=". This operator is used to check if two values ​​are different. If the values ​​are different, the expression returns true. If the values ​​are equal, the expression returns false. For example, the expression "5 != 3" returns true, because 5 and 3 are different values. On the other hand, the expression "2 != 2" returns false, because the two values ​​are equal.

The inequality operator can be used with different data types, including numbers, strings, and Booleans. For example, you can use the inequality operator to compare two strings and see if they are different. The expression '"Hello" != "World"' returns true, because the two strings are different. Likewise, the expression '"true" != "false"' returns true, because the two strings are different.

The inequality operator can also be used to compare two Boolean values. For example, the expression 'true != false' returns true, because the two boolean values ​​are different. Likewise, the expression 'true != true' returns false, because the two boolean values ​​are equal.

It is important to note that the inequality operator is a binary operator, which means that it requires two operands. One operand is the value to the left of the operator, and the other is the value to the right of the operator. For example, in the expression '5 != 3', 5 is the left operand and 3 is the right operand.

Inequality operators are often used in conditional statements, which are a fundamental part of programming logic. A conditional statement is a statement that performs a specific action based on the result of a condition. For example, you can use an inequality operator in a conditional statement to perform a specific action if two values ​​are different.

For example, consider the following code:

if (age != 18) {
  console.log("You are not 18 years old.");
} else {
  console.log("You are 18 years old.");
}

In this code, the conditional statement checks if the 'age' variable is different from 18. If the 'age' variable is different from 18, the code displays the message "You are not 18 years old.". If the 'age' variable equals 18, the code displays the message "You are 18 years old.".

In summary, the inequality operator is a powerful tool in logic programming. It allows you to compare two values ​​and take specific actions based on the result of that comparison. By mastering the use of the inequality operator, you can create more complex and powerful programs.

Now answer the exercise about the content:

What does the inequality operator do in programming logic?

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

You missed! Try again.

Next page of the Free Ebook:

256.3. Relational Operators: Greater than

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text