In C++, the “<<” operator is used to redirect to the standard input-output object. During redirection, the “\n” definition is used to jump to the beginning of the next line after a line is printed. The word “cin” is used together with the “>>” operator when entering data from the keyboard.
In order to be able to allow for input and output, the iostream file containing the relevant libraries must be included in the program, as shown below:
#include using namespace std;
C++ Operators
Symbols used to change data by performing arithmetic operations on a set of data, to obtain a new set of data from two existing pieces of data, to compare more than one piece of data, or to perform logical operations on the data are called operators.
In the following section, we will learn about some of the different C++ operators and how to use them. We begin with the assignment operator, which is represented by the equal sign or =.
Assignment Operator (=) in C++
The = operator is used when assigning any value to a variable. Here is an example of how to use the assignment operator = in C++:
int num = 12; char letter = 'n';
Arithmetic Operators in C++
Operators used for performing mathematical operations in C++ are known as arithmetic operators. These operators work – mostly – in the same way that the math operators you learned in school work. For example, the + operator – or addition operator – is used to add two or more numbers to one another.
The C++ arithmetic operators include:
- Addition: This operator is used to perform addition. It is represented by the + symbol.
- Subtraction: This operator is used to perform subtraction. It is represented by the – symbol.
- Multiplication: This operator is used to perform multiplication. It is represented by the asterisk or * symbol.
- Division: This operator is used to perform division. It is represented by the forward-slash or / symbol.
- Modulo: This operator is used to return the remainder of a division. It is represented by the percent or % symbol.
Below is a list of examples showing how to use the various mathematical operators C++ has to offer. Feel free to try the code our in your integrated development environment (IDE) or code editor to better understand their use:
C++ Addition Operator Example:
#include using namespace std; // This program adds two numeric values // and displays the result. int main() { int num1 = 120; int num2 = 300; int Sum; Sum = num1 + num2; cout << "Total: " << Sum << "\n"; return 0; }
Running this code results in the following output:
Total: 420
Here is an example of how to use the C++ multiplication * operator to find the area of a circle:
C++ Multiplication Example:
#include using namespace std; // Area of the Circle int main() { double pi = 3.1415; double r = 13; double area; cout << "Area of the Circle" << "\n"; cout << "--------------" << "\n"; area = pi * r * r; cout << area << " cm2" << "\n"; return 0; }
This code results in the following output:
Area of the Circle -------------- 530.914 cm2
This code example shows how to use multiple C++ mathematical operators:
#include using namespace std; int main() { int num1 = 6, num2 = 3; cout << "sum of two numbers = " << num1 + num2 << endl; cout << "subtraction of two numbers = " << num1 - num2 << endl; cout << "product of two numbers = " << num1 * num2 << endl; cout << "division of two numbers = " << num1 / num2 << endl; return 0; }
Once more, the output of running this code is:
sum of two numbers = 9 subtraction of two numbers = 3 product of two numbers = 18 division of two numbers = 2
Modular Arithmetic Operator (%) in C++
As we all know from mathematics, when the first number is divided by the second number, the remainder is the mod value. When you want to return the modulo – or remainder of a division – in C++, you can do so in the following manner:
#include using namespace std; int main() { int x = 8 ,y = 4 ,z = 3; int mod1 = x%y; int mod2 = x%z; int mod3 = y%z; cout << "mod1 = " << mod1 << endl; cout << "mod2 = " << mod2 << endl; cout << "mod3 = " << mod3 << endl; return 0; }
The above code sample results in the following output when you run it in your code editor or IDE:
mod1 = 0 mod2 = 2 mod3 = 1
Increment and Decrease Operators in C++
The ++ operator increments the value of the adjacent variable by one; the — operator decrements it by one. The meaning of these operators changes if they are placed to the left or right of the variable.
The following example show what happens if we use the increment ++ operator to the left of the variable:
a = ++b
The increment operator first increments the value of b by one and then assigns it to the variable a. In this situation, the value of the a and b variables will be the same.
On the other hand, if we use the increment operator to the right of the variable, as in this example:
a = b++
The increment operator first assigns the value of b to a, then increments b by one. In this situation, the value of b is one more than the value of a.
Let’s summarize the use of the increment and decrement operators in a table:
Here are some examples further showing how to use increment and decrement operators in C++:
//Multiplication of one minus of the entered numbers #include using namespace std; int main() { int a,b; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; --a; --b; cout << "total = " << a*b << endl; return 0; }
This results in the following output:
Enter first number: 3 Enter second number: 5 total = 8
Example 2:
//Using the increment operator #include using namespace std; int main() { int a=50; int b=30; cout << "a= " << a++ << "\n"; cout << "a= " << a << "\n"; cout << "b= " << ++b <<"\n"; return 0; }
Again, the result of running this code results in the follow output:
a= 50 a= 51 b= 31
Example 3:
//Using the increment operator 2 #include using namespace std; int main() { int counter; int a; counter=5; a=0; a=counter++; cout << "a= "<< a++ << "\n"; cout << "counter= " << counter << "\n"; cout <<"--------"<<"\n"; counter=5; a=0; a=++counter; cout << "a= " << ++a << "\n"; cout << "counter= " << counter<< "\n"; return 0; }
Running this code in your code editor results in:
a= 5 counter= 6 -------- a= 7 counter= 6
Arithmetic Assignment Operators in C++
In C++, some arithmetic operations can also be expressed in a different form, known as abbreviated.
Comparison Operators in C++
Operators used to compare two numeric values or two characters are known as comparison operators. The if statement is used for comparison as well. If the result of the comparison is “true”, the line following this statement is processed; if “not true” – or false -, the line after the else statement is processed.
Here is a list of the available comparison operators in C++:
- Greater than: Used to test if a value is greater than another value. It is represented by the > symbol.
- Less than: Used to test if a value is less than another value. It is represented by the < symbol.
- Greater than or Equal to: Used to test if a value is either greater than OR equal to another value. It is represented by the >= symbol.
- Less than or Equal to: Used to test if a value is either less than OR equal to another value. It is represented by the <= symbol.
- Equal to: Used to test if a value is equal to another value. Represented by two equal symbols or ==.
- Not Equal: Used to test if a value is not equal to another value. Results in true if so. Represented by the !=.
Here are some examples of how to use comparison operators in C++
Comparison Operator Example 1
//Comparison Operators #include using namespace std; int main() { int num; cout << "Enter a number:"; cin >> num; if (num<0) cout << "Entered a negative number: " << num<< "\n"; else cout << "Entered a positive number: " << num << "\n"; return 0; }
Here is the output of this code:
Enter a number:-10 Entered a negative number: -10 Enter a number:5 Entered a positive number: 5
Comparison Operator Example 2
#include using namespace std; int main() { bool a=(6<3); bool b=(5>8); bool c=(10<=10); bool d=(12>=15); bool e=(1==1); bool f=(0!=0); cout << "a value= " << a << endl; cout << "b value= " << b << endl; cout << "c value= " << c << endl; cout << "d value= " << d << endl; cout << "e value= " << e << endl; cout << "f value= " << f << endl; return 0; }
Here is the output of this example:
a value= 0 b value= 0 c value= 1 d value= 0 e value= 1 f value= 0
As you probably know, 0’s represent “not true” and 1’s represent “true”. Using the bool method, we are able to achieve these results.
Logical Operators in C++
Logical operators are used to test two or more conditions together. In this case, two or more conditions are evaluated together considering their truth values.
Below you will find some programming examples of C++ logical operators.
Logical Operator Example
//logical operators #include using namespace std; int main() { int num1,num2; cout << "Enter first number: "; cin >> num1; cout << "Enter second number: "; cin >> num2; if (num1>0 && num2>0) cout << "Both numbers are positive"; else cout << "At least one of the numbers is negative."; return 0; }
The result of this code? See below:
Enter first number: -5 Enter second number: 15 At least one of the numbers is negative.
Special Purpose Ternary Operator ( ? : ) in C++
The special ternary operator performs in the same manner as the if structure. Its usage is as follows:
? :
If the condition is true, the expression to the left of the : sign is valid, and if the condition is not true, the expression to the right of the : sign is valid.
Here is an example of the ternary operator in C++ being used:
#include using namespace std; int main() { int a=10, b=5, c=25, d=15, e, f; e=a>b ? a:c; f=c
This code results in the following output:
e value= 10 f value= 13
Priority Order of Transactions and Order of Precedence
Just like in mathematics, most programming languages have a sequence of operations or order of precedence. In order for C++ to execute operations in an orderly manner, a priority order is defined for each operation. According to this order, operations in parentheses are performed first, and, finally, the sequence of operations is concluded with the equals operator.
Operations with the same priority are in order of priority from left to right.
You can examine the table below for the order of priority:
You learned about operator operations and how they are used in C++. Based on the practices we provide, you can code your own simple programs, so you will repeat and learn the subject better. Please do not try to memorize them. This will both delay your learning and cause confusion. Instead, try to practice by writing a lot of software projects and C++ code.
Read more C++ programming tutorials.
Check out this C++ Beginners Course!