SlideShare uma empresa Scribd logo
1 de 26
OPERATION AND EXPRESSION IN C++




                                  1
Basic Arithmetic Operation
• Arithmetic is performed with operators
   –   +   for addition
   –   -   for subtraction
   –   *   for multiplication
   –   /   for division

• Example: storing a product in the variable
           total_weight

  total_weight = one_weight * number_of_bars;
                                                2
Arithmetic Expression
• Arithmetic operators can be used with any
  numeric type
• An operand is a number or variable
  used by the operator
• Result of an operator depends on the types
  of operands
   – If both operands are int, the result is int
   – If one or both operands are double, the result is double



                                                                3
Arithmetic Expression (cont.)
• Division with at least one operator of type double
  produces the expected results.

              double divisor, dividend, quotient;
              divisor = 3;
                dividend = 5;
              quotient = dividend / divisor;

   – quotient = 1.6666…
   – Result is the same if either dividend or divisor is
     of type int

                                                           4
Arithmetic Expression (cont.)
• Be careful with the division operator!
   – int / int produces an integer result
      (true for variables or numeric constants)

             int dividend, divisor, quotient;
            dividend = 5;
            divisor = 3;
             quotient = dividend / divisor;

   – The value of quotient is 1, not 1.666…
   – Integer division does not round the result, the
     fractional part is discarded!

                                                       5
Arithmetic Expression (cont.)
• % operator gives the remainder from integer
  division
•       int dividend, divisor, remainder;
        dividend = 5;
        divisor = 3;
        remainder = dividend % divisor;

   The value of remainder is 2

                                                6
Arithmetic Expression (cont.)




                                7
Arithmetic Expression (cont.)




                                8
Arithmetic Expression (cont.)
• Use spacing to make expressions readable
   – Which is easier to read?

              x+y*z    or x + y * z

• Precedence rules for operators are the same as
  used in your algebra classes
• Use parentheses to alter the order of operations
   x + y * z ( y is multiplied by z first)
  (x + y) * z ( x and y are added first)

                                                     9
Arithmetic Expression (cont.)
• Some expressions occur so often that C++
  contains to shorthand operators for them
• All arithmetic operators can be used this way
   – += eg. count = count + 2; becomes
           count += 2;
   – *= eg. bonus = bonus * 2; becomes
           bonus *= 2;
   – /= eg. time = time / rush_factor; becomes
            time /= rush_factor;
   – %= eg. remainder = remainder % (cnt1+ cnt2); becomes
           remainder %= (cnt1 + cnt2);



                                                            10
Assignment Statement
• An assignment statement changes the value of a variable
   – total_weight = one_weight + number_of_bars;
       • total_weight is set to the sum one_weight + number_of_bars

   – Assignment statements end with a semi-colon

   – The single variable to be changed is always on the left
     of the assignment operator ‘=‘

   – On the right of the assignment operator can be
       • Constants -- age = 21;
       • Variables -- my_cost = your_cost;
       • Expressions -- circumference = diameter * 3.14159;



                                                                      11
Assignment Statement (cont.)
• The ‘=‘ operator in C++ is not an equal sign
   – The following statement cannot be true in algebra

      • number_of_bars = number_of_bars + 3;

   – In C++ it means the new value of number_of_bars
     is the previous value of number_of_bars plus 3




                                                         12
Assignment Statement (cont.) –
Initializing Variables
• Declaring a variable does not give it a value
   – Giving a variable its first value is initializing the variable
• Variables are initialized in assignment statements

              double mpg;        // declare the variable
              mpg = 26.3;       // initialize the variable
• Declaration and initialization can be combined
  using two methods
   – Method 1
                 double mpg = 26.3, area = 0.0 , volume;
   – Method 2
                 double mpg(26.3), area(0.0), volume;


                                                                      13
Relational Operation
• A Boolean Expression is an expression that is
  either true or false
   – Boolean expressions are evaluated using
     relational operations such as
      • = = , !=, < , >, <=, and >= which produce a boolean value
   – and boolean operations such as
      • &&, | |, and ! which also produce a boolean value
• Type bool allows declaration of variables that
  carry the value true or false


                                                                    14
Relational Operation (cont.)
• Boolean expressions are evaluated using
  values from the Truth Tables
• For example, if y is 8, the expression
             !( ( y < 3) | | ( y > 7) )
  is evaluated in the following sequence
              ! ( false | | true )
                    ! ( true )
                       false

                                            15
