Operator - PowerPoint PPT Presentation

About This Presentation
Title:

Operator

Description:

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

Number of Views:35
Avg rating:3.0/5.0
Slides: 21
Provided by: Abi112
Category:

less

Transcript and Presenter's Notes

Title: Operator


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 // a10 b?
  • b 4 // a10 b4
  • a b // a4 b4
  • b 7 // a4 b7

4
Compound assignation operators
  • (, -, , /, , gtgt, ltlt, , , )

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 a1 aa1
  • 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
  • ( , !, gt, lt, gt, lt )

9
Relational operators
  • Equal
  • !Different
  • gtGreater than
  • ltLess than
  • gtGreater or equal than
  • ltLess or equal than

10
Example
  • (7 5)would return false.
  • (5 gt 4)would return true.
  • (3 ! 2)would return true.
  • (6 gt 6)would return true.
  • (5 lt 5)would return false.

11
Example 2
  • Suppose that a2, b3 and c6
  • (a 5)would return false.
  • (ab gt c)would return true since (23 gt 6) is
    it.
  • (b4 gt ac)would return false since (34 gt 26)
    is it.
  • ((b2) 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 lt 4)returns true because (6 lt 4) would be
    false.
  • !truereturns false.
  • !falsereturns true.

14
Logic operators and
FirstOperanda SecondOperandb resulta b resulta b
True True True True
True False False True
False True False True
False False False False
15
Example
  • ( (5 5) (3 gt 6) ) returns false ( true
    false ).
  • ( (5 5) (3 gt 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
  • 75 ? 4 3  returns 3 since 7 is not equal to
    5.
  • 752 ? 4 3  returns 4 since 7 is equal to
    52.
  • 5gt3 ? a b  returns a, since 5 is greater than
    3.
  • agtb ? 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 gt z) a 'A' / This will set a 65
    /
  • if (!(x gt z)) a 'B' / This will change
    nothing /
  • if (b lt 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 /

19
  • // Filename RELAT1ST.CPP
  • // Prints the results of several relational
    operations
  • include ltiostream.hgt
  • void main()
  • int high 45
  • int low 10
  • int middle 25
  • int answer
  • answer high gt low
  • cout ltlt "High gt low is " ltlt answer ltlt endl
  • answer low gt high
  • cout ltlt "Low gt high is " ltlt answer ltlt endl
  • answer middle middle
  • cout ltlt "Middle middle is " ltlt answer ltlt endl

20
Output
  • High gt low is 1
  • Low gt high is 0
  • Middle middle is 1
  • High gt middle is 1
  • Middle lt low is 0
  • Bonus relation 0 0 is 1
Write a Comment
User Comments (0)
About PowerShow.com