SlideShare une entreprise Scribd logo
1  sur  21
An operator is a symbol that
tells the compiler to perform
  specific mathematical or
    logical manipulations.
C++   is    rich   in   built-in
operators and provides following
type of operators:
    •Unary Operator
    •Arithmetic Operators
    •Relational Operators
    •Logical Operators
    •Bitwise Operators
    •Assignment Operators
    •Misc Operators
Unary Operator Requires single operand
item to perform operation
Assume variable A holds 10 then:


Operator   Description                Example
                                      A++ or ++A will give
++         Adds two operands
                                      11

           Subtracts second operand
--                                  A-- or --A will give 9
           from the first
There   are    following   arithmetic                      operators
supported by C++ language:
Assume variable A holds 10 and variable B holds
20 then:
Operator   Description                         Example
+          Adds two operands                   A + B will give 30
           Subtracts second operand from the
-                                            A - B will give -10
           first
*          Multiply both operands              A * B will give 200
/          Divide numerator by de-numerator B / A will give 2
           Modulus Operator and remainder
%                                              B % A will give 0
           of after an integer division
There   are    following   relational   operators
supported by C++ language.
Assume variable A holds 10 and variable B holds
20 then:
Opera
      Description                                                              Example
tor

      Checks if the value of two operands is equal or not, if yes then         (A == B) is
==
      condition becomes true.                                                  not true.

      Checks if the value of two operands is equal or not, if values are not (A != B) is
!=
      equal then condition becomes true.                                     true.


      Checks if the value of left operand is greater than the value of right   (A > B) is
 >
      operand, if yes then condition becomes true.                             not true.


      Checks if the value of left operand is less than the value of right      (A < B) is
 <
      operand, if yes then condition becomes true.                             true.

      Checks if the value of left operand is greater than or equal to the      (A >= B) is
 >=
      value of right operand, if yes then condition becomes true.              not true.

      Checks if the value of left operand is less than or equal to the value   (A <= B) is
 <=
      of right operand, if yes then condition becomes true.                    true.
There are following logical operators supported
by C++ language.
 Assume variable A holds 1 and variable B holds 0
 then:
 Operator Description                                                   Example

          Called Logical AND operator. If both the operands are non
 &&                                                                     (A && B) is false.
          zero then condition becomes true.


          Called Logical OR Operator. If any of the two operands is
 ||                                                                     (A || B) is true.
          non zero then condition becomes true.


          Called Logical NOT Operator. Use to reverses the logical
 !        state of its operand. If a condition is true then Logical NOT !(A && B) is true.
          operator will make false.
The Bitwise operators supported by C++ language
    are listed in the following table.
    Assume variable A holds 60 and variable B
    holds 13 then:
Operator   Description                                                                 Example
           Binary AND Operator copies a bit to the result if it exists in both         (A & B) will give 12 which is
&
           operands.                                                                   0000 1100
                                                                                       (A | B) will give 61 which is
|          Binary OR Operator copies a bit if it exists in either operand.
                                                                                       0011 1101
           Binary XOR Operator copies the bit if it is set in one operand but not      (A ^ B) will give 49 which is
^
           both.                                                                       0011 0001
           Binary Ones Complement Operator is unary and has the effect of              (~A ) will give -60 which is
~
           'flipping' bits.                                                            1100 0011

           Binary Left Shift Operator. The left operands value is moved left by the    A << 2 will give 240 which is
<<
           number of bits specified by the right operand.                              1111 0000


           Binary Right Shift Operator. The left operands value is moved right by the A >> 2 will give 15 which is
>>
           number of bits specified by the right operand.                             0000 1111
There   are    following   assignment   operators
supported by C++ language:
Oper
     Description                                                                            Example
ator

     Simple assignment operator, Assigns values from right side operands to left side       C = A + B will assign
=
     operand                                                                                value of A + B into C
     Add AND assignment operator, It adds right operand to the left operand and assign the C += A is equivalent to
+=
     result to left operand                                                                C=C+A
     Subtract AND assignment operator, It subtracts right operand from the left operand     C -= A is equivalent to
