Comparison Operators

FIELDS OF STUDY

Software Development; Coding Techniques; Computer Science

ABSTRACT

Comparison operators compare two values in an expression that resolves to a value of true or false. The main comparison operators are equal to, not equal to, greater than, greater than or equal to, less than, and less than or equal to. Comparison operators are used in conditional expressions to determine if one block of code or another executes, thus controlling flow in a computer program. They thereby support complex decision making in computer programs.

PRINCIPAL TERMS




Operators used to compare the value of a variable (x) to a given value are called “comparison operators.” The table provides multiple comparison operators,





Operators used to compare the value of a variable (x) to a given value are called “comparison operators.” The table provides multiple comparison operators, definitions of the operators, and statements using them. True and false examples of values for the variable are provided for each statement.
UNDERSTANDING COMPARISON OPERATORS

In computer programming, comparison operators are used in conditional expressions to determine which block of code executes, thus controlling the program flow. Comparison operators compare two values in an expression that resolves to a value of true or false. In if-then-else statements, comparison operators enable different blocks of code to execute based on whether the result of the comparison is true or false. Boolean operators can be used to combine either-or expressions to create complex Boolean expressions that also resolve to true or false. This allows more complex scenarios to be tested to determine which block of code should execute.

There are six main comparison operators: equal to, not equal to, greater than, greater than or equal to, less than, and less than or equal to. Different programming languages use different syntax to express these operators, but the meanings are the same. Some languages include additional operators, such as the equal value and equal type operator (===) and the not equal value or not equal type operator (!==) in JavaScript.

Comparison operators can compare a variety of data types, including integers and strings. For example, if x is an integer variable, x is less than 5 evaluates to true if x equals 4 or less but evaluates to false if x equals 5 or more. If x and y are string variables, x equals y evaluates to true only if the strings are the same length and are composed of the same characters in the same order. Therefore, “abc” is equal to “abc” but “abc” is not equal to “bac.” For some operations, only data of the same type can be compared. For example, if x is an integer and y is a string, then expression x is greater than y would return an error. Some languages, such as JavaScript and MySQL, use numeric values for the characters in a string to make the comparison.

Arrays are not typically compared using comparison operators. Different languages return different results when comparing arrays for equivalence. For example, the C language returns a pointer to the both array's location in memory and then compares the memory locations for equivalence. Thus, the contents of the arrays, their stored values, are not directly compared.

Similarly, when objects are compared with the “equal to” comparison operator in languages like JavaScript or C#, the comparison operator checks whether the objects are of the same class and does not compare the data stored within the objects. In the language PHP, however, the values are also assessed. It is thus important to be aware of the rules of the programming language in question.

USING COMPARISON OPERATORS




Comparison Operators

They are also used to compare strings. Here another Boolean operator, OR, has been used to check two comparisons, and if either is true, the entire statement returns a value of true and the function is called:




Comparison Operators

Comparison operators are an important development tool because of their support for the if-else conditional logic. With the added power of Boolean logic, complex decision-making structures are possible. For instance, “branching,” or “selection,” allows for various courses of action to be taken (that is, different else-if statements to execute) depending on the condition.

However, using comparison operators incorrectly can lead to errors that are difficult to correct. Programmers must ensure they are aware of how comparison operators behave when used to compare different types of data in the using different computer languages, paying particular attention to how arrays, objects, and strings are handled.

COMPARISON OPERATORS IN PRACTICE

Comparison operators are often used in process codes. In the following example, an error code is returned from a function that updates a customer's name in a database. Comparison operators are then used to determine which error has occurred and then to transfer control to the appropriate block of code. First, the function is called and the value it returns is stored in a variable named code.




Comparison Operators

Next, an if statement is used to execute different blocks of code depending on which value is returned by the function.




Comparison Operators

THE POWER OF COMPARISON

Comparison operators are used in a wide variety of development scenarios. They are used in authentication routines, where exact comparison is crucial. For example, a user's credentials may be checked against a database of authorized users before access to the database is permitted. Comparison operators may also be used for more mundane tasks such as processing discount codes. When a user types in a discount code, it is compared to valid codes, and if the code is valid, the applicable discount is applied.

—Maura Valentino, MSLIS

Friedman, Daniel P., and Mitchell Wand. Essentials of Programming Languages. 3rd ed., MIT P, 2008.

Haverbeke, Marijn. Eloquent JavaScript: A Modern Introduction to Programming. 2nd ed., No Starch Press, 2014.

MacLennan, Bruce J. Principles of Programming Languages: Design, Evaluation, and Implementation. 3rd ed., Oxford UP, 1999.

Nixon, Robin. Learning PHP, MySQL, JavaScript, CSS & HTML5: A Step-by-Step Guide to Creating Dynamic Websites. 3rd ed., O'Reilly, 2014.

Scott, Michael L. Programming Language Pragmatics. 4th ed., Morgan Kaufmann Publishers, 2016.

Van Roy, Peter, and Seif Haridi. Concepts, Techniques, and Models of Computer Programming. MIT P, 2004.