Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.

Similar presentations


Presentation on theme: "Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation."— Presentation transcript:

1 Operator

2 Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation always takes place from right to left and never at the inverse. a = b;

3 Example int a, b; // a:? b:? a = 10; // a:10 b:? b = 4; // a:10 b:4 a = b; // a:4 b:4 b = 7; // a:4 b:7

4 Compound assignation operators (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

5 Example 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);

6 Increase and decrease. 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+=1; a=a+1; are all equivalent in its functionality: the three increase by 1 the value of a.

7 Arithmetic operators ( +, -, *, /, % ) The five arithmetical operations supported by the language are: +addition -subtraction *multiplication /division %module

8 Relational Operator ( ==, !=, >, =, <= )

9 Relational operators ==Equal !=Different >Greater than <Less than >=Greater or equal than <=Less or equal than

10 Example (7 == 5)would return false. (5 > 4)would return true. (3 != 2)would return true. (6 >= 6)would return true. (5 < 5)would return false.

11 Example 2 Suppose that a=2, b=3 and c=6: (a == 5)would return false. (a*b >= c)would return true since (2*3 >= 6) is it. (b+4 > a*c)would return false since (3+4 > 2*6) is it. ((b=2) == a)would return true.

12 Logical Operator ( !, &&, || ).

13 NOT Logic operators !(5 == 5)returns false because the expression at its right (5 == 5) would be true. !(6 <= 4)returns true because (6 <= 4) would be false. !truereturns false. !falsereturns true.

14 Logic operators && and || First Operand a Second Operand b result a && b result a || b True False True FalseTrueFalseTrue False

15 Example ( (5 == 5) && (3 > 6) ) returns false ( true && false ). ( (5 == 5) || (3 > 6)) returns true ( true || false ).

16 Conditional operator ( ? ) The conditional operator evaluates an expression and returns a different value according to the evaluated expression, depending on whether it is true or false. Its format is: condition ? result1 : result2 if condition is true the expression will return result1, if not it will return result2.

17 Example 7==5 ? 4 : 3 returns 3 since 7 is not equal to 5. 7==5+2 ? 4 : 3 returns 4 since 7 is equal to 5+2. 5>3 ? a : b returns a, since 5 is greater than 3. a>b ? a : b returns the greater one, a or b.

18 /* Program COMPARES.C */ int main() /* This file will illustrate logical compares */ { int x = 11, y = 11, z = 11; char a = 40, b = 40, c = 40; float r = 12.987, s = 12.987, t = 12.987; /* First group of compare statements */ if (x == y) z = -13; /* This will set z = -13 */ if (x > z) a = 'A'; /* This will set a = 65 */ if (!(x > z)) a = 'B'; /* This will change nothing */ if (b <= c) r = 0.0; /* This will set r = 0.0 */ if (r != s) t = c/2; /* This will set t = 20 */ /* Second group of compare statements */ if (x = (r != s)) z = 1000; /* This will set x = some positive number and z = 1000 */ if (x = y) z = 222; /* This sets x = y, and z = 222 */ if (x != 0) z = 333; /* This sets z = 333 */ if (x) z = 444; /* This sets z = 444 */ /* Third group of compare statements */ x = y = z = 77; if ((x == y) && (x == 77)) z = 33; /* This sets z = 33 */ if ((x > y) || (z > 12)) z = 22; /* This sets z = 22 */ if (x && y && z) z = 11; /* This sets z = 11 */ if ((x = 1) && (y = 2) && (z = 3)) r = 12.00; /* This sets x = 1, y = 2, z = 3, r = 12.00 */ if ((x == 2) && (y = 3) && (z = 4)) r = 14.56; /* This doesn't change anything */ /* Fourth group of compares */ if (x == x); z = 27.345; /* z always gets changed */ if (x != x) z = 27.345; /* Nothing gets changed */ if (x = 0) z = 27.345; /* This sets x = 0, z is unchanged */ return 0; } /* Result of execution (No output from this program.) */

19 // Filename: RELAT1ST.CPP // Prints the results of several relational operations #include void main() { int high = 45; int low = 10; int middle = 25; int answer; answer = high > low; cout low is " << answer << endl; answer = low > high; cout high is " << answer << endl; answer = middle == middle; cout << "Middle == middle is " << answer << endl; answer = high >= middle; cout = middle is " << answer << endl; answer = middle <= low; cout << "Middle <= low is " << answer << endl; answer = 0 == 0; cout << "Bonus relation: 0 == 0 is " << answer << endl; return; }

20 Output High > low is 1 Low > high is 0 Middle == middle is 1 High >= middle is 1 Middle <= low is 0 Bonus relation: 0 == 0 is 1


Download ppt "Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation."

Similar presentations


Ads by Google