### C++ Language

***** Before deep dive into the C++ Operators, let's revise all the C++ tokens for better understanding.

## C++ Tokens

In C++, a token is the smallest individual unit in a program that has a specific meaning to the compiler. C++ programs are composed of various types of tokens. The basic categories of tokens in C++ include:

• __Keywords__: These are reserved words that have special meanings in C++. Examples include *'int'*, *'double'*, *'if'*, *'else'*, *'while'*, and so on.

• __Identifiers__: These are names given to various program elements such as variables, functions, arrays, etc. Identifiers must follow certain rules, such as starting with a letter, being composed of letters, digits, and underscores.

• __Literals__: Represent constant values used in the program. Examples include integer literals (*42*), floating-point literals (3.14), character literals (*'A'*), and string literals ("Hello").

• __Operators__: Symbols that perform operations on one or more operands. Examples include *'+', '-', '*', '/', '==', '!=', '&&',* etc.

• __Punctuators__: These are characters that have special meanings in C++, such as braces *'{}'*, parentheses *'()'*, commas *','*, and semicolons *';'*.

• __Comments__: Used for adding remarks or explanations in the code. C++ supports both single-line comments (*'//'*) and multi-line comments (*'/* */'*).

• __Preprocessor Directives__: Lines in the code that start with *'#'* and are processed by the preprocessor before compilation. Examples include *'#include', '#define'*, etc.

• __Whitespace__: Spaces, tabs, and newline characters that separate tokens but are otherwise ignored by the compiler.

## C++ Operators

C++ operators are symbols or keywords used to perform operations on variables and values. They are the building blocks of expressions and statements in C++. Here are some of the key categories of operators:

### Arithmetic Operators:

These operators are used to perform basic arithmetic operations.

Operators | Meanings | Examples | Results |
---|---|---|---|

+ | Addition: Adds two operands. | 5+2 | 7 |

- | Subtraction: Subtracts the right operand from the left operand. | 5-2 | 3 |

* | Multiplication: Multiplies two operands. | 5*2 | 10 |

/ | Division: Divides the left operand by the right operand. | 5/2 | 2 |

% | Modulus: Computes the remainder of the division of the left operand by the right operand. | 5%2 | 1 |

#### Syntax:

int a = 5, b = 2; int sum = a + b; // sum = 7 int difference = a - b; // difference = 3 int product = a * b; // product = 10 int quotient = a / b; // quotient = 2 int remainder = a % b; // remainder = 1

### Relational Operators:

These operators are used for comparing two values.

Operators | Meanings | Examples | Results |
---|---|---|---|

== | Equal to: Checks if two operands are equal. | 5==2 | false |

!= | Not equal to: Checks if two operands are not equal. | 5!=2 | true |

> | Greater than: Checks if the left operand is greater than the right operand. | 5>2 | true |

< | Less than: Checks if the left operand is less than the right operand. | 5<2 | false |

>= | Greater than or equal to: Checks if the left operand is greater than or equal to the right operand. | 5>=2 | true |

<= | Less than or equal to: Checks if the left operand is less than or equal to the right operand. | 5<=2 | false |

#### Syntax:

int x = 5, y = 10; bool isEqual = (x == y); // false bool isNotEqual = (x != y); // true bool isLessThan = (x < y); // true bool isGreaterThan = (x > y); // false bool isLessThanOrEqual = (x <= y); // true bool isGreaterThanOrEqual = (x >= y); // false

### Logical Operators:

These operators are used to perform logical operations.

Operators | Meanings | Examples | Results |
---|---|---|---|

&& | Logical AND: Returns true if both operands are true. | (5<2)&&(5>3) | false |

|| | Logical OR: Returns true if at least one operand is true. | (5<2)||(5>3) | true |

! | Logical NOT: Returns true if the operand is false, and false if the operand is true. | !(5>2) | true |

#### Syntax:

bool condition1 = true, condition2 = false; bool resultAnd = (condition1 && condition2); // false bool resultOr = (condition1 || condition2); // true bool resultNot = !condition1; // false

### Assignment Operators:

These operators are used to perform logical operations.

i = 5;

Operators | Meanings | Examples | Results |
---|---|---|---|

= | Assignment: Assigns the value of the right operand to the left operand. | j = 2; i = j; |
Now 'i' value is '2' |

+= | Add and assign: Adds the right operand to the left operand and assigns the result to the left operand. | i+=10 | 15 |

-= | Subtract and assign): Subtracts the right operand from the left operand and assigns the result to the left operand. | i-=10 | -5 |

*= | Multiply and assign: Multiplies the left operand by the right operand and assigns the result to the left operand. | i*=10 | 50 |

/= | Divide and assign: Divides the left operand by the right operand and assigns the result to the left operand. | i/=10 | 0 |

%= | Modulus and assign: Computes the modulus of the left operand with the right operand and assigns the result to the left operand. | i%=10 | 5 |

#### Syntax:

int num = 10; num += 5; // num is now 15 (num = num + 5) num -= 3; // num is now 12 (num = num - 3) num *= 2; // num is now 24 (num = num * 2) num /= 4; // num is now 6 (num = num / 4) num %= 2; // num is now 0 (num = num % 2)

### Increment and Decrement Operators:

These operators are used to increment (++) or decrement (--) a variable by 1. For example, **++** is used to increase, and **--** is used to reduce the value of an integer or char variable.

#### Syntax:

int count = 5; count++; // Increment count by 1 (count is now 6) count--; // Decrement count by 1 (count is now 5 again)

### Bitwise Operators:

These operators perform bitwise operations on integer types, and Bitwise operators are used for low-level operations, such as bit manipulation.

Operators | Meanings |
---|---|

<< | Left shift: Shifts the bits to left. |

>> | Right shift: Shifts the bits to right. |

~ | Bitwise NOT: Bitwise inversion (one's complement). |

& | Bitwise AND: Bitwise logical and. |

| | Bitwise NOT: Bitwise logical or. |

^ | Bitwise XOR: Bitwise logical exclusive or. |

#### Syntax:

int a = 5, b = 3; int bitwiseAnd = a & b; // 1 (binary: 0101 & 0011 = 0001) int bitwiseOr = a | b; // 7 (binary: 0101 | 0011 = 0111) int bitwiseXor = a ^ b; // 6 (binary: 0101 ^ 0011 = 0110) int bitwiseNot = ~a; // -6 (bitwise NOT of 5 is -6 in two's complement)

***** Let's show an example of bitwise operators in C language.

### Conditional (Ternary) Operator:

Conditional operator is used to check a condition and select a value depending on the condition. Normally the selected value will be assigned to a variable which ha the following form.

#### Syntax:

int x = 5, y = 10; int max = (x > y) ? x : y; // max is assigned the larger of x and y

### Comma Operator:

The comma operator in C++ is a binary operator that evaluates two expressions and returns the result of the second expression. It is often used in contexts where multiple expressions are allowed, but only a single expression is expected. The syntax for the comma operator is simply a comma (*','*).

#### Syntax:

int expr1, expr2;

**Note:** Here, *'expr1'* is evaluated first, followed by *'expr2'*. The result of the entire expression is the result of *'expr2'*. The value of *'expr1'* is essentially discarded.

#### What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.