Tutorials Navigation
C++ Beginner's Tutorial: Operators.
Tutorial Name: C++ Beginner's Tutorial: Operators.
Category: PC Tutorials
Submitted By: Nissan
Date Added:
Comments: 0
Views: 2,242
Related Forum: PC Building Forum
Share:
C++ Beginners Tutorial: Operators.
Weve learnt how to output text, declare variables and receive input, but as of yet we cant do anything with those variables themselves. Sure, we can just give them values, but what use is that? What we really need is a way to manipulate variables without doing any hard work ourselves. Luckily, our salvation is found in the form of operators, little characters that really add to the functionality of programs.
We have already investigated two operators linked to C++s iostream library; the input and output operators, or >> and <<, respectively, but there are a lot more, most of which are much more useful than the iostream ones!
These operators can be put into little categoric groups, the first of which being the arithmetic operators. They are exactly as they sound; operators for performing arithmetic, or in simpler terms, maths. Yep, they are just the age old +, -, / and * signs, and are used in exactly the same way as they are in normal mathematics. For example, this little piece of code adds two numbers together and displays the result:
#include <iostream>
using namespace std;
int sum = 0;
int main() {
sum = 64 + 32;
cout << sum;
return 0;
}
The others are used in exactly the same way, and can even be combined into one statement; this little line is perfectly valid:
var = 6 + 5 * 9 - 33 / 2;
The problem with doing that though is that C++ processes commands in a certain order, and so you have to be careful to make sure that your statement will produce the result that you want it too. It is safer just to not mix arithmetic operators in the same statement, or to use parentheses. Parentheses (or brackets) tell C++ to do things in a certain order; it always performs what is in the parentheses first. This is a handy way to ensure that you get the result that you want.
These are not the only arithmetic operators though, there are a few more, such as %, -- and ++. The first of these is called modulo and is like division, but instead of returning the answer it returns the remainder produced. For example, 7 divided by 2 will not produce 3.5, like / would (assuming floats, if using ints or chars you would get 3, as the fractional part is dropped), but instead gives a result of 1; the remainder. This operator is quite specialised so you won't use it much, but we will cover it in a later section.
The other two are the incremented decrement operators, and they do exactly what their names states; they decrease or increase a number by 1. These operators are quite special however, they are what is called unary operators; operators with only one operand. Everything before now has had something on either side of the operator:
4 + 5,
cout << Hi,
var = 10;
But unary operators only have something on one side, and in the case of the increment and decrement operators, that something can be on either side. This placement is not a casual choice however;
var++ can produce different results to ++var. In fact, it changes the order in which the operation is performed. For example:
i++ or i-- First the value of i is sent to the program, then i is incremented
++i or --i First i is incremented, then the result is sent to the program
The increment and decrement operators are mostly used just to shorten code, as you could achieve the exact same thing by using the + or - operators.
Ratings
Comments
Related Tutorials
- 01. Emulating Xbox 360 on PC for Running COD4 With Mods(3,656)
- 02. How to: Matrix Numbers | Batch File(1,954)
- 03. How to Password Protect Files on Windows(865)
- 04. How to play Socom 2/3/ and Combined Assault on PC(6,879)
- 05. Modern Warfare 2 Vac Ban Bypass Tutorial(6,243)
- 06. How to embed an image on TheTechGame(3,140)
- 07. [PC] NIOH 2 OTHER USER SAVE RESIGN(13,088)
- 08. Host bot lobbies! Full Tutorial!(11,611)
- 09. Unban yourself [Plutonium BO2](14,308)
- 10. Fall Guys - How to Change Your Name Color on Fall Guys(8,410)
- 11. Best Crosshair Settings for Valorant(6,550)
- 12. Othercide The Surgeon Boss Guide(2,568)
- 13. Othercide Remembrances Unlock Guide(4,517)
- 14. Othercide Beginners Tips and Tricks(2,734)
- 15. How to Fix Grounded Crashes, Loading Time, Low FPS and Other(4,881)
"C++ Beginner's Tutorial: Operators." :: Login/Create an Account :: 0 comments