2 Operators

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

OPERATORS

AIRTHMETIC OPERATORS

Airthmetic operators are used to perform arithmetic operations which include

+, - , * , / , %, ++, --

The precedence of operators determines the order in which operators are evaluated within an
expression. Operator precedence refers to the rules that determine the order in which
operators are evaluated in an expression. When an expression contains multiple operators, the
precedence rules help dictate which operators are evaluated first and which are evaluated
later. This ensures that expressions are evaluated in a logical and consistent manner.

Here's a summary of the operator precedence in C++, from highest to lowest:

 Parentheses ((), [ ], { }): Expressions within parentheses are evaluated first. Nested
parentheses are evaluated from the innermost to the outermost.
 Unary Operators (+, -, ++, --, !, ~, * [pointer dereference], & [address-of]): Unary
operators are applied to a single operand. They have higher precedence than binary
operators.
 Multiplicative Operators (*, /, %): These operators perform multiplication, division, and
modulo operations. They are evaluated from left to right.
 Additive Operators (+, -): These operators perform addition and subtraction. They are
evaluated from left to right.

ASSIGMENT STATEMENT

A statement that assigns a value to a variable is known as assignment statement. The


assignment operator = is used in assignment statement to assign a value or computational
result to a variable. The name of variable is on left side and value to assign on right side.

A= B+C x=y=100

+= , -=, etc

Lvalue and Rvalue

Operand to the left side of = are lvalue and operand to the right side of = is rvalue
X=5; is correct 5=x is incorrect

INCREMENT OPERATOR

It is a unary operator used to increase the value of variable by 1.Increment operator cannot
increment the value of constant and expressions.

a++ , x++ is valid but 10++ is invalid

(a+b)++ is also not valid

DECREMENT OPERATOR

Prefix and postfix operators are used in programming languages, including C++, to modify the
value of variables or expressions. They differ in the timing of the operation and the value that is
returned. Here's a comparison of prefix and postfix operators:

SIZE OF OPERATOR

The sizeof operator in C++ is used to determine the size, in bytes, of a data type or an
expression. It provides information about the memory space occupied by a data type or object
in memory. The syntax of the sizeof operator is as follows:

sizeof(operand)

sizeof(int);

LOGICAL OPERATORS

Logical operators in programming, including C++, are used to perform logical operations on
Boolean values (`true` or `false`). They allow you to combine or manipulate Boolean values to
make decisions, control program flow, and create complex conditions. C++ provides three main
logical operators: AND (`&&`), OR (`||`), and NOT (`!`). Here's an overview of each logical
operator:

1. **Logical AND (`&&`):**

- The logical AND operator (`&&`) returns `true` if both of its operands are `true`, otherwise, it
returns `false`.

- It evaluates the second operand only if the first operand is `true`. If the first operand is
`false`, the second operand is not evaluated, as the result of the expression is already known to
be `false`.
- Example: `if (x > 0 && y < 10) { /* code */ }`

2. **Logical OR (`||`):**

- The logical OR operator (`||`) returns `true` if at least one of its operands is `true`,
otherwise, it returns `false`.

- It evaluates the second operand only if the first operand is `false`. If the first operand is
`true`, the second operand is not evaluated, as the result of the expression is already known to
be `true`.

- Example: `if (isRainy || isSnowy) { /* code */ }`

3. **Logical NOT (`!`):**

- The logical NOT operator (`!`) reverses the logical value of its operand. If the operand is
`true`, `!` turns it into `false`, and if the operand is `false`, `!` turns it into `true`.

- Example: `if (!isSunny) { /* code */ }`

Logical operators are frequently used in conditional statements (`if`, `while`, `for`, etc.) and in
creating more complex conditions. They help you build decision-making structures and control
the flow of your program based on different combinations of conditions.

RELATIONAL OPERATORS

Relational operators in programming, including C++, are used to compare two values and
determine the relationship between them. They return a Boolean value (`true` or `false`) based
on the comparison result. Relational operators are fundamental for making decisions and
controlling program flow based on conditions. C++ provides the following relational operators:

1. **Equal to (`==`):**

- The equal to operator (`==`) compares two values to check if they are equal.

- Returns `true` if the values are equal, and `false` otherwise.

- Example: `if (x == y) { /* code */ }`

2. **Not equal to (`!=`):**

- The not equal to operator (`!=`) compares two values to check if they are not equal.
- Returns `true` if the values are not equal, and `false` if they are equal.

- Example: `if (x != y) { /* code */ }`

3. **Greater than (`>`):**

- The greater than operator (`>`) compares two values to check if the left value is greater than
the right value.

- Returns `true` if the left value is greater, and `false` otherwise.

- Example: `if (x > y) { /* code */ }`

4. **Less than (`<`):**

- The less than operator (`<`) compares two values to check if the left value is less than the
right value.

- Returns `true` if the left value is less, and `false` otherwise.

- Example: `if (x < y) { /* code */ }`

5. **Greater than or equal to (`>=`):**

- The greater than or equal to operator (`>=`) compares two values to check if the left value is
greater than or equal to the right value.

- Returns `true` if the left value is greater than or equal, and `false` otherwise.

- Example: `if (x >= y) { /* code */ }`

6. **Less than or equal to (`<=`):**

- The less than or equal to operator (`<=`) compares two values to check if the left value is less
than or equal to the right value.

- Returns `true` if the left value is less than or equal, and `false` otherwise.

- Example: `if (x <= y) { /* code */ }`

Relational operators are used extensively in conditional statements, loops, and other situations
where you need to compare values and make decisions based on their relationships.

BITWISE OPERATOR
It works on bits and perform bit by bit operations. Bitwise operators in programming, including
C++, are used to perform operations on individual bits of integers or other binary data. These
operators work at the binary level, manipulating the individual bits of numbers. Bitwise
operations are often used for low-level tasks like optimizing code, manipulating hardware
registers, and performing tasks related to data compression and encryption.

C++ provides several bitwise operators:

Bitwise AND (&):

Performs a bitwise AND operation between the corresponding bits of two operands. If both bits
are 1, the result is 1. Otherwise, the result is 0.

A = 0011 0011 B= 0001 0001

A&B= 0011 0011


0001 0001
0001 0001

Bitwise OR (|):

Performs a bitwise OR operation between the corresponding bits of two operands. If either bit
is 1, the result is 1. If both bits are 0, the result is 0.

A|B= 0011 0011


0001 0001
0011 0011

Bitwise XOR (^):

Performs a bitwise exclusive OR (XOR) operation between the corresponding bits of two
operands.If the bits are different (one is 0 and the other is 1), the result is 1. If the bits are the
same, the result is 0.

A^B= 0011 0011


0001 0001
0010 0010

Bitwise NOT (~):

Performs a bitwise NOT operation on a single operand.It inverts each bit of the operand. If the
bit is 0, it becomes 1, and if the bit is 1, it becomes 0.
A= 0011 0011 ~A = 1100 1100

Left Shift (<<):

Shifts the bits of the left operand to the left by a specified number of positions. The vacated
positions are filled with zeros.
A= 0011 0011
A<<2

0011 0011 = 11 0011 00 = 1100 1100

Right Shift (>>):

Shifts the bits of the left operand to the right by a specified number of positions. Depending on
the data type, the vacated positions might be filled with zeros or the sign bit.
A= 0011 0011
A>>2

0011 0011 = 00 0011 00 = 0000 1100

You might also like