Stuyvesant Computer Programming Club
Operators
Home
Lessons
Topics Covered
Club Policies
Links
Contact Information
Downloads

Operators

Assignation (=)
The assignation operator serves to assign a value to a variable.
a = 5;
assigns the integer value 5 to variable a. We can also do this:
a = b;
this assigns the value of b (whatever it is) to a.
 
Arithmetic operators ( +, -, *, /, % )
The five arithmetical operations supported by the language are:
+ addition
- subtraction
* multiplication
/ division
% mod (remainder)
Looking at this code:(assume, this is in the main function)
int a, b;
b = 10;             //b = 10
a = b + 2;          //a=b+2=10+2=12
cout<< a = << a << and b = << b << endl; 
//will output: a = 12 and b = 10
b =  a * 3;         //b=36
a = 5%3;            /*remainder when 5 divides
                   3, which is 2. Thus, a = 2*/
a = a + 2;          /*yes, we can actually do
                   that. The program first
                   finds the value of a + 2
                   (which is 4) and assigns
                   that value to a, thus a = 4*/
cout<< a = << a << and b = << b << endl; 
//will output: a = 4 and b = 36
There are two types of comments. The // was described before, it comments out one line of code. But what if the comment you want to write is longer than one line? Then you use a block comment, or /* and */. Everything between /* and */ will be commented out, even if it is many lines.
 
Parenthesis
Like in math, if an expression is within a parenthesis, that will be done first. Otherwise, C++ follows PEMDAS. For example:
a = 3 + 5 * 6     //a = 3+30 = 33
a = (3 + 5) * 6   //a = 8 * 6 = 48
 
Compound assignation operators (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
A feature of assignation in C++ are the compound assignation operators (+=, -=, *= and /=), which allow to modify the value of a variable with one of the basic operators:
value += increase;  
//is equivalent to value = value + increase;
a -= 5;   //is equivalent to a = a - 5;
a /= b;   //is equivalent to a = a / b;
price *= units + 1;  
//is equivalent to price = price * (units + 1);
 
Increment and decrement
Another example of saving language when writing code are the increase operator (++) and the decrease operator (--). They increase or reduce by 1 the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus:
a++;
++a;
a+=1;
a=a+1;

are all equivalent in its functionality: the three increase by 1 the value of a.
There is a difference between the prefix operator (++a) and suffix operator (a++). The prefix operator, adds the one first and then gives the value. The suffix operator gives the value first and then adds one. For example:
int a, b;
b = 3;
a = ++b;
cout<< a = << a << and b = << b << endl; 
//will output: a = 4 and b = 4
here, b is given 3. the ++b adds one to b, thus b = 4. now, once b = 4, bs value is given to a, thus a = 4.
int a, b;
b = 3;
a = b++;
cout<< a = << a << and b = << b << endl; 
//will output: a = 3 and b = 4
here, b is given 3. bs value is first given to a, thus a = 3, AND THEN the one is added to b, thus b = 4. Thus the two methods will yield different results.
 
Relational operators ( ==, !=, >, <, >=, <= )
In order to evaluate a comparison between two expressions we can use the Relational operators. It will return 1 if true and 0 if false.
We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other. Here is a list of the relational operators that can be performed in C++:
== Equal
!= Not equal
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
 
For example:
(1 == 2)  //is false
(1 < 2)   //is false
(2 < 2)   //is false
(2 <= 2)  //is true
(5 != 2)  //is true
Supposing a = 2, b = 3, and c = 6
(a == 5)      //is false
(a == 2)      //is true
((a + b)== 5) //is true
(a*b >= c)    //is true
Note that the operator = (one equal sign) is not the same as operator == (two equal signs), the first is an assignation operator (assigns the right side of the expression to the variable in the left) and the other (==) is a relational operator of equality that compares whether both expressions in the two sides of the operator are equal to each other. Thus, if you have ((b=2) == a), we first assigned the value 2 to b and then we compared it to a, that also stores value 2, so the result of the operation is true.
 
Logic operators ( !, &&, || )
Going back to MQ4 (for whatever it is nowadays), we have the logic operators. ! is the same is not. && is the same as and. || (two pikes) is the same as or.
(5 == 5)  //returns true
!(5 == 5) //returns false
Remember the logic tables?
   a        b    a&&b   a||b
True   True   True   True
True   False  False  True
False  True   False  True
False  False  False   False
 
For example:
((5 == 5) && (3 > 6)) returns false (true && false).
((5 == 5) || (3 > 6)) returns true (true || false).

Enter supporting content here