Software Development; Coding Techniques; Computer Science
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.
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.
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.
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 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 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.
Next, an if statement is used to execute different blocks of code depending on which value is returned by the function.
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.
MacLennan, Bruce J. Principles of Programming Languages: Design, Evaluation, and Implementation. 3rd ed., Oxford UP, 1999.
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.