-=
     and assign the result to left operand                                                  C=C-A
     Multiply AND assignment operator, It multiplies right operand with the left operand    C *= A is equivalent to
*=
     and assign the result to left operand                                                  C=C*A
     Divide AND assignment operator, It divides left operand with the right operand and     C /= A is equivalent to
/=
     assign the result to left operand                                                      C=C/A
     Modulus AND assignment operator, It takes modulus using two operands and assign        C %= A is equivalent
%=
     the result to left operand                                                             to C = C % A
                                                                                            C <<= 2 is same as C =
<<= Left shift AND assignment operator
                                                                                            C << 2
                                                                                            C >>= 2 is same as C =
>>= Right shift AND assignment operator
                                                                                            C >> 2
                                                                                            C &= 2 is same as C =
&=   Bitwise AND assignment operator
                                                                                            C&2
                                                                                            C ^= 2 is same as C =
^=   bitwise exclusive OR and assignment operator
                                                                                            C^2
                                                                                            C |= 2 is same as C = C
|=   bitwise inclusive OR and assignment operator
                                                                                            |2
There are few other operators supported by C++ Language.
Operator            Description
                    Size of operator returns the size of a variable. For example size of(a), where a is
Size of
                    integer, will return 4.
                    Conditional operator. If Condition is true ? then it returns value X : otherwise
Condition ? X : Y
                    value Y
                    Comma operator causes a sequence of operations to be performed. The value of
,                   the entire comma expression is the value of the last expression of the comma-
                    separated list.
. (dot) and ->      Member operators are used to reference individual members of classes,
(arrow)             structures, and unions.
                    Casting operators convert one data type to another. For example, int(2.2000)
Cast
                    would return 2.
                    Pointer operator & returns the address of an variable. For example &a; will give
&
                    actual address of the variable.
                    Pointer operator * is pointer to a variable. For example *var; will pointer to a
*
                    variable var.
*Expression in C++ is form when we combine
operands (variables and constant) and C++
Operators.“
*Expression can also be defined as:
"Expression in C++ is a combination of
Operands and Operators.“
*Operands in C++ Program are those values
on which we want to perform perform
operation.
"An expression in which arithmetic
operators   are   used  is  called
arithmetic expression“.
For example an arithmetic expression
is look just like that a+b=5
"A relational operator with constants and
variables makes relational expression or An
expressions in which relational operators are use
is called relational expression.
Points about relational operators :-
1.Relational operators are use to compare values.
2.All the expressions evaluates from left to
right.
3.There are six relational operators in C++
programming (>,<,>=,<=,==,!=).
4.These operators evaluates results true or
false.
5.False statement represent by 0 and True
statement represent by 1.
6.These operators evaluate at statement level and
has no preference.
1. There are three logical operators And( && ),or( || ) these two
both are binary operator and not( ! ) is u nary operator.
2. More than one relation expression are combine by using logical
                             operators.
3. The expression will evaluate from left to right if more than one
relation expression are use.
Example 1 . write the corresponding C++
expression for the following mathematical
expression ?
1.2 – ye2y + 4y
2.P + q /(r + s)4
Solution = 1. 2-y*exp(2*y)+4*y
          = 2. p+q/ pow ((r+s),4)
Operators and Expression
Operators and Expression

Contenu connexe

Tendances

C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
vinay arora
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
dishti7
 

Tendances (20)

Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
 
11operator in c#
11operator in c#11operator in c#
11operator in c#
 
Operators in C/C++
Operators in C/C++Operators in C/C++
Operators in C/C++
 
Operators
OperatorsOperators
Operators
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
 
operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++
 
operators in c++
operators in c++operators in c++
operators in c++
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
C# operators
C# operatorsC# operators
C# operators
 
Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
 
Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
C operator and expression
C operator and expressionC operator and expression
C operator and expression
 
Operators
OperatorsOperators
Operators
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
 
Constructor and destructors
Constructor and destructorsConstructor and destructors
Constructor and destructors
 

Similaire à Operators and Expression