16
Mantic/Logic Operation – Order of
Precedence
• If parenthesis are omitted from boolean
  expressions, the default precedence of
  operations is:
  – Perform ! operations first
  – Perform relational operations such as < next
  – Perform && operations next
  – Perform | | operations last



                                                   17
Mantic/Logic Operation –Precedence
Rules
• Items in expressions are grouped by
  precedence
  rules for arithmetic and boolean operators
  – Operators with higher precedence are performed
    first
  – Binary operators with equal precedence are
    performed left to right
  – Unary operators of equal precedence are
    performed right to left

                                                 18
19
Precedence Rules - Example
• The expression
           (x+1) > 2 | | (x + 1) < -3

    is equivalent to
             ( (x + 1) > 2) | | ( ( x + 1) < -3)

    – Because > and < have higher precedence than | |

•     and is also equivalent to
             x+1>2||x+1<-3
                                                        20
Precedence Rules – Example (cont.)
• (x+1) > 2 | | (x + 1) < -3

• First apply the unary –
   – Next apply the +'s
   – Now apply the > and <
   – Finally do the | |




                                     21
Unary Operator
• Unary operators require only one operand
   – + in front of a number such as +5
   – - in front of a number such as -5
• ++ increment operator
   – Adds 1 to the value of a variable
                      x ++;
     is equivalent to x = x + 1;
• --   decrement operator
   – Subtracts 1 from the value of a variable
                      x --;
     is equivalent to    x = x – 1;

                                                22
Program Style
• A program written with attention to style
  – is easier to read
  – easier to correct
  – easier to change




                                              23
Program Style - Indenting
• Items considered a group should look like a
  group
   – Skip lines between logical groups of statements
   – Indent statements within statements
              if (x = = 0)
                 statement;
• Braces {} create groups
   – Indent within braces to make the group clear
   – Braces placed on separate lines are easier to locate


                                                            24
Program Style - Comments
• // is the symbol for a single line comment
   – Comments are explanatory notes for the programmer
   – All text on the line following // is ignored by the
     compiler
   – Example: //calculate regular wages
                  gross_pay = rate * hours;
• /* and */ enclose multiple line comments
   – Example: /* This is a comment that spans
                 multiple lines without a
                 comment symbol on the middle line
              */

                                                           25
Program Style - Constant
• Number constants have no mnemonic value
• Number constants used throughout a program
  are difficult to find and change when needed
• Constants
  – Allow us to name number constants so they have
    meaning
  – Allow us to change all occurrences simply by
    changing the value of the constant



                                                     26

Mais conteúdo relacionado

Mais procurados (20)

Operators and Expression
Operators and ExpressionOperators and Expression
Operators and Expression
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
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++
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
 
Operators in C/C++
Operators in C/C++Operators in C/C++
Operators in C/C++
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
 
Operators and Expressions
Operators and ExpressionsOperators and Expressions
Operators and Expressions
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
Operators
OperatorsOperators
Operators
 
Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming
 
Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
 
Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++
 
C# operators
C# operatorsC# operators
C# operators
 
operators in c++
operators in c++operators in c++
operators in c++
 
C OPERATOR
C OPERATORC OPERATOR
C OPERATOR
 
Lecture03(c expressions & operators)
Lecture03(c expressions & operators)Lecture03(c expressions & operators)
Lecture03(c expressions & operators)
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
 

Destaque

03. operators and-expressions
03. operators and-expressions03. operators and-expressions
03. operators and-expressionsStoian Kirov
 
Operator Precedence and Associativity
Operator Precedence and AssociativityOperator Precedence and Associativity
Operator Precedence and AssociativityNicole Ynne Estabillo
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssionseShikshak
 
C++ Project: Point of Sales System for an Audio and Video Shop
C++ Project: Point of Sales System for an Audio and Video ShopC++ Project: Point of Sales System for an Audio and Video Shop
C++ Project: Point of Sales System for an Audio and Video Shopprojectlearner
 
Expectations (Algebra 1)
Expectations (Algebra 1)Expectations (Algebra 1)
Expectations (Algebra 1)rfant
 
multimedia authorizing tools
multimedia authorizing toolsmultimedia authorizing tools
multimedia authorizing toolsSantosh Jhansi
 
Philosophy of early childhood education 3
Philosophy of early childhood education 3Philosophy of early childhood education 3
Philosophy of early childhood education 3Online
 
