Operators in C++ Language

Operators in C++ Language

Operators in C++:

What Are Operators? Operators in C++ are symbols that use to perform specific tasks on data. C++ operators include Arithmetic operators, logical operators, bitwise operators, relational operators, compound assignment operators etc. The operators can be categorized as follows:

operators in C++

  • Unary operators:

Unary operators work with only one operand. Following are unary operators:

++, –, ~, ! etc. Examples of unary operators are a++, –b.

  • Binary operators:

Binary operators work with two operands. Following are binary operators:

+, -, *, % etc. Examples of unary operators are a+b, a*b.

 

Example program:

#include<iostream>

using namespace std;

int main(){

int a=3, b=6, ans;

a = a++;                           //unary operator

ans = a + b;                     //binary operator

cout<<“a= “<<a<<” and a + b = “<<ans;

return 0;

}

Try It Now:

 

  • Arithmetic operator:

The arithmetic operator used to performs mathematical operations on data. Following are the arithmetic operators in C++ Language:

OperatorsSymbolsDescription

Addition

+

Add two values

Subtraction

Subtract two values

Multiplication

*

Multiply two values
Division

/

Divide two values

Modulus%

Use to find the reminder of the division of two values.

All arithmetic operators word only with numeric values. Let see an example program:

#include<iostream>
using namespace std;
int main(){
int a=3, b=6, ans;
ans = a + b;
cout<<” a + b = “<<ans<<endl;
ans = a – b;
cout<<” a – b = “<<ans;
return 0;
}

In the above program, we add two values then subtract them.

Try It Now:

 

  • Assignment operator:

A statement that use to assign a value to the variable is called an assignment statement. Assignment operator ‘=’ use to assign a value. The variable name always writes on the left side of the assignment operator. And the value writes on the right side of the assignment operator. The values can be an expression, constant, variable or a function.

Syntax:

variable = value;

Example:

ans = add(1,4);

ans = ans + 10;

  • Increment/Decrement Operator:

Increment operator uses to increase the value of a variable by 1. It denotes by ‘++’. Decrement operator uses to decrease the value of a variable by 1. It denotes by ‘–‘. Increment/Decrement operator is an unary operator.

The Increment/Decrement operator cannot increase/decrease the value of constant and expressions. For example, a++ is valid statement but 17++ is not valid. –a is also valid but 45– is not. Increment/decrement operator can be used in two forms(prefix and postfix form).

Prefix form:

In prefix form, the operator writes before the variable. For Example, ++y. Here, firstly increment/decrement in the variable then other acts will perform. For example: if b=5 and a = ++b; then the value of a will 6.

Postfix form:

In postfix form, the operator writes after the variable. For Example, y++. Here, firstly act perform then increment/decrement in the variable. For example: if b=5 and a = b++; then the value of a will 5 because of postfix increase the value after assigning it to another variable.

Please Note That if you are facing any kind of problem then tell us we will provide you with the possible solution.

We have the best collection of computer courses on our website. Our website is daily updated latest updated software and games. Also, visit our facebook page.

Summary
Review Date
Reviewed Item
Operators in C++ Language
Author Rating
51star1star1star1star1star

Add a Comment

Your email address will not be published. Required fields are marked *