C programming operators
C programming operatorsC programming operators
C programming operators
Suneel Dogra
 

Similaire à Operators and Expression (20)

Session03 operators
Session03 operatorsSession03 operators
Session03 operators
 
07 ruby operators
07 ruby operators07 ruby operators
07 ruby operators
 
itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in java
 
Bit shift operators
Bit shift operatorsBit shift operators
Bit shift operators
 
java operators
 java operators java operators
java operators
 
Python operators part2
Python operators part2Python operators part2
Python operators part2
 
Programming C Part 02
Programming C Part 02Programming C Part 02
Programming C Part 02
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
Operator 04 (js)
Operator 04 (js)Operator 04 (js)
Operator 04 (js)
 
C programming operators
C programming operatorsC programming operators
C programming operators
 
Python : basic operators
Python : basic operatorsPython : basic operators
Python : basic operators
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
Operators and it's type
Operators and it's type Operators and it's type
Operators and it's type
 
C operators
C operatorsC operators
C operators
 
Opeartor &amp; expression
Opeartor &amp; expressionOpeartor &amp; expression
Opeartor &amp; expression
 
Computer programming 2 Lesson 7
Computer programming 2  Lesson 7Computer programming 2  Lesson 7
Computer programming 2 Lesson 7
 
C# Fundamentals - Basics of OOPS - Part 2
C# Fundamentals - Basics of OOPS - Part 2C# Fundamentals - Basics of OOPS - Part 2
C# Fundamentals - Basics of OOPS - Part 2
 
Java unit1 b- Java Operators to Methods
Java  unit1 b- Java Operators to MethodsJava  unit1 b- Java Operators to Methods
Java unit1 b- Java Operators to Methods
 
COM1407: C Operators
COM1407: C OperatorsCOM1407: C Operators
COM1407: C Operators
 

Dernier

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 

Dernier (20)

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 

