Operators & Expressions

Introduction

An operator is a symbol or letter which makes the compiler perform a specific operation on operands (variables) in a program.

An expression is a combination of operands ( i.e, constants, variables, numbers), and operators that perform a specific operation and parenthesis.

An expression is a sequence of operators that specifies a computation. an expression may result in a numerical value.

For example: x+y is an expression in which the sign + is an operator which specifies the addition operation.

Arithmetic operators ( +, -, *, /, % )

Arithmetic Operators are used for various mathemetical calculations. the result of an arithmetic expression is a numerical value. The five arithmetical operations supported by the language are:

Operations of addition, subtraction, multiplication and division should not suppose an understanding challenge for you since they literally correspond with their respective mathematical operators.

The only one that may not be known by you is the module, specified with the percentage sign (%). Module is the operation that gives the remainder of a division of two integer values. For example, if we write a = 11 % 3;, the variable a will contain 2 as the result since 2 is the remainder from dividing 11 between 3.

Relational operators ( ==, !=, >, <, >=, <= )

A relational operator is used to compare two values and the result of this comparision is always logical, either true or false. They are used to determine the relationship between different operands.

Here you have some examples:

of course, instead of using only numberic constants, we can use any valid expression, including variables. Suppose that a=2, b=3 and c=6,

Logic operators ( !, &&, || ).

A Logical operator is used to connect two relational or logical expressions. The result of such an operation is always logical, either true or false. For example:

Conditional operator ( ? ).

C++ provides a conditional operator (? : ) for performaing single conditional operations. It is a ternary operator, it requires three operators. The general form is :

Expression1 ? Expression2 : Expression3

if expression1 is true the expression will return Expression2, if not it will return Expression3

Assignment Operator

The assignation operator serves to assign a value to a variable. a = 5, assigns the integer value 5 to variable a. The part at the left of the = operator is known as lvalue (left value) and the right one as rvalue (right value). lvalue must always be a variable whereas the right side can be either a constant, a variable, the result of an operation or any combination of them.

Compound assignation operators (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

A feature of assignation in C++ that contributes to its fame of sparing language when writing are the compound assignation operators (+=, -=, *= and /= among others), which allow to modify the value of a variable with one of the basic operators:

value += increase; is equivalent to value = value + increase;

a -= 5; is equivalent to a = a - 5;

a /= b; is equivalent to a = a / b;

price *= units + 1; is equivalent to price = price * (units + 1);

and the same for all other operations.

Bitwise Operators ( &, |, ^, ~, <<, >> ).

Bitwise operators modify variables considering the bits that represent the values that they store, that means, their binary representation.

Explicit type casting operators

Type casting operators allows you to convert a datum of a given type to another. There are several ways to do this in C++, the most popular one, compatible with the C language is to precede the expression to be converted by the new type enclosed between parenthesis ():

int i;

float f = 3.14;

i = (int) f;

sizeof()

This operator accepts one parameter, that can be either a variable type or a variable itself and returns the size in bytes of that type or object: a = sizeof (char);

This will return 1 to a because char is a one byte long type.

HOME LEARN C++ PREVIOUS NEXT