Relational OperatorsA relational operator is used to check the relationship between two operands. Show
Logical OperatorsLogical operators connect two or more relational expressions into one or reverse the logic of an expression. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0.
NOTE:
Practice Problems(1) Assuming x is 5, y is 6, and z is 8, indicate whether each of the following relational expressions is true or false: A) x == 5 (2) Assume the variables a = 2, b = 4, and c = 6. Determine whether each of the following conditions is True or False: A) a == 4 || b > 2 (3) Given that the value of the string variable aValue is "68", evaluate the following C++ expression and tell the value (true/false). "Comparison (computer programming)" redirects here. For comparison of files, see File comparison. 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). In programming languages that include a distinct boolean data type in their type system, like Pascal, Ada, or Java, these operators usually evaluate to true or false, depending on if the conditional relationship between the two operands holds or not. In languages such as C, relational operators return the integers 0 or 1, where 0 stands for false and any non-zero value stands for true. An expression created using a relational operator forms what is termed a relational expression or a condition. Relational operators can be seen as special cases of logical predicates. Equality[edit]Usage[edit]Equality is used in many programming language constructs and data types. It is used to test if an element already exists in a set, or to access to a value through a key. It is used in switch statements to dispatch the control flow to the correct branch, and during the unification process in logic programming. One possible meaning of equality is that "if a equals b, then either a or b can be used interchangeably in any context without noticing any difference." But this statement does not necessarily hold, particularly when taking into account mutability together with content equality. Location equality vs. content equality[edit]Sometimes, particularly in object-oriented programming, the comparison raises questions of data types and inheritance, equality, and identity. It is often necessary to distinguish between:
In many modern programming languages, objects and data structures are accessed through references. In such languages, there becomes a need to test for two different types of equality:
The first type of equality usually implies the second (except for things like not a number (NaN) which are unequal to themselves), but the converse is not necessarily true. For example, two string objects may be distinct objects (unequal in the first sense) but contain the same sequence of characters (equal in the second sense). See identity for more of this issue. Real numbers, including many simple fractions, cannot be represented exactly in floating-point arithmetic, and it may be necessary to test for equality within a given tolerance. Such tolerance, however, can easily break desired properties such as transitivity, whereas reflexivity breaks too: the IEEE floating-point standard requires that NaN ≠ NaN holds. In contrast, the (2022) private standard for posit arithmetic (posit proponents mean to replace IEEE floats) has a similar concept, NaR (Not a Real), where NaR = NaR holds.[1] Other programming elements such as computable functions, may either have no sense of equality, or an equality that is uncomputable. For these reasons, some languages define an explicit notion of "comparable", in the form of a base class, an interface, a trait or a protocol, which is used either explicitly, by declaration in source code, or implicitly, via the structure of the type involved. Comparing values of different types[edit]In JavaScript,
PHP, VBScript and a few other dynamically typed languages, the standard equality operator evaluates to true if two values are equal, even if they have different types, making the number 4 compare equal to the text string "4", for
instance. A typed equality operator is often available also, in such languages, returning true only for values with identical or equivalent types (in PHP, Ordering[edit]Greater than and less than comparison of non-numeric data is performed according to a sort convention (such as, for text strings, lexicographical order) which may be built into the programming language and/or configurable by a programmer. When it is desired to associate a numeric value with the result of a comparison between two data items, say a and b, the usual convention is to assign −1 if a < b, 0 if a = b and
1 if a > b. For example, the C function Comparison of programmer-defined data types (data types for which the programming language has no in-built understanding) may be carried out by custom-written or
library functions (such as Logical equivalence[edit]Though perhaps unobvious at first, like the boolean logical operators XOR, AND, OR, and NOT, relational operators can be designed to have logical equivalence, such that they can all be defined in terms of one another. The following four conditional statements all have the same logical equivalence E (either all true or all false) for any given x and y values: This relies on the domain being well ordered. Standard relational operators[edit]The most common numerical relational operators used in programming languages are shown below. Standard SQL, uses the same operators as BASIC, while many databases allow
Other conventions are less common: Common
Lisp and Macsyma/Maxima use Basic-like operators except for inequality, which is Syntax[edit]Relational operators are also used in technical literature instead of words. Relational operators are usually written in infix notation, if supported by the programming language, which means that they appear between their operands (the two expressions being related). For example, an expression in Python will print the message if the x is less than y: if x < y: print("x is less than y in this example") Other programming languages, such as Lisp, use prefix notation, as follows: Operator chaining[edit]In mathematics, it is common practice to chain relational operators, such as in 3 < x < y < 20 (meaning 3 < x and x < y and y < 20). The syntax is clear since these relational operators in mathematics are transitive. However, many recent programming languages would see an expression like 3 < x < y as consisting of two left (or right-) associative operators, interpreting it as something like It is possible to give the expression Some languages, like Common Lisp, use multiple argument predicates for this. In Lisp Confusion with assignment operators[edit]Early FORTRAN (1956–57) was bounded by heavily restricted character sets where International Algebraic Language (IAL, ALGOL 58) and ALGOL (1958 and 1960) thus introduced B and C[edit]This uniform de facto standard among most programming languages was eventually changed, indirectly, by a minimalist compiled language named B. Its sole intended application was as a vehicle for a first port of (a then very primitive) Unix, but it also evolved into the very influential C language. B started off as a syntactically changed variant of the systems programming language BCPL, a
simplified (and typeless) version of CPL. In what has been described as a "strip-down" process, the As a small type system was later introduced, B then became C. The popularity of this language along with its association with Unix, led to Java, C#, and many other languages following suit, syntactically, despite this needless conflict with the mathematical meaning of the equal sign. Languages[edit]Assignments in C have a value and since any
non-zero scalar value is interpreted as true in conditional expressions,[9] the code int x = 1; int y = 2; if (x = y) { /* This code will always execute if y is anything but 0*/ printf("x is %d and y is %d\n", x, y); } Though
Java and C# have the same operators as C, this mistake usually causes a compile error in these languages instead, because the if-condition must be of type In ALGOL-like languages such as Pascal, Delphi, and Ada (in the sense that they allow nested function definitions), and in
Python, and many functional languages, among others, assignment operators cannot appear in an expression (including Similarly, some languages, such as
BASIC use just the Some programmers get in the habit of writing comparisons against a constant in the reverse of the usual order: if (2 == a) { /* Mistaken use of = versus == would be a compile-time error */ } If This table lists the different mechanisms to test for these two types of equality in various languages:
Ruby uses See also[edit]
Notes and references[edit]
Which operators are used to show the relationship between operands?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).
Which operator is used to know the relationship between two values?(b) Relational operators compare the relationship between two values or arguments.
What are the operators used in relational operator?The result of the comparison, either true ( 1 ) or false ( 0 ), can be used to make a decision regarding program flow (see the IF statement). Table 1 lists the relational operators.
...
Relational Operators.. What are the 3 relational operators?< : less than. <= : less than or equal to. > : greater than.
|