Expression and Operators in C++ Expression

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

EXPRESSION AND OPERATORS IN C++

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

- assigns a value to a variable.


- always takes place from right to left, and never the other way around.

Arithmetic operators

- used to perform arithmetic operations on variables and data.

The five arithmetical operations supported by C++ are:

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

- modify the current value of a variable by performing an operation on it.

They are equivalent to assigning the result of an operation to the first operand:

Increment and Decrement (++,--)

- 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.

LEARNING CONTENTS (Relational Operator (==, <=, >=, !, >, <))

- are used in decision making and loops.


- the word relational refers to the relationship values can have with one another.
- Two expressions can be compared using relational and equality operators.
- to know if two values are equal or if one is greater than the other.
- The result of such an operation is either true or false (i.e., a Boolean value).

The relational operators in C++ are:


Be careful! The assignment operator (operator =, with one equal sign) is not the same as the equality
comparison operator (operator ==, with two equal signs); the first one (=) assigns the value on the
right-hand to the variable on its left, while the other (==) compares whether the values on both sides
of the operator are equal.

LEARNING CONTENTS (Logical Operator (!, &&, ||))

- 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.

Here are the possible results of a||b:


This is known as short-circuit evaluation, and works like this for these operators:

The Comma Operator

- (,) 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.

Bitwise operators (&, |, ^, ~, <<, >>)

- modify variables considering the bit patterns that represent the values they store.
Explicit Type casting operator

- allow to convert a value of a given type to another type.


- to precede the expression to be converted by the new type enclosed between parentheses (()).
- Another way to do the same thing in C++ is to use the functional notation preceding the expression to
be converted by the type and enclosing the expression between parentheses.

- 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.

LEARNING CONTENTS (Operator Precedence)

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.

You might also like