Your SlideShare is downloading. ×
0
OPERATORS BY: Kalpana & Milan
Operators Introduction  <ul><li>An operator is a symbol which helps the user to command the computer to do a certain mathe...
‘ C’ Has A Set Of Operators Which Can Be Classified As  <ul><li>1. Arithmetic operators  2. Relational Operators  3. Logic...
1. Arithmetic Operators   <ul><li>All the basic arithmetic operations can be carried out in C.  All the operators have alm...
<ul><li>Arithmetic Operators Used for performing numeric calculations
There are unary, binary, and ternary operators  </li></ul><ul><ul><li>unary (1 operand)   -5
binary (2 operands)  13 - 7
ternary (3 operands)  exp1 ? exp2 : exp3 </li></ul></ul>For example:  a = b + c;  denote an expression in which there are ...
Unary <ul><li>Having a single operand
There are two arithmetic unary operators: + and -
Examples:
x = -y; a = +b; The  not  logical operator is also a unary operator.
Operator & its   Use:
! Negation,
++ increment by 1,
-- decrement by 1 </li></ul>
Binary <ul><li>Having two operands
There are several math operators that require two operands: + - * / %
Examples: total = x * y + 2;  </li></ul>
<ul><li>Operator & its Use
+ addition,
- subtraction,
* multiplication,
/ division,
% remainder division or modulus,
= assignment,
> greater than,
< less than,
&&  AND,
Upcoming SlideShare
Loading in...5
×

Operators

1,706

Published on