algebraic expression class VIII
algebraic expression class VIIIalgebraic expression class VIII
algebraic expression class VIIIHimani Priya
 
Multimedia authoring tools
Multimedia authoring toolsMultimedia authoring tools
Multimedia authoring toolsOnline
 

Destaque (15)

03. operators and-expressions
03. operators and-expressions03. operators and-expressions
03. operators and-expressions
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Matrices
MatricesMatrices
Matrices
 
computer networks
computer networkscomputer networks
computer networks
 
Operator Precedence and Associativity
Operator Precedence and AssociativityOperator Precedence and Associativity
Operator Precedence and Associativity
 
Operators
OperatorsOperators
Operators
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 
C++ Project: Point of Sales System for an Audio and Video Shop
C++ Project: Point of Sales System for an Audio and Video ShopC++ Project: Point of Sales System for an Audio and Video Shop
C++ Project: Point of Sales System for an Audio and Video Shop
 
Expectations (Algebra 1)
Expectations (Algebra 1)Expectations (Algebra 1)
Expectations (Algebra 1)
 
multimedia authorizing tools
multimedia authorizing toolsmultimedia authorizing tools
multimedia authorizing tools
 
Philosophy of early childhood education 3
Philosophy of early childhood education 3Philosophy of early childhood education 3
Philosophy of early childhood education 3
 
algebraic expression class VIII
algebraic expression class VIIIalgebraic expression class VIII
algebraic expression class VIII
 
Multimedia authoring tools
Multimedia authoring toolsMultimedia authoring tools
Multimedia authoring tools
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Semelhante a Operation and expression in c++

Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cSowmya Jyothi
 
Lecture 2 variables
Lecture 2 variablesLecture 2 variables
Lecture 2 variablesTony Apreku
 
ESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptxESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptxAvijitChaudhuri3
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptxSKUP1
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptxLECO9
 
Lamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckLamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckseidounsemel
 
Programming techniques
Programming techniquesProgramming techniques
Programming techniquesPrabhjit Singh
 
Class 2 variables, classes methods...
Class 2   variables, classes methods...Class 2   variables, classes methods...
Class 2 variables, classes methods...Fernando Loizides
 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java Ahmad Idrees
 
Lab 4 reading material formatted io and arithmetic expressions
Lab 4 reading material formatted io and arithmetic expressionsLab 4 reading material formatted io and arithmetic expressions
Lab 4 reading material formatted io and arithmetic expressionsAksharVaish2
 
OPERATORS OF C++
OPERATORS OF C++OPERATORS OF C++
OPERATORS OF C++ANANT VYAS
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptxAshokJ19
 

Semelhante a Operation and expression in c++ (20)

Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
 
Lecture 2 variables
Lecture 2 variablesLecture 2 variables
Lecture 2 variables
 
ESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptxESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptx
 
c-programming
c-programmingc-programming
c-programming
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
C Language Part 1
C Language Part 1C Language Part 1
C Language Part 1
 
Coper in C
Coper in CCoper in C
Coper in C
 
Lamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckLamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ck
 
Programing techniques
Programing techniquesPrograming techniques
Programing techniques
 
Programming techniques
Programming techniquesProgramming techniques
Programming techniques
 
python ppt
python pptpython ppt
python ppt
 
Class 2 variables, classes methods...
Class 2   variables, classes methods...Class 2   variables, classes methods...
Class 2 variables, classes methods...
 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java
 
Lab 4 reading material formatted io and arithmetic expressions
Lab 4 reading material formatted io and arithmetic expressionsLab 4 reading material formatted io and arithmetic expressions
Lab 4 reading material formatted io and arithmetic expressions
 
OPERATORS OF C++
OPERATORS OF C++OPERATORS OF C++
OPERATORS OF C++
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 

Mais de Online

Philosophy of early childhood education 2
Philosophy of early childhood education 2Philosophy of early childhood education 2
Philosophy of early childhood education 2Online
 
Philosophy of early childhood education 1
Philosophy of early childhood education 1Philosophy of early childhood education 1
Philosophy of early childhood education 1Online
 
Philosophy of early childhood education 4
Philosophy of early childhood education 4Philosophy of early childhood education 4
Philosophy of early childhood education 4Online
 
Functions
FunctionsFunctions
FunctionsOnline
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and outputOnline
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selectionOnline
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetitionOnline
 
Introduction to problem solving in c++
Introduction to problem solving in c++Introduction to problem solving in c++
Introduction to problem solving in c++Online
 
