Expressions and Assignment Statements
4th chunk
Outline:
- Relational and Boolean expressions are then discussed.
In addition to arithmetic expressions, programming languages support relational and Boolean expressions
Relational Expressions
Properties:
- A relational operator is an operator that compares the values of its two operands.
- A relational expression has two operands and one relational operator
- The value of a relational expression is Boolean, except when Boolean is not a type included in the language.
- The relational operators are often overloaded for a variety of types
- The types of the operands that can be used for relational operators are numeric types, strings, and enumeration types
- The relational operators always have lower precedence than the arithmetic operators
Example: a + 1 > 2 * b the arithmetic expressions are evaluated first.
The syntax of the relational operators for equality and inequality differs among some programming languages
- Example: for inequality, the C-based languages use !=, Fortran 95+ uses .NE. or <>, and ML and F# use <>.
Boolean Expressions
Properties
Consist of
- Boolean variables,
- Boolean constants,
- Relational expressions
- Boolean operators
Operators
- AND
- OR
- NOT
- Sometimes XOR and EQUIVALENCE
As a result, produce Boolean values. In the meantime, the C-based languages assign a higher precedence to AND than OR (however, in the mathematics of Boolean algebras, the OR and AND operators must have equal precedence).
- Explanation: Perhaps this resulted from the baseless correlation of multiplication with AND and of addition with OR, which would naturally assign higher precedence to AND.
The precedence of the arithmetic, relational, and Boolean operators in the C-based languages is as follows:
One odd result of C’s design of relational expressions is that the following expression is legal:
a > b > c
Explanation: The leftmost relational operator is evaluated first because the relational operators of C are left associative, producing either 0 or 1. Then, this result is compared with the variable c. There is never a comparison between b and c in this expression.
Some languages, including Perl and Ruby, provide two sets of the binary logic operators, && and and for AND and || and or for OR
Difference: spelled version (AND, OR) has lower precedence.
Readability dictates that a language should include a Boolean type.
Explanation: Some error detection is lost in the use of numeric types for Boolean operands, because any numeric expression, whether intended or not, is a legal operand to a Boolean operator. In the other imperative languages, any non-Boolean expression used as an operand of a Boolean operator is detected as an error
REFERENCES:
- Sebesta, R. W. (n.d.). Expressions and Assignment Statements. In Concepts of Programming Languages (12th ed., pp. 316–318).