Operators
In C++, operators are symbols used to perform various operations on variables and values. They allow us to perform mathematical calculations, compare values, assign values, and more.
C++ provides a wide range of operators, including:
- Arithmetic operators, such as
+
,-
,*
,/
, and%
- Assignment operators, such as
=
,+=
,-=
,*=
,/=
, and%=
- Comparison operators, such as
==
,!=
,>
,<
,>=
, and<=
- Logical operators, such as
&&
(AND),||
(OR), and!
(NOT) - Increment and decrement operators, such as
++
and--
- Bitwise operators, such as
&
(AND),|
(OR),^
(XOR),~
(NOT),<<
(left shift), and>>
(right shift)
Here's an example that demonstrates the usage of arithmetic operators:
1#include <iostream>
2
3using namespace std;
4
5int main() {
6 int x = 10;
7 int y = 5;
8
9 int sum = x + y;
10 int difference = x - y;
11 int product = x * y;
12 int quotient = x / y;
13 int remainder = x % y;
14
15 cout << "Sum: " << sum << endl;
16 cout << "Difference: " << difference << endl;
17 cout << "Product: " << product << endl;
18 cout << "Quotient: " << quotient << endl;
19 cout << "Remainder: " << remainder << endl;
20
21 return 0;
22}
In this code, we declare two integer variables x
and y
and perform arithmetic operations such as addition, subtraction, multiplication, division, and modulo. The result of each operation is then outputted using the cout
object.
By understanding the different operators available in C++, you will have the tools to perform various calculations and comparisons, which are important in finance. These operators form the foundation of more complex algorithms and mathematical models used in the financial industry.