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
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