Operator :- An operator is a symbol that "opetates" on one or more expression, producing a value that can be assigned to a variable.
or
operator are used to perform operations on variables and value.
C++ divides the operators into the following groups:
- Arithmetic operators.
- Assignment operators.
- comparison operators.
- Logical operators.
- conditional operators.
What is an Arithmetic operators:-Arithmetic operator sre the simplest and basic operators that do arithmetic operators.These operatoers require two variables and are called Binary operators.The various Arithmetic operators. in C++ are as follows:
| Operator | Meaning |
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus(Remainder of integer division) |
EX:-
#include <iostream>
using namespace std;
int main() {
int a,b,c;
cout<<"Enter the number1:";
cin>>a;
cout<<"Enter tne number2:";
cin>>b;
c=a+b;
cout<<"Addition of two number="<<c<<endl;
return 0;
}
OUTPUT:-
Enter the number1:5
Enter tne number2:3
Addition of two number=8
What is an Assignment operators:-An assignment operators is used to assing the value toa variables.
The syntax is
variable=expression;
e.g.
a=5;
b=50
x=y=z=100;
The various Assignment operators are:
| Operators. | Meaning. |
| = | Assings value on right hand side(RHS)to the left hand side(LHS). |
| += | Value of LHS variable will be added to value of RHS and assing back to the variable in LHS. |
| -= | Value of RHS variable will be subtracted from the value of LHS and assing it back to the variable in LHS. |
| *= | Value of LHS variable will be multiplied by value of RHS and assing it back to the variable in LHS. |
| /= | Value of LHS variable will be divided by the value of RHS and assing it back to the variable in LHS. |
| %= | The remainder will be stored back to the LHS after integer division is carried out between LSH variable and LHS variable. |
e.g. x+= y is equivalent to x=x+y
a+=7 =>a =a+7;
a*=4 =>a=a*4
a%=(x-10) =>a=a%(x-10)
Example:-
#include <iostream>
using namespace std;
int main() {
int a,b,c;
cout<<"Enter the number1:";
cin>>a;
a+=a;
cout<<"Assignment operator="<<a<<endl;
return 0;
}
OUTPUT
Enter the number1:10
Assignment operator=20
What is comparison operators:- Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions
The return value of a comparison is either 1 or 0, which means true (1) or false (0).
| Operators | Name | Example |
| == | Equal to | x==y |
| != | Not equal | x!=y |
| > | Greater than | x>y |
| < | Less than | x<y |
| >= | Greater than or equal to | x>=y |
| <= | Less than or equal to | x<=y |
Example
#include <iostream>
using namespace std;
int main() {
int a,b,c;
cout<<"Enter the number1:";
cin>>a;
cout<<"Enter tne number2:";
cin>>b;
c=a==b;
cout<<"compare of two number="<<c<<endl;
return 0;
}
OUTPUT
1 .Enter the number1:5
Enter tne number2:4
compare of two number=0
2.Enter the number1:5
Enter tne number2:5
compare of two number=1
What is a Logical operators:-Logical operators are referred to as Boolean operators.These operator act upon operands that are themself logical exprerssion. After testing the value of condition, it gives the result , which is either True or False. The logical operators are:
| operator | Meaning |
| && | AND |
| || | OR |
| ! | NOT |
The AND (&&) operator gives the result true if both (all) the conditions have the true value, otherwise gives the result false.
| Condition1 | Condition2 | Result |
| False | False | False |
| False | True | False |
| True | False | False |
| True | True | True |
Example 1:-
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x > 3 && x < 10); // returns true (1) because 5 is greater than 3 AND 5 is less than 10
return 0;
}
OUTPUT
1
OR (||) operator gives the result true if any onr of the condition or both the condition are true.
| Condition1 | Condition2 | Result |
| False | False | False |
| False | True | True |
| True | False | True |
| True | True | True |
Example 2:-
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x > 3 || x < 4); // returns true (1) because one of the conditions are true (5 is greater than 3, but 5 is not less than 4)
return 0;
}
OUTPUT
1
NOT(!) operator gives the result true if condition is false and false if condition is true.
| Condition1 | Result |
| False | True |
| True | False |
Example 3:-
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (!(x > 3 && x < 10)); // returns false (0) because ! (not) is used to reverse the result
return 0;
}
OUTPUT
0
What is a Conditional Operator:-The Cinditional operator(?) is used to carry out conditional operations. It can be used in place of if-else statement.
The syntax is:- expression 1 ? expression2: expression 3
Note:- The conditional expression operator is generally used only when the conditonal and both expressions are very simple.
Example:- #include <iostream> #include <string> using namespace std; int main() { double marks; // take input from users cout << "Enter your marks: "; cin >> marks; //conditionaloperator checks if // marks is greater than 40 string result = (marks >= 40) ? "passed" : "failed"; cout << "You " << result << " the exam."; return 0; }
Output 1
Enter your marks: 80 You passed the exam.
Output 2
Enter your marks: 39.5 You failed the exam.