You are on page 1of 4

Operations

Operators vary from language to language but most languages have different operators that perform similar operations.

Arithmetic Operations
Here are some fundamental arithmetic operators and how they are performed in C++. Assuming a = 6: Operation Addition Subtraction Multiplication Division Modulus (Remainder of division) Common Operator + * / % C++ Example >>> a + 2 Result: 8 >>> a - 2 Result: 4 >>> a * 2 Result: 12 >>> a / 2 Result: 3 >>> a % 2 Result: 0 or >>> a % 4 Result: 2 >>> a++ Result: 7 >>> a-Result: 5

Increment Decrement

++ --

Comparison Operations
Comparison/Relational Operators test for some kind of relationship between two values. Assuming a = 6. Operation Equal To Not Equal To Bigger Than Smaller Than Bigger or Equal To Common Operator == != > < >= C++ Example >>> a == 6 Result: TRUE >>> a != 6 Result: FALSE >>> a > 10 Result: FALSE >>> a < 10 Result: TRUE >>> a >= 3 Result: TRUE or >>> a >= 8 Result: FALSE >>> a <= 3 Result: FALSE

Smaller or Equal To

<=

Assignment Operations
Assignment operators assign values to variables. The basic assignment operator in most languages is the equals sign =. The below examples are compound assignment operators which perform an arithmetic operation and then automatically assign the result to the variable. Assuming a = 6. C++ Operator += -= *= /= %= C++ Example >>> a += 2 a == 8 >>> a -= 2 a == 4 >>> a *= 2 a == 12 >>> a /= 2 a == 3 >>> a %= 4 a == 2 Same as >>> a = a +2 >>> a = a - 2 >>> a = a * 2 >>> a = a / 2 >>> a = a % 4

Boolean Algebra and Logic Operations


Boolean algebra utilises Boolean data types as discussed in the previous lesson. Boolean operations are the same as logic gate operations utilised in electronics/physics, except derived operators like XOR, NOR, NAND, etc. are not utilised, as there is no need, considering those are derived from the three basic operators that will be discussed below. The logic is pseudocodal so the actual operators may vary by language.

Truth Tables
Truth tables are used to show every possible outcome of an operation. AND operator Condition 1 AND Condition 2 Condition 1 Condition 2 FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE Result is TRUE when Condition 1 AND Condition 2 are both TRUE. OR operator Condition 1 OR Condition 2 Condition 1 Condition 2 FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE Result is TRUE when Condition 1 OR Condition 2 are either TRUE.

Result FALSE FALSE FALSE TRUE

Result FALSE TRUE TRUE TRUE

OR operator NOT Condition 1 Condition 1 Result FALSE TRUE TRUE FALSE Result is TRUE when Condition 1 is NOT TRUE. Effectively returns the opposite value.

Recap using Python 3.x Examples:


>>> a = 10 >>> b = 5 >>> a == 10 TRUE >>> b != 5 FALSE >>> b > a FALSE >>> a == 10 AND b == 7 FALSE >>> NOT(a == 10 AND b == 5) FALSE >>> a == 14 OR b == 5 TRUE >>> NOT(a == 14 OR b == 7) TRUE >>> a + b 15 >>> a * b 50 >>> a += 2 >>> a == 12 TRUE >>> a *= 2 >>> a == 24 TRUE >>> a -= 14 >>> a == 10 TRUE >>> (a - b) == b TRUE >>> ((a - b) != b) OR (a > b) TRUE >>> ((a - b) != b) AND (a < b) FALSE >>> (a % b) == 0 TRUE //Simple assignment //Simple assignment //Equal to test //Not equal to test //Bigger than test //Are both a==10 AND b==7 TRUE? //(a == 10 AND b == 5) is TRUE. NOT reverses it. //Is one of a==14 OR b==5 TRUE? //(a == 14 OR b == 7) is FALSE. NOT reverses it. //Addition. Note that the variables are unchanged. //Multiplication. Variables are unchanged. //a = a + 2. Variable is changed. //a is now equal to 12. //a = a * 2. Variable is changed. //a is now equal to 24. //a = a - 14. Variable is changed. //a is now equal to 10, back at its original value. //a - b is 10 - 5 which is equal to b since b == 5. //(a - b) != b is FALSE, but a > b is TRUE. //(a - b) != b is FALSE and a > b is FALSE. //Basically checks if b is a factor of a (remainder 0).

Sources
http://en.wikipedia.org/wiki/Boolean_algebra http://www.amazon.com/Cambridge-International-Computing-Coursebook-Examinations/dp/0521186625 http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

You might also like