programming theory 2

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 a nd how they are performed in C++.  Assuming a = 6: Operation Common Operator C++ Example  Addition + >>> a + 2 Result: 8 Subtraction - >>> a - 2 Result: 4 Multiplication * >>> a * 2 Result: 12 Division / >>> a / 2 Result: 3 Modulus (Remainder of division) % >>> a % 2 Result: 0 or >>> a % 4 Result: 2 Increment ++ >>> a++ Result: 7 Decrement -- >>> a-- Result: 5 Comparison Operations Comparison/Relational Operators test for some kind of relati onship between two values.  Assuming a = 6. Operation Common Operator C++ Example Equal To == >>> a == 6 Result: TRUE Not Equal To != >>> a != 6 Result: FALSE Bigger Than > >>> a > 10 Result: FALSE Smaller Than < >>> a < 10 Result: TRUE Bigger or Equal To >= >>> a >= 3 Result: TRUE or >>> a >= 8 Result: FALSE Smaller or Equal To <= >>> a <= 3 Result: FALSE 

Upload: alex-mer

Post on 03-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Theory 2

7/28/2019 Programming Theory 2

http://slidepdf.com/reader/full/programming-theory-2 1/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 Common Operator C++ Example

 Addition + >>> a + 2Result: 8

Subtraction - >>> a - 2Result: 4 

Multiplication * >>> a * 2Result: 12 

Division / >>> a / 2Result: 3

Modulus (Remainder of division)

% >>> a % 2Result: 0

or >>> a % 4Result: 2

Increment ++ >>> a++Result: 7 

Decrement -- >>> a--Result: 5 

Comparison OperationsComparison/Relational Operators test for some kind of relationship between two values. Assuming a = 6.

Operation Common Operator C++ Example

Equal To == >>> a == 6Result: TRUE 

Not Equal To != >>> a != 6Result: FALSE 

Bigger Than > >>> a > 10Result: FALSE 

Smaller Than < >>> a < 10Result: TRUE

Bigger or Equal To >= >>> a >= 3Result: TRUE 

or >>> a >= 8

Result: FALSESmaller or Equal To <= >>> a <= 3

Result: FALSE 

Page 2: Programming Theory 2

7/28/2019 Programming Theory 2

http://slidepdf.com/reader/full/programming-theory-2 2/4

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

+= >>> a += 2a == 8

>>> a = a +2

-= >>> a -= 2a == 4

>>> a = a - 2

*= >>> a *= 2a == 12

>>> a = a * 2

/= >>> a /= 2a == 3

>>> a = a / 2

%= >>> a %= 4a == 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 Result

FALSE FALSE FALSE

FALSE TRUE FALSETRUE FALSE FALSE

TRUE TRUE TRUEResult is TRUE when Condition 1 AND Condition 2 are both TRUE.

OR operator  – Condition 1 OR Condition 2

Condition 1 Condition 2 Result

FALSE FALSE FALSE

FALSE TRUE TRUE

TRUE FALSE TRUETRUE TRUE TRUE

Result is TRUE when Condition 1 OR Condition 2 are either TRUE.

Page 3: Programming Theory 2

7/28/2019 Programming Theory 2

http://slidepdf.com/reader/full/programming-theory-2 3/4

OR operator  – NOT Condition 1

Condition 1 Result 

FALSE TRUE

TRUE FALSEResult is TRUE when Condition 1 is NOT TRUE. Effectively returns the opposite value.

Recap using Python 3.x Examples:>>> a = 10 //Simple assignment 

>>> b = 5 //Simple assignment 

>>> a == 10 //“Equal to” test 

TRUE

>>> b != 5 //“Not equal to” test 

FALSE

>>> b > a //“Bigger than” test 

FALSE

>>> a == 10 AND b == 7 // Are both “a==10” AND “b==7” TRUE? 

FALSE

>>> NOT(a == 10 AND b == 5) //(a == 10 AND b == 5) is TRUE. NOT reverses it.

FALSE

>>> a == 14 OR b == 5 //Is one of “a==14” OR “b==5” TRUE? 

TRUE

>>> NOT(a == 14 OR b == 7) //(a == 14 OR b == 7) is FALSE. NOT reverses it.

TRUE 

>>> a + b //Addition. Note that the variables are unchanged.15

>>> a * b //Multiplication. Variables are unchanged.

50 

>>> a += 2 //a = a + 2. Variable is changed.

>>> a == 12 //a is now equal to 12. 

TRUE

>>> a *= 2 //a = a * 2. Variable is changed.

>>> a == 24 //a is now equal to 24. 

TRUE>>> a -= 14 //a = a - 14. Variable is changed.

>>> a == 10 //a is now equal to 10, back at its original value. 

TRUE

>>> (a - b) == b //a - b is 10 - 5 which is equal to b since b == 5. 

TRUE

>>> ((a - b) != b) OR (a > b) //“(a - b) != b” is FALSE, but “a > b” is TRUE. 

TRUE

>>> ((a - b) != b) AND (a < b) //“(a - b) != b” is FALSE and “a > b” is FALSE. 

FALSE

>>> (a % b) == 0 //Basically checks if b is a factor of a (remainder 0). 

TRUE