Operators and Expression

  • 1.
  • 2. An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
  • 3. C++ is rich in built-in operators and provides following type of operators: •Unary Operator •Arithmetic Operators •Relational Operators •Logical Operators •Bitwise Operators •Assignment Operators •Misc Operators
  • 4. Unary Operator Requires single operand item to perform operation Assume variable A holds 10 then: Operator Description Example A++ or ++A will give ++ Adds two operands 11 Subtracts second operand -- A-- or --A will give 9 from the first
  • 5. There are following arithmetic operators supported by C++ language: Assume variable A holds 10 and variable B holds 20 then: Operator Description Example + Adds two operands A + B will give 30 Subtracts second operand from the - A - B will give -10 first * Multiply both operands A * B will give 200 / Divide numerator by de-numerator B / A will give 2 Modulus Operator and remainder % B % A will give 0 of after an integer division
  • 6. There are following relational operators supported by C++ language. Assume variable A holds 10 and variable B holds 20 then:
  • 7. Opera Description Example tor Checks if the value of two operands is equal or not, if yes then (A == B) is == condition becomes true. not true. Checks if the value of two operands is equal or not, if values are not (A != B) is != equal then condition becomes true. true. Checks if the value of left operand is greater than the value of right (A > B) is > operand, if yes then condition becomes true. not true. Checks if the value of left operand is less than the value of right (A < B) is < operand, if yes then condition becomes true. true. Checks if the value of left operand is greater than or equal to the (A >= B) is >= value of right operand, if yes then condition becomes true. not true. Checks if the value of left operand is less than or equal to the value (A <= B) is <= of right operand, if yes then condition becomes true. true.
  • 8. There are following logical operators supported by C++ language. Assume variable A holds 1 and variable B holds 0 then: Operator Description Example Called Logical AND operator. If both the operands are non && (A && B) is false. zero then condition becomes true. Called Logical OR Operator. If any of the two operands is || (A || B) is true. non zero then condition becomes true. Called Logical NOT Operator. Use to reverses the logical ! state of its operand. If a condition is true then Logical NOT !(A && B) is true. operator will make false.
  • 9. The Bitwise operators supported by C++ language are listed in the following table. Assume variable A holds 60 and variable B holds 13 then: Operator Description Example Binary AND Operator copies a bit to the result if it exists in both (A & B) will give 12 which is & operands. 0000 1100 (A | B) will give 61 which is | Binary OR Operator copies a bit if it exists in either operand. 0011 1101 Binary XOR Operator copies the bit if it is set in one operand but not (A ^ B) will give 49 which is ^ both. 0011 0001 Binary Ones Complement Operator is unary and has the effect of (~A ) will give -60 which is ~ 'flipping' bits. 1100 0011 Binary Left Shift Operator. The left operands value is moved left by the A << 2 will give 240 which is << number of bits specified by the right operand. 1111 0000 Binary Right Shift Operator. The left operands value is moved right by the A >> 2 will give 15 which is >> number of bits specified by the right operand. 0000 1111
  • 10. There are following assignment operators supported by C++ language:
  • 11. Oper Description Example ator Simple assignment operator, Assigns values from right side operands to left side C = A + B will assign = operand value of A + B into C Add AND assignment operator, It adds right operand to the left operand and assign the C += A is equivalent to += result to left operand C=C+A Subtract AND assignment operator, It subtracts right operand from the left operand C -= A is equivalent to -= and assign the result to left operand C=C-A Multiply AND assignment operator, It multiplies right operand with the left operand C *= A is equivalent to *= and assign the result to left operand C=C*A Divide AND assignment operator, It divides left operand with the right operand and C /= A is equivalent to /= assign the result to left operand C=C/A Modulus AND assignment operator, It takes modulus using two operands and assign C %= A is equivalent %= the result to left operand to C = C % A C <<= 2 is same as C = <<= Left shift AND assignment operator C << 2 C >>= 2 is same as C = >>= Right shift AND assignment operator C >> 2 C &= 2 is same as C = &= Bitwise AND assignment operator C&2 C ^= 2 is same as C = ^= bitwise exclusive OR and assignment operator C^2 C |= 2 is same as C = C |= bitwise inclusive OR and assignment operator |2
  • 12. There are few other operators supported by C++ Language. Operator Description Size of operator returns the size of a variable. For example size of(a), where a is Size of integer, will return 4. Conditional operator. If Condition is true ? then it returns value X : otherwise Condition ? X : Y value Y Comma operator causes a sequence of operations to be performed. The value of , the entire comma expression is the value of the last expression of the comma- separated list. . (dot) and -> Member operators are used to reference individual members of classes, (arrow) structures, and unions. Casting operators convert one data type to another. For example, int(2.2000) Cast would return 2. Pointer operator & returns the address of an variable. For example &a; will give & actual address of the variable. Pointer operator * is pointer to a variable. For example *var; will pointer to a * variable var.
  • 13.
  • 14. *Expression in C++ is form when we combine operands (variables and constant) and C++ Operators.“ *Expression can also be defined as: "Expression in C++ is a combination of Operands and Operators.“ *Operands in C++ Program are those values on which we want to perform perform operation.
  • 15.
  • 16. "An expression in which arithmetic operators are used is called arithmetic expression“. For example an arithmetic expression is look just like that a+b=5
  • 17. "A relational operator with constants and variables makes relational expression or An expressions in which relational operators are use is called relational expression. Points about relational operators :- 1.Relational operators are use to compare values. 2.All the expressions evaluates from left to right. 3.There are six relational operators in C++ programming (>,<,>=,<=,==,!=). 4.These operators evaluates results true or false. 5.False statement represent by 0 and True statement represent by 1. 6.These operators evaluate at statement level and has no preference.
  • 18. 1. There are three logical operators And( && ),or( || ) these two both are binary operator and not( ! ) is u nary operator. 2. More than one relation expression are combine by using logical operators. 3. The expression will evaluate from left to right if more than one relation expression are use.
  • 19. Example 1 . write the corresponding C++ expression for the following mathematical expression ? 1.2 – ye2y + 4y 2.P + q /(r + s)4 Solution = 1. 2-y*exp(2*y)+4*y = 2. p+q/ pow ((r+s),4)