Operators in Cpp
Operators are vital in programming, allowing manipulation, calculations, and data comparisons. In C++, different types of operators serve distinct purposes. Arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, division, and increment/decrement. Relational operators compare values to determine relationships like equality, inequality, and greater/less than.
Logical operators help evaluate conditions using logical AND, OR, and NOT operations. Assignment operators assign values to variables. Bitwise operators manipulate individual bits in binary representations. Understanding and utilizing these operators are crucial for expressing logic, implementing algorithms, and achieving desired outcomes in C++ programming.
Arithmetic Operators
Arithmetic operators are fundamental in programming and are used to perform mathematical calculations. In C++, there are several arithmetic operators available:
A. Addition (+)
The addition operator is used to add two values together.
int sum = 5 + 3;
// sum is now 8
B. Subtraction (-)
The subtraction operator is used to subtract one value from another.
int difference = 10 - 4;
// difference is now 6
C. Multiplication (*)
The multiplication operator is used to multiply two values.
int product = 2 * 6;
// product is now 12
D. Division (/)
The division operator is used to divide one value by another.
double result = 15.0 / 14.0;
// result is now 3.75
E. Modulo (%)
The modulo operator returns the remainder of a division operation.
int remainder = 20 % 3;
// remainder is now 2
F. Increment (++) and decrement (--)
The increment and decrement operators are used to increase or decrease the value of a variable by one.
int num = 5;
num++; //Increment by 1
//num is now 6
int count = 10;
count-–; // Decrement by 1
// count is now 9
These arithmetic operators play a crucial role in performing calculations and manipulating values in C++ programs. By using these operators effectively, you can perform various mathematical operations and implement complex algorithms.
Relational Operators
Relational operators are used to compare values and determine relationships between them. In C++, the following relational operators are available:
A. Equality (==)
The equality operator checks if two values are equal.
bool isEqual = ( 5 == 5);
// isEqual is true
B. Inequality (!=)
The inequality operator checks if two values are not equal.
bool is NotEqual = (10 != 5);
// isNotEqual is true
C. Greater than (>)
The greater than operator checks if one value is greater than another.
bool isGreater = (8 > 3);
// isGreater is true
D. Less than (<)
The less than operator checks if one value is less than another.
bool isLess = (4 < 9); // isLess is true
E. Greater than or equal to (>=)
The greater than or equal to operator checks if one value is greater than or equal to another.
bool isGreaterOrEqual = (7 >= 7);
// isGreaterOrEqual is true
F. Less than or equal to (<=)
The less than or equal to operator checks if one value is less than or equal to another.
bool isLessOrEqual = (6 <= 10);
// isLessOrEqual is true
Relational operators return a boolean value (true or false) based on the comparison result. They are commonly used in conditional statements and loops to make decisions and control the flow of the program based on the relationship between values.