![C++ Game Development By Example](https://wfqqreader-1252317822.image.myqcloud.com/cover/625/36698625/b_36698625.jpg)
Operators
An operator is a symbol that performs a certain operation on a variable or expression. So far, we have used the = sign, which calls an assignment operator that assigns a value or expression from the right-hand side of the equals sign to a variable on the left-hand side.
The simplest form of other kinds of operators are arithmetic operators such as +, -, *, /, and %. These operators operate on a variable such as int and float. Let's look at some of the use cases of these operators:
#include <iostream> #include <conio.h> // Program prints out value of a + b and x + y to screen int main() { int a = 8; int b = 12; std::cout << "Value of a + b is : " << a + b << std::endl; float x = 7.345f; float y = 12.8354; std::cout << "Value of x + y is : " << x + y << std::endl; _getch(); return 0; }
The output of this is as follows:
![](https://epubservercos.yuewen.com/1079FF/19470379008810206/epubprivate/OEBPS/Images/458de139-125b-4dc0-a19c-b1ea186ed3c5.png?sign=1739141539-lQVH5PyWLBReUXKjPKxLzAe3bfG9cclx-0-913d5811aed6f10aed1f6d12ff40c745)
Let's look at examples for other operations as well:
#include <iostream> #include <conio.h> // Program prints out values to screen int main() { int a = 36; int b = 5; std::cout << "Value of a + b is : " << a + b << std::endl; std::cout << "Value of a - b is : " << a - b << std::endl; std::cout << "Value of a * b is : " << a * b << std::endl; std::cout << "Value of a / b is : " << a / b << std::endl; std::cout << "Value of a % b is : " << a % b << std::endl; _getch(); return 0; }
The output is as shown as follows:
![](https://epubservercos.yuewen.com/1079FF/19470379008810206/epubprivate/OEBPS/Images/6ec0b0d7-1bc6-4785-b2dc-d20ce9549e10.png?sign=1739141539-Q53sQwaWGDwCyxSkXzD2kmqIl53ggmgU-0-52c0509573877b69f8efa9561a0331e7)
+, -, *, and / are self-explanatory. However, there is one more arithmetic operator: %, which is called the module operator. It returns the remainder of a division .
How many times is 5 contained in 36? 7 times with a remainder of 1. That's why the result is 1.
Apart from the arithmetic operator, we also have an increment/decrement operator.
In programming, we increment variables often. You can do a=a+1; to increment and a=a-1; to decrement a variable value. Alternatively, you can even do a+=1; and a-=1; to increment and decrement, but in C++ programming there is an even shorter way of doing that, which is by using the ++ and -- signs to increment and decrement the value of a variable by 1.
Let's look at an example of how to use it to increment and decrement a value by 1:
#include <iostream> #include <conio.h> // Program prints out values to screen int main() { int a = 36; int b = 5; std::cout << "Value of ++a is : " << ++a << std::endl; std::cout << "Value of --b is : " << --b << std::endl; std::cout << "Value of a is : " << a << std::endl; std::cout << "Value of b is : " << b << std::endl; _getch(); return 0; }
The output of this is as follows:
![](https://epubservercos.yuewen.com/1079FF/19470379008810206/epubprivate/OEBPS/Images/3a1012d5-e4ab-4caf-9b0e-85630e8267c3.png?sign=1739141539-RpmE4FD2OvNrp6Nbvq0Y84ToLnLLQIDc-0-d350e6d53dc4d4c7e5518d585c290d14)
Consequently, the ++ or -- operator increments the value permanently. If the ++ is to the left of the variable, it is called a pre-increment operator. If it is put afterward, it is called a post-increment operator. There is a slight difference between the two. If we put the ++ on the other side, we get the following output:
![](https://epubservercos.yuewen.com/1079FF/19470379008810206/epubprivate/OEBPS/Images/7d8ead12-2497-4a7e-a4d7-0528a55a5f96.png?sign=1739141539-POckPj7eoPE06PjMYwqwo2z9aBHAKgM8-0-c68fcd6aaf0013266fe466a909a3cdd7)
In this case, a and b are incremented and decremented in the next line. So, when you print the values, it prints out the correct result.
It doesn't make a difference here, as it is a simple example, but overall it does make a difference and it is good to understand this difference. In this book, we will mostly be using post-increment operators.
In fact, this is how C++ got its name; it is an increment of C.
Apart from arithmetic, increment, and decrement operators, you also have logical and comparison operators, as shown:
Logical operators:
![](https://epubservercos.yuewen.com/1079FF/19470379008810206/epubprivate/OEBPS/Images/2.jpg?sign=1739141539-JQF5WxXLrIoP5TMkxnyt5bDbP3HSKikd-0-0b3f7a843c4a49f77f7da985f17d7e27)
Comparison operators:
![](https://epubservercos.yuewen.com/1079FF/19470379008810206/epubprivate/OEBPS/Images/3.jpg?sign=1739141539-8YoVwqxDUZi0A46x8l8DeZzE62WupoMU-0-5495395fa26bf61c8050ef6d86d213d8)
We will cover these operators in the next section.