Optical transmission technique
Optical transmission techniqueOptical transmission technique
Optical transmission techniqueOnline
 
Multi protocol label switching (mpls)
Multi protocol label switching (mpls)Multi protocol label switching (mpls)
Multi protocol label switching (mpls)Online
 
Lan technologies
Lan technologiesLan technologies
Lan technologiesOnline
 
Introduction to internet technology
Introduction to internet technologyIntroduction to internet technology
Introduction to internet technologyOnline
 
Internet standard routing protocols
Internet standard routing protocolsInternet standard routing protocols
Internet standard routing protocolsOnline
 
Internet protocol
Internet protocolInternet protocol
Internet protocolOnline
 
Application protocols
Application protocolsApplication protocols
Application protocolsOnline
 
Addressing
AddressingAddressing
AddressingOnline
 
Transport protocols
Transport protocolsTransport protocols
Transport protocolsOnline
 
Leadership
LeadershipLeadership
LeadershipOnline
 
Introduction to management
Introduction to managementIntroduction to management
Introduction to managementOnline
 
Motivation
MotivationMotivation
MotivationOnline
 

Mais de Online (20)

Philosophy of early childhood education 2
Philosophy of early childhood education 2Philosophy of early childhood education 2
Philosophy of early childhood education 2
 
Philosophy of early childhood education 1
Philosophy of early childhood education 1Philosophy of early childhood education 1
Philosophy of early childhood education 1
 
Philosophy of early childhood education 4
Philosophy of early childhood education 4Philosophy of early childhood education 4
Philosophy of early childhood education 4
 
Functions
FunctionsFunctions
Functions
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selection
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
 
Introduction to problem solving in c++
Introduction to problem solving in c++Introduction to problem solving in c++
Introduction to problem solving in c++
 
Optical transmission technique
Optical transmission techniqueOptical transmission technique
Optical transmission technique
 
Multi protocol label switching (mpls)
Multi protocol label switching (mpls)Multi protocol label switching (mpls)
Multi protocol label switching (mpls)
 
Lan technologies
Lan technologiesLan technologies
Lan technologies
 
Introduction to internet technology
Introduction to internet technologyIntroduction to internet technology
Introduction to internet technology
 
Internet standard routing protocols
Internet standard routing protocolsInternet standard routing protocols
Internet standard routing protocols
 
Internet protocol
Internet protocolInternet protocol
Internet protocol
 
Application protocols
Application protocolsApplication protocols
Application protocols
 
Addressing
AddressingAddressing
Addressing
 
Transport protocols
Transport protocolsTransport protocols
Transport protocols
 
Leadership
LeadershipLeadership
Leadership
 
Introduction to management
Introduction to managementIntroduction to management
Introduction to management
 
Motivation
MotivationMotivation
Motivation
 

Último

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 

Último (20)

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 

