C++ Programming

C++ operators and how to use them? Operators in C++ programming

Description:

C++ operators- In this article, you will in detail, what are operators and how to use them in C++ programming. I will explain everything with the help of C++ programs.

Amazon Purchase Links:

Top Gaming Computers

allpcb circuit

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Arithmetic operators:

The arithmetic operators are the symbols that represent arithmetic operations. These are used in arithmetic expressions. Each arithmetic operator operates upon two numeric values( constants and variables ) and returns a value.
The following arithmetic operators are used in C++:

Operators
Meaning

+

Addition

_

Subtraction

*

Multiplication

/

Division

%

For reminder

The ‘ + ’ and ‘ – ‘ symbols are quite familiar to you. The ‘ + ’ symbol indicates addition. Whereas ‘ – ’ symbol indicates subtraction.
The ‘ * ’ (asterisk ) is used in c/ C++ to indicate multiplication.
The ‘ / ’ (slash) is used to indicate for both real and integer division. If both operands in division operators are integers the result will be integer. For example:
5/3 -> 1
3/5 -> 0

Whereas on the other hand real division is performed producing the real result if both or either of the operand is real for example:
5.0 / 2.0 -> 2.5
5 / 3.0 -> 1.666667
3.0 / 4 -> 0.75
While using the division ( / ) operator, the second operand must be a non zero.
The percent sign ( % ) produces a modulus or a reminder of an integer division. % computes the arithmetic remainder after a first integer value is divided by a second integer value. For the modulus operator, both operands must be integers and the second operand must be non zero otherwise it will not work.2 % 2  0
3 % 2 -> 1

11 % 3 -> 2

2 % 10 ->2

5 % 10 -> 5

All arithmetic operators except the remainder operator are used for all types of numeric data.


Example how to use arithmetic operators in C++ programming:

Increment ( ++ ) and Decrement ( — ) operators:

Unlike the other programming languages, C / C++ also supports the ++ “Increment” and — “Decrement” operators to increment and decrement variables by one respectively. Let’s take the example of counting the number of persons entering a room or leaving the room. You can also use it for counting the items etc. The increment operator ( ++ ) is used to add 1 to variable and decrement operator ( — ) is used to subtract 1 from variable. These operators can be used on either side of the variable. So let’s discuss these operators in detail.

Increment ( ++ ) operator:

The increment operator is represented by a pair of plus (++) sign. It is utilized to add 1 to the value of an integer variable. This operator can be utilized previously or after the variable name.

For instance:

In the event that we need to add 1 to a value of variable ab it is regularly composed or written as

ab = ab+1;

by utilizing the increment operator ” ++ ” it is normally written as

ab++;

The increment operator can be written either before or after the variable name. On the off chance that it is composed before the variable, it is known as prefixing. In the event that it is composed after the variable it is known as postfixing. Prefix and postfix operators have a diverse impact when they are utilized in expression.

Prefix increment operator:

At the point when an increment operator is utilized in prefix mode in an expression it adds 1 to the value of the variable before the value of the variable is utilized in the expression.



Example: how to use prefix increment operators in C++ programming:

In the above program 1 will be added to the value of c before it is used in the expression thus after execution the sum will be equal to 9 and value of c will be 4.

Postfix increment operator:

When an increment operator is used in postfix mode in an expression it adds 1 to the value of the variable after the value of the variable has been used in the expression. For example if in the above example increment operator is used in postfix mode the result will be different the statement will be as shown below:

Sum= a + b + c + C++;

In this case, 1 will be added to the value f c after its existing value has been used in the expression. Thus after execution, the sum will be equal to 5 and the value of c will be 4

Decrement ( — ) operators:

The decrement operator is represented by double minus ( — ) sign it is used to subtract 1 from the value of an integer variable.

For example to subtract 1 from the value of a variable ab the decrement statement is written as

Ab–;

Or

-ab;

Prefix decrement operator:

When a decrement operator is used in prefix mode in an expression it subtracts 1 from the value of the variable before the value of the variable is used in the expression.

Example how to use prefix decrement operators in C++ programming:

In the above program, 1 will be subtracted from the value of c before it is used in the expression thus after execution the sum will be equal to 7, and the value of c will be 2.

Postfix decrement operator:

When a decrement operator ( — ) is used in postfix mode in an expression then it subtracts 1 from the value of the variable, after the value of the variable has been used in the expression. For example if in the above example decrement operator is used in postfix mode the result will be different the statement will be as shown below:

Sum= a + b + c + c–;

