What role do the relational operators play How is relational operator == differs from =?

What Does Operator Mean?

An operator, in computer programing, is a symbol that usually represents an action or process. These symbols were adapted from mathematics and logic. An operator is capable of manipulating a certain value or operand.

Operators are the backbone of any program and they are used for everything from very simple functions like counting to complex algorithms like security encryption.

Techopedia Explains Operator

There are several classifications of operators and each of them can have one or more operands, a specific data that is to be manipulated.

  • Assignment Operator: This refers to the "=" (equals) sign and assigns a variable. A variable is the framework of the information.
  • Arithmetic Operators: These include "+" (addition), "-" (subtraction), "*" (multiplication), "/" (division), "\" (integer division), "Mod" (Modulo) and "^" (exponentiation).
  • Boolean Operators: These make use of "And" (logical conjunction), "AndAlso" (short circuit And), "OrElse" (short circuit Or), "Or" (logical inclusion), "Not" (negation) and "Xor" (logical inclusion). These symbols are also known as logical operators.
  • Relational Operators: These include ">" (greater than), "<" (lesser than), ">=" (greater than or equal to), "<=" (lesser than or equal to), "==" (equal to), "<>" (not equal to), and "Is" (comparing references). These symbols are used to evaluate variables.
  • Bitwise Operators: These are used in manipulating bits of a binary value and are not always used in programming. These include the symbols "Not" (bitwise negation), "Xor" (bitwise exclusive or), "And" (bitwise and), and "Or" (bitwise or).

download the slides used in this presentation
PowerPoint pptx | Acrobat pdf

Objectives

While engaging with this module, you will...

  1. make comparisons between variables
  2. compound comparisons to make several evaluations at once

Equivalency

In C++, you will form logical expressions that evaluate to either true or false. In fact, true and false are reserved words in C++. There is a relationship between these words and the and numerical values. The compiler will interpret 0 as false and false as 0. It will interpret any other number with true, and true maps to 1 (may be different on different platforms). Thus

  • 0 <-> false
  • ≠0 -> true -> 1

As an example, if you were to output true to the screen, you get 1.

cout<<true; // outputs 1

Relational Operators

The relational operators are �<�, �<=�, �>�, �>=�, �==�, �!=�. The first 4 are clear and not much need be said about them. For example...

short val = 5, num = 8, bob = 0;
(val <= num); // evals to true (or 1)
(num % val > bob); // evals to true (3 is greater than 0)

The == operator is the �is equal� operator and �!=� is the �is not equal� operator.

(val == num); // true
(num != (num/val)); // true

I now bring special attention to the == operator. Many times, those learning C++ for the first time will make a mistake when trying to use this operator that the compiler will NOT catch. The code will compile and run, but incorrectly! They will use the = operator instead of the == operator. Thus, val = num will compile and run but will NOT compare the two values. It will set the value of the variable val to that of num and will return true...always. This is hardly the desired result. BE CAREFUL.

Logical Operators:

The logical operators are �&&�, �||�, and �!�. Note: and (&&) has a higher precedence than or (||). You should check the order of precedence of the operators in the appendix in the text. If you are unfamiliar with basic logic, this is how it works: a && b is true only if they are both true; a || b is false only if they are both false. !a is not a; i.e. the �!� operator changes true to false and vice versa.

(val == num) || ( ! val);
/* false, since val is not equal to num (F), val is true (5 same as true), so not true is false, and F || F is false */

Skip to content

Kenneth Leroy Busbee

Overview

A relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. These include numerical equality (e.g., 5 = 5) and inequalities (e.g., 4 ≥ 3).[1]

Discussion

The relational operators are often used to create a test expression that controls program flow. This type of expression is also known as a Boolean expression because they create a Boolean answer or value when evaluated. There are six common relational operators that give a Boolean value by comparing (showing the relationship) between two operands. If the operands are of different data types, implicit promotion occurs to convert the operands to the same data type.

Operator symbols and/or names can vary with different programming languages. Most programming languages use relational operators similar to the following:

Operator Meaning
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equality (equal to)
!= or <> inequality (not equal to)

Examples:

  • 9 < 25
  • 9 < 3
  • 9 > 14
  • 9 <= 17
  • 9 >= 25
  • 9 == 13
  • 9 != 13
  • 9 !< 25
  • 9 <> 25

Note: Be careful. In math you are familiar with using the symbol = to mean equal and ≠ to mean not equal. In many programming languages the ≠ is not used and the = symbol means assignment.

Key Terms

relational operatorAn operator that gives a Boolean value by evaluating the relationship between two operands.

References

  • cnx.org: Programming Fundamentals – A Modular Structured Approach using C++

What is the meaning of == relational operator?

In computer science, a relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. These include numerical equality (e.g., 5 = 5) and inequalities (e.g., 4 ≥ 3).

What is the role of relational operators in C ++? Distinguish between == and?

The “=” is an assignment operator is used to assign the value on the right to the variable on the left. The '==' operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false.

What does the relational operator mean equal to less than greater than not equal to?

Relational operators are binary meaning they require two operands. ... Relational Operators..

What is difference between comparison operator and relational operator?

The relational comparison operators are equal in precedence and are evaluated from left to right. The <, <=, >=, and > operators are numerical comparison operators, while instanceof is a type comparison operator. All of these operators produce boolean values.