Consider the following operation: In the above statement, x and y are the operands while + is an addition operator. When the C++ compiler encounters the above statement, it will add x and y and store the result in variable a. In this C++ Tutorial, you will Learn:
What are Operators?
Types Of Operators in C++
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Other Operators
sizeof operator
Comma Operator
Conditional Operator
Operators Precedence
Types Of Operators in C++
There are mainly 6 different types of operators in C++
Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Other Operators
Arithmetic Operators
They are the types of operators used for performing mathematical/arithmetic operations. They include: For example: Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in our code. It will allow us to read from and write to the console. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. Declaring an integer variable a and initializing it to 11. Declaring an integer variable b and initializing it to 5. Declaring an integer variable c. Printing value of operation a+b alongside other text on the console. Printing value of operation a-b alongside other text on the console. Printing value of operation a*b alongside other text on the console. Printing value of operation a/b alongside other text on the console. Printing value of operation a%b alongside other text on the console. Printing value of operation a++ alongside other text on the console. Printing value of operation a– alongside other text on the console. The main() function should return an value if the program runs fine. End of the body of the main() function.
Relational Operators
These types of operators perform comparisons on operands. For example, you may need to know which operand is greater than the other, or less than the other. They include: For example: Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in our code. It will allow us to read from and write to the console. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. Declaring an integer variable a and initializing it to 11. Declaring an integer variable b and initializing it to 5. Printing some text on the console stating the values of variables a and b. Performing the arithmetic operation, a==b in an if decision making statement to know whether it’s true or false. The { marks the beginning of the body of the if statement. Text to print on the console if the operation a==b is true. The endl is a C++ keyword for end line. It pushes the cursor to start printing in the next line. The } marks the end of the body of the if statement. The else part of the above if statement. It states what to do if the operation a==b is false. Text to print on the console if the operation a==b is false. The endl is a C++ keyword for end line. It pushes the cursor to start printing in the next line. The } marks the end of the body of the else statement. Performing the arithmetic operation, a<b in an if decision making statement to know whether it’s true or false. The { marks the beginning of the body of the if statement. Text to print on the console if the operation a<b is true. The endl is a C++ keyword for end line. It pushes the cursor to start printing in the next line. The } marks the end of the body of the if statement. The else part of the above if statement. It states what to do if the operation a<b is false. Text to print on the console if the operation a<b is false. The endl is a C++ keyword for end line. It pushes the cursor to start printing in the next line. The } marks the end of the body of the else statement. Performing the arithmetic operation a>b in an if decision making statement to know whether it’s true or false. The { marks the beginning of the body of the if statement. Text to print on the console if the operation a>b is true. The endl is a C++ keyword for end line. It pushes the cursor to start printing in the next line. The } marks the end of the body of the if statement. The else part of the above if statement. It states what to do if the operation a>b is false. Text to print on the console if the operation a>b is false. The endl is a C++ keyword for end line. It pushes the cursor to start printing in the next line. The } marks the end of the body of the else statement. The main() function should return an value if the program runs fine. End of the body of the main() function.
Logical Operators
The logical operators combine two/more constraints/conditions. Logical operators also complement evaluation of original condition under consideration. They include: For example: Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in our code. It will allow us to read from and write to the console. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. Declaring 4 integer variables a, b, c and d and assigning them different values. Using the && (AND) operator inside the if statement. It joins two conditions, value of a equals to value of b and, value of a is greater than value of b. First condition is false, second condition is true. False&&true is False, hence, the outcome of if is false. Text to print on console if the above if statement is true. This won’t be executed. Part to be executed if the above if statement is false. Text to print on the console if the if statement is false. This will be executed. Using the || (OR) operator within an if statement. It joins two conditions, value of a equals to value of b and, value of a is greater than value of b. First condition is false, second condition is true. False||true is True, hence, the outcome of if is true. Text to print on console if the above if statement is true. This will be executed. Part to be executed if the above if statement is false. Text to print on the console if the if statement is false. This will not be executed. Checking whether value of variable is 0. Text to print on console if the above if statement is true. This will not be executed. Part to be executed if the above if statement is false. Text to print on the console if the if statement is false. This will be executed. The main() function should return an value if the program runs fine. End of the body of the main() function.
Bitwise Operators
Bitwise operators perform bit-level operations on operands. First, operators are converted to bit level then operations are performed on the operands. When arithmetic operations like addition and subtraction are done at bit level, results can be achieved faster. They include: Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in our code. It will allow us to read from and write to the console. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. Declaring an unsigned integer variables p and assigning it a value of 60, which is, 0011 1100 in binary. Declaring an unsigned integer variables q and assigning it a value of 13, which is, 0000 1101 in binary. Declaring an integer variable z and initializing it to 0. Performing the bitwise & (AND) operation on variables p and q and storing the result in variable z. Printing the result of the above operation on the console alongside other text. Performing the bitwise | (OR) operation on variables p and q and storing the result in variable z. Printing the result of the above operation on the console alongside other text. Performing the bitwise ^ (XOR) operation on variables p and q and storing the result in variable z. Printing the result of the above operation on the console alongside other text. Performing the bitwise ~ (NOT) operation on variables p and q and storing the result in variable z. Printing the result of the above operation on the console alongside other text. Performing the left shift operation on variable p and storing the result in variable z. Printing the result of the above operation on the console alongside other text. Performing the right shift operation on variable p and storing the result in variable z. Printing the result of the above operation on the console alongside other text. The main() function should return an value if the program runs fine. End of the body of the main() function.
Assignment Operators
Assignment operators assign values to variables. The operand/variable is added to left side of the operator while the value is added to the right side of the operator. The variable and the value must belong to the same data type, otherwise, the C++ compiler will raise error. For example: In the above example, x is the variable/operand, = is the assignment operator while 5 is the value. Here are the popular assignment operators in C++: For example: Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in our code. It will allow us to read from and write to the console. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. Declaring an integer variables x and assigning it a value of 5. Printing value of variable x alongside other text on the console. The \n is a new line character. It moves the cursor to the next line when printing. Adding 5 to value of variable x and assigning result to variable x. Printing value of variable x on the console alongside other text. Subtracting 5 from value of x and assigning result to variable x. Printing value of variable x on the console alongside other text. Multiplying value of variable x with 5 and assigning result to variable x. Printing value of variable x on the console alongside other text. Dividing value of variable x by 5 and assigning result to variable x. Printing value of variable x on the console alongside other text. The main() function should return an value if the program runs fine. End of the body of the main() function.
Other Operators
Other Operators include sizeof operator, Comma Operator, Conditional Operator, and Operators Precedence. Let us discuss other operators supported by C++:
sizeof operator
This operator determines a variable’s size. Use sizeof operator to determine the size of a data type. For example: Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in our code. It will allow us to read from and write to the console. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. Determining the size of integer data type using sizeof operator and printing it alongside other text on the console. Determining the size of character data type using sizeof operator and printing it alongside other text on the console. Determining the size of float data type using sizeof operator and printing it alongside other text on the console. Determining the size of float data type using sizeof operator and printing it alongside other text on the console. The main() function should return an value if the program runs fine. End of the body of the main() function.
Comma Operator
The comma operator (,) triggers the performance of a sequence of operations. It expresses first operand and discards the result. Next, it evaluates the second operand and returns the value and type. Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in our code. It will allow us to read from and write to the console. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. Declaring two integer variables x and y. Assigning the variable y a value of 100. Incrementing value of y and assigning result to variable x. It start with y at 100, then increments it to 101 (y++). Next, y is added to 10. Finally, y, still at 101, is added to 99, giving 200. x is now 200. Printing value of variable x on the console. The main() function should return an value if the program runs fine. End of the body of the main() function.
Conditional Operator
This operator evaluates a condition and acts based on the outcome of the evaluation. Syntax: Parameters:
The Condition is the condition that is to be evaluated. Expression1 is the expression to be executed if condition is true. Expression3 is the expression to be executed if condition is false.
For example: Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in our code. It will allow us to read from and write to the console. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. Declaring two integer variables a and b. Variable a has been assigned a value of 1. Assigning value to variable b. If variable a is less than 10, b will be assigned the value 2, otherwise, b will be assigned a value of 5. Printing value of variable b on the console alongside other text. The main() function should return an value if the program runs fine. End of the body of the main() function.
Operators Precedence
A single operation may have more than one operator. In that case, operator precedence determines the one evaluated first. The following list shows the precedence of operators in C++, with decreasing precedence from left to right: (), [],*, /, %, +/-, « , », == , !=, ^, |, &&, ||, ?:, =, +=, -=, *=, /=
Summary:
Operators are symbols for performing logical and arithmetic operations. Arithmetic operators help us perform various arithmetic operations on operands. Relational operators help us perform various comparison operations on operands. Logical operators help us perform various logical operations on operands. Bitwise operators help us perform bitwise operations on operands. Assignment operators help us perform various arithmetic operations on operands. The sizeof operator returns the size of a variable or data type. The comma operator executes a sequence of operations. The conditional operator evaluates a condition and acts based on the outcome.