C++ 03: Operators in C++
Operators in C++
1) Arithmetic Operators in C++
- Addition Operator +
- Subtraction Operator -
- Multiplication Operator *
- Division Operator /
- Modulo Operator %
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 7;
b = 2;
// printing the sum of a and b
cout << "a + b = " << (a + b) << endl;
// printing the difference of a and b
cout << "a - b = " << (a - b) << endl;
// printing the product of a and b
cout << "a * b = " << (a * b) << endl;
// printing the division of a by b
cout << "a / b = " << (a / b) << endl;
// printing the modulo of a by b
cout << "a % b = " << (a % b) << endl;
return 0;
}Increment and Decrement Operators
C++ also provides increment and decrement operators: ++ and -- respectively. ++ increases the value of the operand by 1, while -- decreases it by 1.
For example,
int num = 5;
// increasing num by 1
++num;
Here, the value of num gets increased to 6 from its initial value of 5.
Example 2: Increment and Decrement Operators
// Working of increment and decrement operators
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 100, result_a, result_b;
// incrementing a by 1 and storing the result in result_a
result_a = ++a;
cout << "result_a = " << result_a << endl;
// decrementing b by 1 and storing the result in result_b
result_b = --b;
cout << "result_b = " << result_b << endl;
return 0;
}
Output
result_a = 11 result_b = 99
In the above program, we used ++ and -- operator as prefixes. We can also use these operators as postfix.
There is a slight difference when these operators are used as a prefix versus when they are used as a postfix.
2) Assignment Operators:
- Simple Assignment =
- Add Assign +=
- Subtract Assign -=
- Multiply Assign *=
- Divide Assign /=
In C++, assignment operators are used to assign values to variables. For example,
// assign 5 to a
a = 5;Program to demonstrate Assignment Operators:
#include <iostream>
using namespace std;
int main()
{
// Assigning value 10 using "=" operator
int a = 10;
cout << "Value of a is "<<a<<"\n";
// Assigning value by adding 10 to a
// using "+=" operator
a += 10;
cout << "Value of a is "<<a<<"\n";
// Assigning value by subtracting 10 from a
// using "-=" operator
a -= 10;
cout << "Value of a is "<<a<<"\n";
// Assigning value by multiplying 10 to a
// using "*=" operator
a *= 10;
cout << "Value of a is "<<a<<"\n";
// Assigning value by dividing 10 from a
// using "/=" operator
a /= 10;
cout << "Value of a is "<<a<<"\n";
return 0;
}
| Operator | Example | Equivalent to |
|---|---|---|
= | a = b; | a = b; |
+= | a += b; | a = a + b; |
-= | a -= b; | a = a - b; |
*= | a *= b; | a = a * b; |
/= | a /= b; | a = a / b; |
%= | a %= b; | a = a % b; |
- Equals to ==
- Less Than <
- Less than equals <=
- Greater Than >
- Greater than equals >=
- Not Equals to !=
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 3;
b = 5;
bool result;
result = (a == b); // false
cout << "3 == 5 is " << result << endl;
result = (a != b); // true
cout << "3 != 5 is " << result << endl;
result = a > b; // false
cout << "3 > 5 is " << result << endl;
result = a < b; // true
cout << "3 < 5 is " << result << endl;
result = a >= b; // false
cout << "3 >= 5 is " << result << endl;
result = a <= b; // true
cout << "3 <= 5 is " << result << endl;
return 0;
}- Logical AND &&
- Logical OR ||
- Logical NOT !
Operator | Example | Meaning |
|---|---|---|
| expression1 && expression2 | Logical AND. |
| expression1 || expression2 | Logical OR. |
| !expression | Logical NOT. |
#include <iostream>using namespace std;int main(){//Logical ANDint result = 0 && 1;cout << "Result: 0 && 1 :" << result << endl;//Logical ORresult = 0 || 1;cout << "Result: 0 || 1 :" << result << endl;//Logical Notresult = !1;cout << "Result: ! 1 :" << result << endl;return 0;}

Comments
Post a Comment