C++ Operators

In this section you will learn about operators in c++.

Without operators the functionality of the language can be considered is incomplete, Operators are symbol that perform mathematical or logical operation on operands.

You will learn about the following:
  • Unary Operators
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Ternary Operators

Unary Operators:

Unary Operators are operators that operate on one operands these operators also fall in the category of Arithmetic operators, Unary operators are used to increment or decrement value of a variable of type number, Two types of Unary operators are there in c++, 1:Pre-increment or decrement. 2: Post-increment or decrement,Following is a practical example of unary operators.

//Unaary operator Post-increment example #include <iostream>
using namespace std;
int main(){
int number = 10;
//unary operator to increament value
/*in post-increment value is used first then it's incremented for example the following line first old value i.e 10 will be sent to out put then new value will be assign to variable. */
cout<<"Here out put is 10 because value is out put first then it's incremented:"<<number++;
cout <<endl<<endl;
cout<<"Here value is 11 because variable is now being incremented in the above line:"<<number;
}

Out Put

Here out put is 10 because value is out put first then it's incremented:10
Here value is 11 because variable is now being incremented in the above line:11


Example 2

#include <iostream> using namespace std;
int main(){ //Pre-increament unary oprator example int number = 10;
cout<<"number is 11 here because value is first incremented before output: "<<++number;
}

Out Put

number is 11 here because value is first incremented before output: 11

Note: The same things apply to pre-decrements and post-decrements, The operators for pre-decrements and post decrements as --

Arithmetic Operators:

Arithmetic operators are use to perform mathematical calculation such as addition, multiplication, etc... Following are arithmetic operators symbol used in c++.

Operator Symbol Used For
+ Addition
- Subtractions
/Division
*Multiplication
%Taking division reminder

Example 1

//example addition operator #include <iostream> using namespace std;
int main(){
int a,b,c;
a = 10;
b = 15;
//c will have value from addition of a and b c = a + b;
cout<<"Sum of"<<a<<"+"<<b<<"="<<c;
}

Out Put

Sum of 10 + 15 =25

Note:The Rest of the operator may be used in the same manner as like example above when using only one operator. Complexity come when you use multiple operators in one statement for example have a look at the following code.
Example 2

//Example of addition and multiplication combine . int d = 0;
int a = 10,b = 15, c = 5;
d = a + b * c;

what will be the value of 'd' here depend on the precedence order of the operators, Let's have a detail explanation of the precedence order of operators in c++.

Precedence Order of Arithmetic operators in c++ is below:

The precedence order for arithmetic operators in c++ is the same is in algebra, like multiplication, division ans also modules or reminder operators has high precedence than subtraction and addition but these three by self or in the same precedence order, addition and subtraction has the same precedence order, for example when an operand operate on two operators there will be applied left associative rule, left associative rule mean the calculation will be performed from left to right, let's have a clear example to see how left association rule are applied.

c = 4 + 5 - 10 + 12; //in the case above the calculation will be performed from left to right as because addition and subtraction has the same precedence order //and hence the results would be 4 + 5 = 9 - 10 = -1 + 12 = 11

Note:In order to force compiler to use your own precedence order you may put '()' parenthesis on the operand as this will take precedence over other operators and any calculation inside parenthesis will be calculated first then the other one.

Relational Operators

Relational operators are used for comparison of two values stored in variables or may be constant, and determining relation between them, in C++ following relation operators are used:

OperatorsMeaning Example Usage
==equala==b
> Greater than a > b
< Less than a < b
!= not equal a != b
>= greater than or equal a >= b
<= Less than equal a <= b
Example Relational Operator

//relational operator exmple #include <iostream> using namespace std;
int main(){
int a = 10, b = 15;
if(a == b){
cout<<"a and b are equal."<<endl;
}else if(a > b){
cout<<"a is greater than b."<<endl;
}else if(a < b){
cout<<"a is less than b.";
}
}

Out put

a is less than b.

Logical Operators:

Logical operators in c++ are used to perform logical operation and combine two or more condition on operands,logical operators are also used in combination with relational operators two and get the combine results, logical operators return true or false based on the result of the condition.

OperatorUsed forExample usage
&&logical and operation if(a == 1 && b == 2){//do something...}
||logical or operation if(a == 1 || b == 2){/*here if either one of the condition is
satisfied the result is going to be true*/
}
!logical Not operationif(!a == 10){//do something...}
Example logical operators in c++

//Example logical operators used in c++ #include <iostream> using namespace std;
int main(){
/*program to get a number from user and check whether this number is equal to the number unerhand an another variable */ int guess = 10;
int maxTry = 3;
int tryCount = 0;
int input; bool tryLeft = true;
flag:
while(tryLeft){
cout<< "Enter Your Number within the range 1-20 to guess the hidden number: ";
cin>>input;
if(input < 1 || input > 20){
cout<<"Try entering number withing the range mentioed."<<endl;
goto flag;
}
else if(input == guess){
cout<<endl<<"*****************************************"<<endl;
cout<<"Congratulation! You Guess the Number, The Number was:"<<guess;
break;
}else if(input != guess){
tryCount++;
if(tryCount > 3){
tryLeft = false;
cout<<"Sorry! You didn't find the number within your try limit."<<endl;
goto flag;
}
cout<<"Try "<<tryCount<<" Of "<<maxTry<<endl;
cout<<"Try Again!"<<endl;
goto flag;
}
}
}

Bitwise Operators:

In c++ Bitwise operators are use two perform bitwise operation on operands.Following is table of bitwise operators used in c++.

OperatorUsed forExample Usage
&Bitwise And operationif(a & b){//Condition will only be true if each bit of a is
equal to b other wise false.
}
|Bitwise or operationif(a | b){//if any of the two bits of a and b or 1 result will be 1(true)
otherwise false.
}
^Bitwise exclusive orTakes two numbers as operands
and does XOR on every bit of two numbers.
The result of XOR is 1 if the two bits are different.
<< Left shift Operationa = 1;
c = a << 1 This would result value 2 in c
because bit of a will be shifted left by 1 bit
the second number on the right of the operator
decides the number of bit to shift
>> Right shift OperationRight shift operation is opposite to left shift operation.
Example 1 Bitwise operators

#include <iostream> using namespace std;
int main(){
int a,b,c;
a = 2;//binary value of a = 0010
b = 4;//binary value of b = 0100
c = a & b; //and hence this would result in c = 0000. Because if we take logical and of each bit of the above two number we will get this.
cout>>c;
}

Out put:

0

Example 2 Bitwise or operation in c++

#include <iostream> using namespace std;
int main(){
int a,b,c;
a = 2;//binary value of a = 0010
b = 4;//binary value of b = 0100
c = a | b; //and hence this would result in c = 0110. Because if we take logical or of each bit of the above two number we will get this.
cout>>c;
}

Out put:

6

Example 3:

//Left shift Operator example #include <iostream> using namespace std;

int main(){
int a, b, c;
a = 2; //binary value of a = 0010
b = 1; //binary value of b = 0001
c = a << b; //resultant value of is derived by shifting 1 bit all bits of a to left which give us c = 0100
cout<<c;
}

Out put:

4

Example 4:

//Right shift Operator example #include <iostream> using namespace std;

int main(){
int a, b, c;
a = 2; //binary value of a = 0010
b = 1; //binary value of b = 0001
c = a >> b; //resultant value of is derived by shifting 1 bit all bits of a to right which give us c = 0001
cout<<c;
}

Out put:

1


No comments:

Post a Comment

What is Cplusplus? Introduction To C++

What is C++? The Simple Answer to this question is c++ is a computer programming language. What is computer programming language and wh...