In this case, 1 will be subtracted from the value f c after its existing value has been used in the expression. Thus after execution, the sum will be equal to 5 and the value of c will be 2.


Assignment operators ( = ):

The assignment operator ( = ) is used to assign values to variables. It is used to assign a numeric or character value to a variable. However the string data can also be assigned to a variable with this operator.

The assignment operator assesses an expression and afterward assigns its value to a variable. The assignment operator ( = ) is utilized to assign the calculated value to a variable. The expression is written on the right side of the assignment operator. The variable to which the value of the expression is to be assigned is written on the left side of the assignment operator.

The syntax of the assignment operator is

Var = expression;

Where:

Var:

Variable name of which the value of expression is assigned. The variable type must be the  same as the type of value returned by the expression.

Expression:

An expression may be a combination of operands (variable and constants ) and arithmetic operators.

A character can also be assigned but a string cannot be assigned to a string variable.

Example how to use assignment operators in C++ programming:

Relational operators:

Relational operators are used to form conditions. These are used to write a conditional expression that describes the relationship between two values .these operations allow us to compare two values to see whether they are equal to each other unequal or whether one is greater than the other or vice versa. Relational operators return the 1 (true) or 0 (False). Don’t confuse the assignment operator ( = ) with the equals relational operator (==) the six relational operators in C++ are:

Greater than ( > ) :

It is used to test if one expression is greater than the other expression. For example, if expression1 and expression2 are two expressions then

expression1 > expression2

If expression1 greater than expression2 then this expression returns a true value. If expression1 is less than expression2 then this expression returns a false value.

Greater than or equal to ( >= ) :

It is used to test if one expression is greater than or equal to the other expression. For example if expression1 and expression2 are two expression then

expression1 >= expression2

If expression1 greater than or equal to  expression2 then this expression returns a true value. If expression1 is less than expression2 then this expression returns a false value.

Less than ( < ) :

It is used to test if one expression is less than the other expression. For example, if expression1 and expression2 are two expressions then

expression1 < expression2

if expression1 is less than expression2 then this expression returns a true value. if expression1 is greater than expression2 then this expression returns a false value.


less than or equal to ( < =) :

It is used to test if one expression is less than  or equal to the other expression. For example if expression1 and expression2 are two expression then

expression1 <= expression2

if expression1 less than or equal to  expression2 then this expression returns true value. if expression1 is greater than expression2 then this expression returns a false value.

Equal to ( ==) :

It is used to test if one expression is equal to the other expression. For example if expression1 and expression2 are two expression then

expression1 == expression2

if expression1 equal to  expression2 then this expression returns a true value. if expression1 is not equal to (less than or greater than ) expression2 then this expression returns a false value. 

not Equal to ( !=) :

It is used to test if one expression is not equal to the other expression. For example if expression1 and expression2 are two expression then

expression1 != expression2

if expression1 is not equal to (less than or greater than)  expression2 then this expression returns true value. if expression1 is equal to expression2 then this expression returns a false value.

Example: how to use Relational operators greater than ( > ) and less than or equal to (<=)  in C++ programming:

Examplea: how to use Relational  operators equal to (==) in C++ programming:


Example how to use Relational  operators not equal to (!=) in C++ programming:

Logical Operators:

More than one condition can be combined and tested in an if statement by connecting them with logical operator &&(AND),  ||(OR),  and ! (NOT). Such condition expression is referred to as Boolean expression complex or compound expression.

A compound expression with && logical expression is true of both of the relational expression are true otherwise it is false for examples:

If((marks > 70) &&  (marks < 80))

Cout<<”garde is A”;

Suppose if marks is 75 the (marks >= 70) && (marks < 80)) is true if marks has the value  60 the (marks >= 70) && (marks < 80)) is false.

The compound expression with (||) logical operation is true if any of the conditions is/are true and false only in the one case when all conditions are false. For example:

If((choice == ‘Y’) || (choice == ‘y’))

goto top;

Thus the logical (choice == ‘Y’) || (choice == ‘y’)) is true if either of the relational expressions is true otherwise it is false.

These definitions are summarized in the following truth table which displays app possible values for two decision-making conditions X and Y and the corresponding values of the logical expression.

 

X

 

 

Y

 

 

(X&&Y)

 

 

(X||Y)

 

true true true True
true False False True
False True False True
False False False False

 

now we discuss each of them in detail. There are three type of logical operators in C++ which are:

  1. && à AND Operator
  2. || à OR    Operator
  3. ! à NOT  Operator

The && (AND) Operator:

