Expression and Operators in C++ Expression
Expression and Operators in C++ Expression
Expression and Operators in C++ Expression
Expression
- a combination of literals, variables, operators, and explicit function calls (not shown above) that produce
a single output value.
- When an expression is executed, each of the terms in the expression is evaluated until a single value
remains (this process is called evaluation).
Operation
- a mathematical calculation involving zero or more input values (called operands) that produces a new
value (called an output value).
- The specific operation to be performed is denoted by a construct (typically a symbol or pair of symbols)
called operator.
Assignment operator
Arithmetic operators
Division operator
- Note the operation (a / b) in our program. The / operator is the division operator.
- if an integer is divided by another integer, we will get the quotient.
- if either divisor or dividend is a floating-point number, we will get the result in decimals.
Modulo operator
- The modulo operator % computes the remainder. When a = 9 is divided by b = 4, the remainder is 1.
- The % operator can only be used with integers.
Compound operator
They are equivalent to assigning the result of an operation to the first operand:
- Some expression can be shortened even more: the increase operator (++) and the decrease
operator (--) increase or reduce by one the value stored in a variable.
- The operation ++ adds 1 to its operand, and -- subtracts 1.
A peculiarity of this operator is that it can be used both as a prefix and as a suffix. That means that it
can be written either before the variable name (++x) or after it (x++).
In the case that the increase operator is used as a prefix (++x) of the value, the expression evaluates
to the final value of x, once it is already increased. On the other hand, in case that it is used as a suffix
(x++), the value is also increased, but the expression evaluates to the value that x had before being
increased.
- The word logical refers to the ways these relationships can be connected together using the
rules of formal logic.
- The key to the concept or relational and logical operators is the idea of true or false.
- The operator ! is the C++ operator for the Boolean operation NOT. It has only one operand, to
its right, and inverts it, producing false if its operand is true, and true if its operand is false.
- The logical operators && and || are used when evaluating two expressions to obtain a single
relational result.
- The operator && corresponds to the Boolean logical operation AND, which yields true if both
its operands are true, and false otherwise.
The following panel shows the result of operator && evaluating the expression a&&b:
The operator || corresponds to the Boolean logical operation OR, which yields true if either of its
operands is true, thus being false only when both operands are false.
- (,) is used to separate two or more expressions that are included where only one expression is
expected.
- When the set of expressions has to be evaluated for a value, only the right-most expression is
considered.
- modify variables considering the bit patterns that represent the values they store.
Explicit Type casting operator
- This operator accepts one parameter, which can be either a type or a variable, and returns the size in
bytes of that type or object.
From greatest to smallest priority, C++ operators are evaluated in the following order:
When an expression has two operators with the same precedence level, grouping determines which one is
evaluated first: either left-to-right or right-to-left.
Enclosing all sub-statements in parentheses (even those unnecessary because of their precedence) improves
code readability.