PDA

View Full Version : C++ Operator Tutorial



Dusk412
07-09-2008, 06:45 PM
C++ Operator Tutorial

Description:
This tutorial will teach you all you need to know about C++ operators such as adding, subtracting, or equal.

Table of Contents:
I: Introduction
II. Assignment Operator
III: Basic Math Operators
IIV. Modulus Division
V. Increment/Decrement Operators
VI. Comparison Operators
VII. Insertion Operator
VIII. Compound Assignment Operators
IX. Logical Operators

I. Introduction:
Operators are symbols used to do math on or compare or assign a value to variables.

II. Assignment Operator:
Equals (=):

a = 4;
Sets the variable a equal to the value 4.

III. Basic Math Operators:
Addition (+):

a = 6 + 2;
Sets the variable a equal to the value 6 + 2 or 8.

Subtraction (-)

a = 6 – 2;
Sets the variable a equal to the value 6 - 2 or 4.

Division (/):

a = 6 / 2;
Sets the variable a equal to the value 6 / 2 or 3.

Multiplication (*)

a = 6 * 2;
Sets the variable a equal to the value 6 * 2 or 12.

IV. Modulus Division:
Modulus Division or Modulo (%):
Modulus Division is when you divide two numbers and it returns the remainder.

a = 7 % 2;
Sets the variable a equal to the value 7 % 2 or 1, because 7 / 2 = 3 with a remainder of 1.

V. Increment/Decrement Operators:
Increment (++):
Adds one to the variable.

a++;
This is the same as saying
a = a + 1; or as you will later learn
a += 1;
Sets the variable a equal to the value a + 1.

Decrement (--):
Subtracts one from the variable.

a--;
This is the same as saying
a = a - 1; or as you will later learn
a -= 1;
Sets the variable a equal to the value a - 1.

VI. Comparison Operators:
Comparison operators are mostly used for if statements or other comparison statements when you want to check if a variable is related to another number or variable in a certain way.

Greater Than (>):

1>2; Returns False, 1 is not greater than 2.

2>1; Returns True, 1 is greater than 2.

Less Than (<):

1<2; Returns True, 1 is less than 2.

2<1; Returns False, 2 is not less than 1.

Greater Than Or Equal To (>=)/;

1>=2; Returns False, 1 is not greater than or equal to 2.

2>=1; Returns True, 1 is greater than 2.

1>=1; Returns True, 1 is equal to 1.

Less Than Or Equal To (<=):

1<=2; Returns True, 1 is less than 2.

2<=1; Returns False, 2 is not less than or equal to 1.

1<=1; Returns True, 1 is equal to 1.

Is Equal To (==):

1==2; Returns False, 1 is not equal to 2.

1==1; Returns True, 1 is equal to 1.

Is Not Equal To (!=):

1!=2; Returns True, 1 is not equal to 2.

1!=1; Returns False, 1 is not not equal to 1 (1 is equal to 1).

VII. Insertion Operator:
An insertion operator is used to separate different variables, different strings, and different values. Not doing so will result in an error.
Insertion Operator (<<):
*assuming we have the variable a set to the number of clouds in the sky…*

cout<<"There are "<< a <<" clouds in the sky";
if the variable a were equal 5 this would write:

There are 5 clouds in the sky


VIII. Compound Assignment Operators:
Plus and Equal To (+=):

a += 5
This is the same as saying
a = a + 5;

Minus and Equal To (-=):

a -= 5
This is the same as saying
a = a - 5;

IX. Logical Operators:
These are used when evaluating two or more expressions to obtain a single result.

Not (!):

!(1 > 2) Returns True, 1 is not greater than 2, which is false, so it is not false, so it is true.

!(1 == 1) Returns False, 1 is equal to 1, which is true, so it is not true, so it is false

And (&&):
Will only return true if all expressions are true. If one or more is false, it will return false.
*Remember to put another set of parentheses around the whole thing*

((1 > 2) && (1 == 1)) Returns False, the first expression is false, the second is true, therefore the whole thing is false because they are both not true.

((1 < 2) && (1 == 1)) Returns True, because they are both true.

Or (||):
Will return true if any of the expressions are true. If all of them are false, it will return false.
*Remember to put another set of parentheses around the whole thing*

((1 > 2) || (1 == 1)) Returns True, the first expression is false, but the second is true, therefore the whole thing is true.

((1 > 2) && (1 != 1)) Returns False, because they are all false.

Other C++ Tutorials:

C++ Compiler Tutorial:
http://www.villavu.com/forum/showthread.php?t=32498?p=428885#post428885

C++ Variable Tutorial:
http://www.villavu.com/forum/showthread.php?t=32499?p=428888#post428888

C++ Constant Tutorial:
http://www.villavu.com/forum/showthread.php?t=32500?p=428889#post428889

C++ Operator Tutorial:
http://www.villavu.com/forum/showthread.php?t=32501?p=428891#post428891

I program You
07-09-2008, 06:54 PM
Great Man We Needed This!!!! Good Job!!!

Dusk412
07-09-2008, 06:59 PM
Thanks. Posted all 4 at once just to swamp you all mwaha!

I program You
07-09-2008, 07:01 PM
Yeah nice don't expect much comment but believe me some people will be really gratefull ;)

Dusk412
07-09-2008, 07:01 PM
I know I figure people come here to script SCAR mostly. But there will be someone somewhere who will use it. Lol. :D

boberman
07-09-2008, 10:37 PM
:p, ok my last tidbit.

>> and << are actually not insertion and extraction operator (well, they are but stay with me here) they are, in fact, bitshift operators, right and left respectively. basically numbers can be rendered as 1s and zeros. So 5 would be 101 and 10 would be 1010. 10 >> 1 would be 5, and 5 << 1 would be 10 (do you see that?) It has its uses. For example, dividing and multiplying by 2 is fairly easy with the bit shift operator.

On last thing. there is a difference between ++i and i++; the first returns i + 1 the latter returns i and then adds one (on a new line of code). Generally ++i is considered faster (same goes for --i). There is no **i :P (ok, yes there is, but it doesn't do what you are thinking, unless you are thinking pointers)

skilld u
07-09-2008, 10:47 PM
which one should I read first. I don't know anything at all about c++

R0b0t1
07-09-2008, 11:36 PM
Note that:



std::cout.operator<<("Some text");



Is also legal.

Dusk412
07-10-2008, 01:08 AM
I would start in the order that is given at the bottom of all my tutorials. Cuz that is the order I wrote them so sometimes I do not explain something in one because it has already been previously explained.