Site hosted by Angelfire.com: Build your free website today!

 

OPERATIONS

TABLE 4.1

OPERATION SIGN EXAMPLE
MULTIPLICATION   *   int answer = 22 * 2
DIVISION   /   int answer = 22 / 2
ADDITION   +   int answer = 22 + 2
SUBTRACTION   -   int answer = 22 - 2

TABLE 4.2

FUNDAMENTALS:

  • Compiler
  • Statements
  • Data Types
  • Operations
  • Memory

 

 

OPERATORS SIGN EXAMPLE
EQUALITY   ==   5 == 3 + 2
INEQUALITY   !=   6 != 3 + 2
AND   &&   math && physics
OR   ||   math || physics
LESS THAN   <   5 < 10
GREATER THAN   >   10 > 5
LESS THAN OR EQUAL TO   <=   5 <= 5
GREATER THAN OR EQUAL TO   >=   5 < 5
INCREMENT   ++   ++x
DECREMENT   --   --x

NOTE: Increment and Decrement operators work with int only.

Multiplicaton and Division (from right to left) take precedence over addition and subtraction (from right to left). Bear in mind that int answer = any number (operation) any number is an example of an assignment statement. Remember that for assignment statements, the value on the right is assigned to the value on the left.

If you had multiple operations you would have to establish precedence using parenthesis. For example, if you had (5*5+7-8)/2*6-4, It would be best to write it like this int answer = (((((5*5)+7-8)/2)*6)-4).

You could assign a variable to hold the values of the operations:

int number1 = num1 + num2;float number_2 = num1 * num2;int number3 = num1++;int number_4 = num1--;

You could also add a number to a value:

int num1 = 5;
int num2 = 2;
int num3 = 0;
int num4 = 0;
int num1 = num1 + 1; The value is now 5 + 1 which is 6
int num2 = num2 + num1; The value is now 5 + 2 which is 7
int number3 = num1++; The value is now 3 + 1 which is 4
int number_4 = num1--; The value is now 4 - 1 which is 3

FIGURE 4.1

#include<iostream.h>
int main(){    float num1, num2;
    cout  <<  "\nType in a number: ";    cin >> num1;    cout  <<  "\nType in another number: ";    cin >> num2;
    if (num1 == num2)    {        cout  <<  "\nThe numbers are equal!";    }    if (num1 != num2)    {        cout  <<  "\nThe numbers are not equal!";    }    if (num1 > num2)    {        cout  <<  endl  <<  num1  << " is greater than "  <<  num2;    }    if (num1 < num2)    {        cout  <<  endl  <<  num1  << " is less than "  <<  num2;    }    cout  << "\nI will now add one to "  <<  num1  <<  " and subtract one from "  <<  num2;    cout  <<  endl  <<  ++num1;    cout  <<  endl  <<  --num2;    cout  << "\nI will now add "  <<  num1  <<  " and "  <<  num2;    cout  <<  endl  <<  num1 + num2;    cout  << "\nI will now multiply "  <<  num1  <<  " and "  <<  num2;    cout  <<  endl  <<  num1 * num2;    cout  << "\nI will now subtract "  <<  num1  <<  " from "  <<  num2;    cout  <<  endl  <<  num1 - num2;
    return(0);}

++num1 VS num1++

Placing the increment operator before a value ie ++num1 evaluates the expression BEFORE it is incremented, placing the increment operator after a value evaluates the expression AFTER it is incremented. The explanation of the code in Figure 4.2 explains this.

--num1 VS num1--

Same as above but involving subtraction. See explanation of code in Figure 4.2.

FIGURE 4.2

#include<iostream.h>
int main(){    int num1, num2 = 10;
    int value1 = 5+(num1++);    cout << "\nValue1 is: " << value1 << " num1 is: " << num1;
    num1 = 10;    int value2 = (num1++)+5;    cout << "\nValue2 is: " << value2 << " num1 is: " << num1;
    num1 = 10;    int value3 = 5+(++num1);    cout << "\nValue3 is: " << value3 << " num1 is: " << num1;
    num1 = 10;    int value4 = (++num1)+5;    cout << "\nValue4 is: " << value4 << " num1 is: " << num1;
    int value5 = 5+(num2--);    cout << "\nValue5 is: " << value5 << " num2 is: " << num2;
    num2 = 10;    int value6 = 5+(num2--);    cout << "\nValue6 is: " << value6 << " num2 is: " << num2;
    num2 = 10;    int value7 = 5+(--num2);    cout << "\nValue7 is: " << value7 << " num2 is: " << num2;
    num2 = 10;    int value8 = 5+(--num2);    cout << "\nValue8 is: " << value8 << " num2 is: " << num2;
    return(0);}

Was the output:
Value1 is:15 Num1 is: 11Value2 is:15 Num1 is: 11Value3 is:16 Num1 is: 11Value4 is:16 Num1 is: 11Value5 is:15 Num1 is: 9Value6 is:15 Num1 is: 9Value7 is:14 Num1 is: 9Value8 is:14 Num1 is: 9

Let's examine the code:
    int num1, num2 = 10;
    int value1 = 5+(num1++);    cout << "\nValue1 is: " << value1 << " num1 is: " << num1;

First we initialised num1 to 10. We set up value1 to equal the result of the expression. We stated before that any variable followed by ++ means to evaluate the expression THEN increment. For this example, it means we are to evaluate the expression: 5 + num1 which is (5 + 10) = 15, THEN increment--> 15 + 1 = 16. Num1 remains an incremented value which is 10 + 1 = 11

    num1 = 10;    int value2 = (num1++)+5;    cout << "\nValue2 is: " << value2 << " num1 is: " << num1;

Notice the code states num1 = 10. This restores num1 back to the original value we assigned to it because in the last block of code, we incremented num1 to 11. Also notice that the +5 is placed after the parenthesis. This does not affect the order of operations because the what is inside the parenthesis is always evaluated first. In this case, num1 is inside the parenthesis. As we stated before, if the increment operator is after the variable, the expression is evaluated first, then the value is incremented, think of it like this: (10 + 5)++. Here it is (10 + 5) = 15 then increment 15 + 1 = 16

    num1 = 10;    int value2 = 5+(++num1);    cout << "\nValue3 is: " << value3 << " num1 is: " << num1;

Here we have the increment operator before the variable. This increments the value, THEN evaluates the expression. First we evaluate what is in the parenthesis which happens to be ++num1. Because ++ before the variable tells us to increment before we evaluate, we increment num1: 10 + 1 = 11. Now we evaluate: 11 + 5 = 16, and num1 remains 11. The same rules apply for decrement, but instead of addition, it is replaced by subtraction.

 

 

Data Types Strings I/OStream Structures Classes Operations Memory Branches Loops Functions Pointers Statements Compiler ADT Arrays Linked Lists Message Board Search Email Home

 
Site Credits| Questions and Enquiries | © 2001 | Contact