Published in: Education, Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,706
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
133
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Operators"

  1. 1. OPERATORS BY: Kalpana & Milan
  2. 2. Operators Introduction <ul><li>An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C language program to operate on data and variables. </li></ul>
  3. 3. ‘ C’ Has A Set Of Operators Which Can Be Classified As <ul><li>1. Arithmetic operators 2. Relational Operators 3. Logical Operators 4. Assignment Operators 5. Increments and Decrement Operators 6. Conditional Operators 7. Bitwise Operators 8. Special Operators </li></ul>
  4. 4. 1. Arithmetic Operators <ul><li>All the basic arithmetic operations can be carried out in C. All the operators have almost the same meaning as in other languages. Both unary and binary operations are available in C language . Unary operations operate on a single operand, therefore the number 5 when operated by unary – will have the value –5. </li></ul>
  5. 5. <ul><li>Arithmetic Operators Used for performing numeric calculations
  6. 6. There are unary, binary, and ternary operators </li></ul><ul><ul><li>unary (1 operand) -5
  7. 7. binary (2 operands) 13 - 7
  8. 8. ternary (3 operands) exp1 ? exp2 : exp3 </li></ul></ul>For example: a = b + c; denote an expression in which there are 3 operands a, b, c and two operator + and = .
  9. 9. Unary <ul><li>Having a single operand
  10. 10. There are two arithmetic unary operators: + and -
  11. 11. Examples:
  12. 12. x = -y; a = +b; The not logical operator is also a unary operator.
  13. 13. Operator & its Use:
  14. 14. ! Negation,
  15. 15. ++ increment by 1,
  16. 16. -- decrement by 1 </li></ul>
  17. 17. Binary <ul><li>Having two operands
  18. 18. There are several math operators that require two operands: + - * / %
  19. 19. Examples: total = x * y + 2; </li></ul>
  20. 20. <ul><li>Operator & its Use
  21. 21. + addition,
  22. 22. - subtraction,
  23. 23. * multiplication,
  24. 24. / division,
  25. 25. % remainder division or modulus,
  26. 26. = assignment,
  27. 27. > greater than,
  28. 28. < less than,
  29. 29. && AND,
  30. 30. || OR </li></ul>
  31. 31. Ternary <ul><li>Having three operands
  32. 32. There is only one ternary operator that requires three operands.
  33. 33. It's the conditional operator:
  34. 34. “ ? :”
  35. 35. We will study this in Conditional operator. </li></ul>
  36. 36. Operator Meaning Example Answer + Addition ans = 7 + 3; 10 - Subtraction ans = 7 - 3; 4 * Multiplication ans = 7 * 3; 21 / Division ans = 7 / 3; 2 % Modulus Operator ans = 7 % 3; 1 Cont... Arithmetic Operators
  37. 37. / Operator <ul><li>Division operator ( /) performs integer division if both operands are integers </li></ul>13 / 5; // displays 2 2 / 4; // displays 0 <ul><li>If either operand is floating-point, the result is floating-point </li></ul>13 / 5.0; // displays 2.6 2.0 / 4; // displays 0.5
  38. 38. % Operator <ul><li>Modulus operator ( % ) computes the remainder resulting from integer division </li></ul>9 % 2; // displays 1 <ul><li>Modulus requires integers for both operands </li></ul>9 % 2.0; // error
  39. 39. 2. Relational Operators <ul><li>Often it is required to compare the relationship between operands and bring out a decision and program accordingly. This is when the relational operator come into picture. C supports the following relational operators. </li></ul>
  40. 40. Operator Meaning < less than <= less than or equal to > greater than >= greater than or equal to == equal to != not equal to
  41. 41. <ul><li>A simple relational expression contains only one relational operator and takes the following form.
  42. 42. exp1 relational operator exp2 Where exp1 and exp2 are expressions, which may be simple constants, variables or combination of them. Given below are some examples of relational expressions .
  43. 43. 6.5 <= 25 TRUE -65 > 0 FALSE 10 < 7 + 5 TRUE
  44. 44. Relational expressions are used in decision making statements of C language such as if, while and for statements. </li></ul>
  45. 45. 3. Logical Operators <ul><li>C has some logical operators. </li></ul>Operator Meaning && Logical AND || Logical OR ! Logical NOT
  46. 46. Logical AND (&&) <ul><li>This operator is used to evaluate 2 conditions. If both the expressions to the left and to the right of the logical operator is true then the whole compound expression is true. Example
  47. 47. True && True= True False && True= False True && False =False False && False =False </li></ul>
  48. 48. Logical OR (||) <ul><li>The logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2 expressions is true. Example </li></ul><ul><li>True || True =True False || True =True True || False =True False || False =False </li></ul>
  49. 49. Logical NOT (!) <ul><li>The logical not operator takes single expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it just reverses the value of the expression. For example
  50. 50. ! True =False ! False=True </li></ul>
  51. 51. 4. Assignment Operators <ul><li>The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression. </li></ul><ul><li>Example:
  52. 52. x + = 1 is same as x = x + 1
  53. 53. The commonly used shorthand assignment operators are as follows: </li></ul>
  54. 54. Statement with simple assignment operator Statement with shorthand operator a = a + 1 a += 1 a = a – 1 a -= 1 a = a * (n+1) a *= (n+1) a = a / (n+1) a /= (n+1) a = a % b a %= b
  55. 55. 5. Increment and Decrement Operators <ul><li>The increment (++) and decrement (--) operators simply add 1 or subtract 1 from a variable and return the value of that variable. These are unary operators meaning they work on only one operand. </li></ul>
  56. 56. <ul><li>There are two ways to use these operators – prefix or postfix, that means you can put the operator in front of the variable like ++a or behind the variable like b-- respectively.
  57. 57. In prefix notation the variable is incremented or decremented first, then the new value is returned.
  58. 58. In postfix notation the variable's original value is returned, and the addition or subtraction happens later. </li></ul>
  59. 59. 6. Conditional Operator <ul><li>The conditional operator is unusual in that it takes three operands. The syntax of this operator is like this:
  60. 60. Condition ? Expression1 : Expression2;
  61. 61. You can think of the conditional operator as if it were a function that works like this: </li></ul>
  62. 62. <ul><li>if ( Condition ) return Expression1; else return Expression2; </li></ul><ul><li>The Condition expression must evaluate to true or false. If condition is true Expression1 is evaluated and its value is returned. If Condition is false Expression2 is evaluated and its value is returned. </li></ul>
  63. 63. 7. Bitwise Operators <ul><li>Bitwise operators work on the bits stored in integral types. They work similar to the logical operators except that instead of working on true and false values they work with ones and zeroes. There are several bitwise operators available:
  64. 64. Symbol and their Meaning: ~ Complement & And | Or ^ Exclusive-Or << Left shift >> Right shift </li></ul>
  65. 65. <ul><li>The complement operator is a unary operator. The other bitwise operators are binary, taking two arguments. Every bit that is 1 in the operand is 0 in the result.
  66. 66. The exclusive-OR (XOR), it performs same as logical OR operator: </li></ul>The result is 1 only when either X is equal to 1 or Y is equal to 1, but not when both X and Y are equal to 1.
  67. 67. 8. Special Operators <ul><li>The [] operator is used to access elements of an array. The syntax is like this:
  68. 68. int a[5] = { 1, 2, 3, 4, 5 }; int b = a[3];
  69. 69. On the second line the a[3] accesses the fourth element of the array and assigns b to the value 4. </li></ul>
  70. 70. THANKU
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×