Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,326 Categories: 12

Total Tutorial Views: 41,377,727

C++ Beginner's Tutorial: Operators.

Tutorial Name: C++ Beginner's Tutorial: Operators.  

Category: PC Tutorials

Submitted By: Nissan

Date Added:

Comments: 0

Views: 2,220

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

Current rating: 10.00 by 2 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"C++ Beginner's Tutorial: Operators." :: Login/Create an Account :: 0 comments

If you would like to post a comment please signin to your account or register for an account.