The && (AND ) operator is used to combine two or more relational expression. If all relational expressions are true then the output returned by the compound expression is true. If anyone relational expression in the compound expression is false the output is false.



Example how to use logical operators && (AND) in C++ programming:

The || (OR) Operator:

The || (OR) operator is used to combine more than one relational expression. if any one of the given relational expression is true the output will be true otherwise the output will be false.

Example how to use logical operators || (OR) in C++ programming:

The ! (NOT) Operator:

The ! (NOT ) operator is also known as the unary operator. It inverts the value returned by the relational expression or the compound expression.

Example how to use logical operators ! (NOT) in C++ programming:


Bitwise Operators

The bitwise operators are utilized to access the computer hardware. Utilization of bitwise operators requires information on various number systems and conversion numbers from one system into another number system. The data inside the computer is represented in binary form, for example in a succession of 0s and 1s. 8-pieces are utilized to represent one character inside the computer. The bitwise operators are used to manipulate the data inside the computer. These are also used in networking software to move data from one computer to another in the network. The commonly used bitwise operators and their details are discussed in my previous tutorial the link is given below.

The Bitwise && (AND) Operator:

The bitwise AND operator is used to compare the contents of two operands (of int data type) on bit-by-bit basis. This is the type of the operator that I frequently use in the Arduino programs. It returns 1 if he corresponding bit in both the operands is l otherwise it returns 0.

The Bitwise | (OR) Operator:

The bitwise OR operator is used to compare the contents of two operands (of int data type) on bit by bit basis. It returns 1 of any one of the two bits is 1, otherwise it returns 0. It is represented by the vertical bar ( | ).

The Bitwise ^ (XOR) Operator:

The bitwise XOR operator is used to compare the contents to two operands (of int data type) one bit-by-bit basis. It returns 1 if the bits in the corresponding operands are different otherwise it returns 1 (if corresponding bits are same). It is represented by the caret ( ^ ) sign.

The Bitwise ~ (complement) Operator:

The bitwise complement operator is used to operate on an expression. It inverts the bits of the expression. It takes each bit of the operand and converts it to 1 if it is a 0 an to 0 it if is 1. It is  represented by the tilde ( ~ ) sign.

The Left-Shift (<<) Operator

The left shift operator is used to shift a specified number of bits of an operand to the left. It has two operands. The first operand on the left hand-side of the operator is the constant or variable the operator is the constant or variable whose bits are to be shifted. The second operand on the right-hand-side of the operator specified the number of bits that are to be shifted to the left. The right shift operator is represented by the (<<) sign

The Right-Shift (>>) Operator :

The right shift operator is used to shift a specified number of bits of an operand to the right. It has two operands. The first operand at the left hand side of the operator is the constant or variable whose bits are to be shifted. The second operand at the right hand side specified the number of bits that are to be shifted to the right.when the specified number of the bits of the operand are shifted to the right then the same number of bits on the left are filled with 0s. The right shift operator is represented by the (>>) sign


The ‘sizeof’ Operator:

In C or C++ data are mainly divided into three different types. These are:

Character data type:

The characters (Punctuation character, digits, and letters) are represented by the char data type, the short data type is the same size as char usually one byte.

Integer data type:

Integer data like, “3, 56, 654, 7, etc” (whole numbers) are represented by int and long.

Real data type:

Numbers including decimal fractions “43.54, 344.3, etc” are represented by float double or long double.

Now to find the size of these data type in C or C++ we have an operator which is called sizeof operator.

So, if you are required to find the size of data or data type then you are supposed to use the sizeof operator. It has the following general format:

sizeof data

Or

sizeof (data type)

Example how to use sizeof operator in C++ programming:

Comma “,” C++ operator:

In the below program example comma operator is used, comma operator is also known as sequence point operator. It is use to put more than one expression. On a single line separated by commas as use in the below program.

Example how to use comma operator in C++ programming:



Priority of operators:

If an expression has two or more operators then there is a certain order in which these operations are performed by the computer system. This order is referred to as the order of precedence. The precedence order is given below

  1. All parentheses are evaluated first.
  2. Multiplication division and modulus operators are performed first these operators have the same level of precedence they are performed in left to right as they are encountered in an arithmetic expression. Further if an expression contains several like operations evaluation also proceeds from left to right.
  • Addition and subtraction will be performed next. They also have same level of precedence so performance is given to the operator which occurs first.

Parentheses are used to change the priority order because C/C++ programming language evaluates expressions in parentheses first, if parentheses are nested then evaluation begins with the innermost pairs and works outward.

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

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

Back to top button