Operation and expression in c++

  • 2. Basic Arithmetic Operation • Arithmetic is performed with operators – + for addition – - for subtraction – * for multiplication – / for division • Example: storing a product in the variable total_weight total_weight = one_weight * number_of_bars; 2
  • 3. Arithmetic Expression • Arithmetic operators can be used with any numeric type • An operand is a number or variable used by the operator • Result of an operator depends on the types of operands – If both operands are int, the result is int – If one or both operands are double, the result is double 3
  • 4. Arithmetic Expression (cont.) • Division with at least one operator of type double produces the expected results. double divisor, dividend, quotient; divisor = 3; dividend = 5; quotient = dividend / divisor; – quotient = 1.6666… – Result is the same if either dividend or divisor is of type int 4
  • 5. Arithmetic Expression (cont.) • Be careful with the division operator! – int / int produces an integer result (true for variables or numeric constants) int dividend, divisor, quotient; dividend = 5; divisor = 3; quotient = dividend / divisor; – The value of quotient is 1, not 1.666… – Integer division does not round the result, the fractional part is discarded! 5
  • 6. Arithmetic Expression (cont.) • % operator gives the remainder from integer division • int dividend, divisor, remainder; dividend = 5; divisor = 3; remainder = dividend % divisor; The value of remainder is 2 6
  • 9. Arithmetic Expression (cont.) • Use spacing to make expressions readable – Which is easier to read? x+y*z or x + y * z • Precedence rules for operators are the same as used in your algebra classes • Use parentheses to alter the order of operations x + y * z ( y is multiplied by z first) (x + y) * z ( x and y are added first) 9
  • 10. Arithmetic Expression (cont.) • Some expressions occur so often that C++ contains to shorthand operators for them • All arithmetic operators can be used this way – += eg. count = count + 2; becomes count += 2; – *= eg. bonus = bonus * 2; becomes bonus *= 2; – /= eg. time = time / rush_factor; becomes time /= rush_factor; – %= eg. remainder = remainder % (cnt1+ cnt2); becomes remainder %= (cnt1 + cnt2); 10
  • 11. Assignment Statement • An assignment statement changes the value of a variable – total_weight = one_weight + number_of_bars; • total_weight is set to the sum one_weight + number_of_bars – Assignment statements end with a semi-colon – The single variable to be changed is always on the left of the assignment operator ‘=‘ – On the right of the assignment operator can be • Constants -- age = 21; • Variables -- my_cost = your_cost; • Expressions -- circumference = diameter * 3.14159; 11
  • 12. Assignment Statement (cont.) • The ‘=‘ operator in C++ is not an equal sign – The following statement cannot be true in algebra • number_of_bars = number_of_bars + 3; – In C++ it means the new value of number_of_bars is the previous value of number_of_bars plus 3 12
  • 13. Assignment Statement (cont.) – Initializing Variables • Declaring a variable does not give it a value – Giving a variable its first value is initializing the variable • Variables are initialized in assignment statements double mpg; // declare the variable mpg = 26.3; // initialize the variable • Declaration and initialization can be combined using two methods – Method 1 double mpg = 26.3, area = 0.0 , volume; – Method 2 double mpg(26.3), area(0.0), volume; 13
  • 14. Relational Operation • A Boolean Expression is an expression that is either true or false – Boolean expressions are evaluated using relational operations such as • = = , !=, < , >, <=, and >= which produce a boolean value – and boolean operations such as • &&, | |, and ! which also produce a boolean value • Type bool allows declaration of variables that carry the value true or false 14
  • 15. Relational Operation (cont.) • Boolean expressions are evaluated using values from the Truth Tables • For example, if y is 8, the expression !( ( y < 3) | | ( y > 7) ) is evaluated in the following sequence ! ( false | | true ) ! ( true ) false 15
  • 16. 16
  • 17. Mantic/Logic Operation – Order of Precedence • If parenthesis are omitted from boolean expressions, the default precedence of operations is: – Perform ! operations first – Perform relational operations such as < next – Perform && operations next – Perform | | operations last 17
  • 18. Mantic/Logic Operation –Precedence Rules • Items in expressions are grouped by precedence rules for arithmetic and boolean operators – Operators with higher precedence are performed first – Binary operators with equal precedence are performed left to right – Unary operators of equal precedence are performed right to left 18
  • 19. 19
  • 20. Precedence Rules - Example • The expression (x+1) > 2 | | (x + 1) < -3 is equivalent to ( (x + 1) > 2) | | ( ( x + 1) < -3) – Because > and < have higher precedence than | | • and is also equivalent to x+1>2||x+1<-3 20
  • 21. Precedence Rules – Example (cont.) • (x+1) > 2 | | (x + 1) < -3 • First apply the unary – – Next apply the +'s – Now apply the > and < – Finally do the | | 21
  • 22. Unary Operator • Unary operators require only one operand – + in front of a number such as +5 – - in front of a number such as -5 • ++ increment operator – Adds 1 to the value of a variable x ++; is equivalent to x = x + 1; • -- decrement operator – Subtracts 1 from the value of a variable x --; is equivalent to x = x – 1; 22
  • 23. Program Style • A program written with attention to style – is easier to read – easier to correct – easier to change 23
  • 24. Program Style - Indenting • Items considered a group should look like a group – Skip lines between logical groups of statements – Indent statements within statements if (x = = 0) statement; • Braces {} create groups – Indent within braces to make the group clear – Braces placed on separate lines are easier to locate 24
  • 25. Program Style - Comments • // is the symbol for a single line comment – Comments are explanatory notes for the programmer – All text on the line following // is ignored by the compiler – Example: //calculate regular wages gross_pay = rate * hours; • /* and */ enclose multiple line comments – Example: /* This is a comment that spans multiple lines without a comment symbol on the middle line */ 25
  • 26. Program Style - Constant • Number constants have no mnemonic value • Number constants used throughout a program are difficult to find and change when needed • Constants – Allow us to name number constants so they have meaning – Allow us to change all occurrences simply by changing the value of the constant 26