Expressions - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Expressions

Description:

Expressions. Expressions. A sequence of operands and operators that reduces to a ... Ternary: binary expression/ operator/ expression/ operator/ expression ... – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 22
Provided by: Dua47
Category:

less

Transcript and Presenter's Notes

Title: Expressions


1
Expressions
2
Expressions
  • A sequence of operands and operators that reduces
    to a single value.
  • e.g. 5 3 (reduces to 8)
  • Operator a syntactical token of the language
    that requires some action (mathematical, logical,
    bitwise, etc.).
  • Operand Receives an operators action. May be
    one, two, or more operands in an expression.

3
Expressions
  • Formats
  • Primary identifier, constant, or parenthetical
    expression.
  • Postfix expression/ operator
  • Unary operator/ expression.
  • Binary expression/ operator/ expression
  • Ternary binary expression/ operator/ expression/
    operator/ expression
  • Assignment variable/ operator/ expression

4
Expressions
  • Primary
  • a b12 name SIZE
  • 5 3.14 a Hello
  • (2 3 10) (a 5 b)
  • Binary
  • 5 3 a b a 5
  • Assignment
  • a 5

5
Cs Primary Math Operators
  • Definitions
  • Overloaded operator an operator that can perform
    more than one operation.
  • Unary operator an operator that acts on one
    operand.
  • Binary operator an operator that acts on two
    operands.

6
Cs Primary Math Operators
  • Unary specifies positive amount
  • Binary addition
  • - Unary specifies negative amount
  • Binary subtraction
  • Multiplication
  • / Division
  • Modulus (remainder)

7
Cs Primary Math Operators
  • Unary /-
  • May be placed in front of single numeric
    literals, constants, or variables.
  • age39 percentLoss-6.5
  • scoretotal-numWrong
  • define TEMP(10)
  • num-TEMP

8
Cs Primary Math Operators
  • Unary /-
  • Always put a space between two s or two s
    when using as math operators.
  • If is omitted C assumes the number is positive.
  • int a
  • int b5
  • int c2
  • a-(bc) /a gets -10/

9
Cs Primary Math Operators
  • Division
  • Use forward slash for division /.
  • Performs integer division when both operands are
    integers.
  • Performs floating-point division when at least
    one operand is non-integer.
  • int score180
  • int score295
  • float avg
  • avg(score1score2)/2 //What is stored in avg?

10
Cs Primary Math Operators
  • Modulus
  • is the modulus operator.
  • Returns the remainder of a division
  • Works with integers only.
  • int remain
  • remain102 //remain holds 0
  • remain92 //remain holds 1

11
Cs Primary Math Operators
  • Operator precedence
  • Hierarchical structure of operators.
  • Determines the order of operations performed.
  • Operators of equal precedence calculated left to
    right.
  • Highest Unary -
  • /
  • -
  • Lowest

12
Cs Primary Math Operators
  • Operator precedence
  • Operators of highest precedence are calculated
    first.
  • ans673 //ans gets 27
  • Operators of equal precedence are calculated left
    to right (usually).
  • ans34/6 //ans gets 2
  • ans4/63 //ans gets 0

13
Cs Primary Math Operators
  • Parenthesis
  • Used to override precedence and make your code
    easier to read.
  • ans(67)3 //ans gets 39
  • You may want to split lengthy expressions into
    multiple lines of code to make it easier to
    understand.

14
Cs Primary Math Operators
  • Multiple assignments
  • Math operators are left associative (left to
    right).
  • The equals assignment operator is right
    associative.
  • num1num2num30

15
Cs Math Operators
  • Compound operators ( - /
  • --)
  • Lower precedence than operators previously
    discussed.
  • Shortens your statements.
  • No advantage in efficiency.
  • scorescore2
  • priceprice0.75
  • countcount1
  • score2
  • price0.75
  • count

16
Type Conversion
  • Implicit Type Conversion
  • C will automatically convert any intermediate
    values to the proper type so that the expression
    can be evaluated.
  • In all expressions (except assignments) any
    implicit type conversions will always be made to
    the more general type according to the following
    promotion order
  • Promotion Hierarchy
  • char?short ? int ? uint ? long ? ulong ? float ?
    double ? long double

17
Type Conversion
  • Implicit Type Conversion

18
  • include ltstdio.hgt
  • int main(void)
  • int x12
  • int y5
  • float c0
  • ccx/y //right side implicitly cast as float
  • printf("f",c) //outputs 2.000000
  • return 0

19
Type Conversion
  • Explicit Type Conversion
  • Instead of letting the compiler implicitly
    convert your data, you can do it yourself using a
    cast expression operator.
  • (float) a //must use parentheses
  • (float) (ab) //operand must be unary expression.
  • (float) a/b //explicit and implicit conversion.

20
Type Conversion
  • include ltstdio.hgt
  • int main(void)
  • int x12
  • int y5
  • float c0
  • cc(float)x/y //right side explicitly cast as
    float
  • printf("f",c) //outputs 2.400000
  • return 0

21
  • /
  • File rad_degree.c
  • Author Duane Birnbaum
  • Project Sample C Program
  • Description This program converts radians to
    degrees.

  • /
  • include ltstdio.hgt
  • define PI 3.14159
  • define FACTOR 180.0/PI
  • int main(void)
  • int radians
  • float degrees
  • printf("Enter the angle in radians ")
  • scanf("d",radians)
  • degreesradiansFACTOR //Will this work
    properly?
Write a Comment
User Comments (0)
About PowerShow.com