computer programming (tmk 3102) lecture notes 6

Upload: mamat88

Post on 30-May-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    1/51

    TMK 3102-Chap 6: Operators &

    Ex ression

    1

    Chapt er 6: Opera t ors&Express ions

    Session Plan (Week 6):To understand:

    arithmetic operators and their order of

    precedence

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    2/51

    TMK 3102-Chap 6: Operators &Ex ression

    2

    I n t roduc t ion We use arithmetic expressions to solve most

    programming problems

    Arithmetic expressions comprise of operators

    and operands

    There are rules for writing and evaluating

    arithmetic expressions

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    3/51

    TMK 3102-Chap 6: Operators &Ex ression

    3

    Operat or and Operand

    What are operator andoperand?

    Example:

    W + Z

    Operand

    Operator

    Operand

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    4/51

    TMK 3102-Chap 6: Operators &Ex ression

    4

    Operator & Operand

    Example:

    Which is which?

    A / BOperator ??

    Operand??

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    5/51

    TMK 3102-Chap 6: Operators &Ex ression

    5

    Basic Ar i t hm et ic Opera t ors

    Addition (+)

    Subtraction (-)

    Multiplication (*)

    Division (/)

    Modulus (%)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    6/51

    TMK 3102-Chap 6: Operators &Ex ression

    6

    Basic Ar i t hm et ic Opera t ors

    Multiplication, addition and subtractionare the simplest to use

    Division is easy, but some precautions needto be taken

    Modulus is the one that normally confusesnovices

    So, lets study in detail the Division andModulus

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    7/51

    TMK 3102-Chap 6: Operators &Ex ression

    7

    Divis ion

    Example:

    W / Z

    Floating Division

    W or Z or both are

    floats

    Integer Division

    W and Z are integers

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    8/51

    TMK 3102-Chap 6: Operators &Ex ression

    8

    In t eger Div is ion

    Example:

    8 / 2 = 4

    an integer

    an integer

    the result is

    also an integer

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    9/51

    TMK 3102-Chap 6: Operators &Ex ression

    9

    In t eger Div is ion

    Example:

    12 / 5 = 2

    an integer

    an integer

    the result is

    also an integer

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    10/51

    TMK 3102-Chap 6: Operators &Ex ression

    10

    Float ing Div is ion

    Example:

    12.0 / 5 = 2.0

    a float

    an integer

    the result is afloat

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    11/51

    TMK 3102-Chap 6: Operators &Ex ression

    11

    Som et h ing to ponder

    What will be the answer

    if an integer is dividedby 0? How about if oneof the operands is anegative integer?

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    12/51

    TMK 3102-Chap 6: Operators &Ex ression

    12

    Modulus

    It returns the remainder that occursafter performing the division of 2

    operands

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    13/51

    TMK 3102-Chap 6: Operators &Ex ression

    13

    Modulus

    Example:

    12 % 5 = 2

    an integer

    an integer

    the result is theremainder of 12/5

    125

    2

    10

    2

    remainderresult

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    14/51

    TMK 3102-Chap 6: Operators &Ex ression

    14

    Modulus

    Example:

    7 % 3 = 1

    an integer

    an integer

    73

    2

    6

    1

    remainderresult

    the result is theremainder of 7/3

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    15/51

    TMK 3102-Chap 6: Operators &Ex ression

    15

    Som et h ing to ponder

    The earlier expressions

    contain only oneoperator at a time. Whatif the expression

    contains more than oneoperator?

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    16/51

    TMK 3102-Chap 6: Operators &Ex ression

    16

    Ar it hm et ic Ex press ion

    An expression may contain 2 or more

    arithmetic operators

    Main issue:

    ORDER OF PRECEDENCE

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    17/51

    TMK 3102-Chap 6: Operators &Ex ression

    17

    Ar it hm et ic Ex press ion

    Examples:

    5 + 6

    5 + 6 * 2

    2.5 + 6 2 * 2

    12 / 6.0 2 * 2

    = 11

    = 22 or 17?

    = ??

    = ??

    = 17

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    18/51

    TMK 3102-Chap 6: Operators &Ex ression

    18

    Ar it hm et ic Ex press ion

    Order of Precedence:

    High: * / %

    Low: + -

    All operators have a precedence level. Highprecedence level operators are evaluated before

    lower ones. Operators of the same precedencelevel are evaluated from left to right

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    19/51

    TMK 3102-Chap 6: Operators &Ex ression

    19

    Ar it hm et ic Ex press ion

    Example:2.5 + 6 2 * 2 = ??

    4

    8.5

    2.5 + 6

    4

    4.5

    2.5 + 6 2 * 2 =Example:

    2.5 + 6 2 * 2 = 4.5

    4

    8.5

    2.5 + 6

    4

    4.5

    2.5 + 6 2 * 2 =

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    20/51

    TMK 3102-Chap 6: Operators &Ex ression

    20

    Try i t !

    Example:

    12 + 6.0 2 * 2 = ??

    Whats the answer??

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    21/51

    TMK 3102-Chap 6: Operators &Ex ression

    21

    Ar it hm et ic Ex press ion

    All expressions in parentheses (brackets)must be evaluated prior to values outsidebrackets

    Nested parenthesized expressions must beevaluated from the inside out, with theinnermost expression evaluated first

    Example:

    ( 9 ( 3 + 2 ) ) * 3 = ??

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    22/51

    TMK 3102-Chap 6: Operators &Ex ression

    22

    Ar it hm et ic Ex press ion

    Example:

    ( 9 ( 3 + 2 ) ) * 3 = ?? 54 12( 9 ( 3 + 2 ) ) * 3 = 12

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    23/51

    TMK 3102-Chap 6: Operators &Ex ression

    23

    Assignm ent St a t em ent

    There are 3 types of assignment:

    Simple

    Multiple

    Shorthand

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    24/51

    TMK 3102-Chap 6: Operators &Ex ression

    24

    Sim ple Ass ignm ent

    Syntax:

    variable = expression ;

    Dont forget the semicolon !!

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    25/51

    TMK 3102-Chap 6: Operators &Ex ression

    25

    Sim ple Ass ignm entImport java.utils.Scanner;

    public class discount {

    public static void main(String [] args ) {

    Scanner in = new Scanner(System.in);

    float price, discount, total;

    System.out.print(Buying price : );price = in.nextDouble();

    System.out.print(Discount rate : );

    discount=in.nextDouble();

    total = price (price * discount);

    System.out.println(For buying price + price + and discountrate + discount);

    System.out.println(The total price is + total);

    }

    }

    Buying price: _

    discount ??

    price ??

    total ??

    Buying price: 10.00

    _

    10.00

    Buying price: 10.00

    Discount rate: _

    Buying price: 10.00

    Discount rate: 0.25

    _

    0.25

    7.50

    Buying price: 10.00

    Discount rate: 0.25

    For buying price RM10.00 and discount rate 0.25

    _

    Buying price: 10.00

    Discount rate: 0.25

    For buying price RM10.00 and discount rate 0.25

    The total price is RM7.50

    _

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    26/51

    TMK 3102-Chap 6: Operators &Ex ression

    26

    Mul t ip le Ass ignm ent

    Syntax:

    variable = variable = expression ;

    Dont forget the semicolon !!

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    27/51

    TMK 3102-Chap 6: Operators &Ex ression

    27

    Example:

    int number, total;

    float start_x, start_y;

    . . .

    number = total = 0;

    start_x = start_y = 100.0;

    total ??

    number ??

    start_x ??

    start_y ??

    0

    0

    100.0

    100.0

    Mul t ip le Ass ignm ent

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    28/51

    TMK 3102-Chap 6: Operators &Ex ression

    28

    Shor t hand Ass ignm ent

    Syntax:

    variableX = variableX opexpression ;

    variableX op= expression;

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    29/51

    TMK 3102-Chap 6: Operators &Ex ression

    29

    Shor t hand Ass ignm ent

    Whenever the expression on the right

    contains the variable on the left (to which

    the value is assigned)Example:

    num = num + 5;

    num 15

    15 + 520

    20

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    30/51

    TMK 3102-Chap 6: Operators &Ex ression

    30

    Shor t hand Ass ignm ent

    Expressions can also be stated using

    shorthand assignment operators

    Example:num += 5; similar to num = num + 5

    shorthand assignment operator

    Shorthand assignment operators have the lowest

    order of precedence the last one to be evaluated

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    31/51

    TMK 3102-Chap 6: Operators &Ex ression

    31

    Shor t hand Ass ignm ent

    Operation Examples ofexpression

    Description

    += num += 5; num = num + 5;

    -= num -= 5; num = num 5;

    *= num *= 5; num = num * 5;

    /= num /= 5; num = num / 5;

    %= num %= 5; num = num % 5;

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    32/51

    TMK 3102-Chap 6: Operators &Ex ression

    32

    Shor t hand Ass ignm ent

    Example:

    pay += hour * rate * 2

    pay + (8 * 5.00 * 2)

    pay 100.00

    hour 8rate 5.00

    similar to pay = pay + (hour * rate * 2)

    pay + (hour * rate * 2)

    100.00 + 80.00 180.00

    180.00

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    33/51

    TMK 3102-Chap 6: Operators &Ex ression

    33

    Ass ignm ent by Value

    Every assignment expression has a value

    Example:

    Expression 1: a = 1;

    Expression 2: x = y = 0;

    Expression 3: p = 12;

    p = 0;

    Example:

    int a, x, y, p;

    Line 1: a=1;

    Line 2: x = y = 0;

    Line 3: p = 12;Line 4: p = p +3;

    Line 5: q = p = p + x;

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    34/51

    TMK 3102-Chap 6: Operators &Ex ression

    34

    Relat ional Operat ors

    Operation Description Examplesof

    Expression

    Value

    < Less than 6 < 9 true

    6 false

    >= Greater than or equal to 9 >= 5 true

    == Equal to 7 == 5 false

    != Not equal to 6 != 5 true

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    35/51

    TMK 3102-Chap 6: Operators &Ex ression

    35

    Mant ic /Logic a l Operat ors

    Symbol Description && AND || OR

    ! NOT

    a b a && b a || b !a !b

    T T T T F F

    T F F T F TF T F T T F

    F F F F T T

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    36/51

    TMK 3102-Chap 6: Operators &Ex ression

    36

    Com pound St at em ent

    Arithmetic, relational and mantic operators can be

    integrated/combined in one expression

    Example:

    ! ( c > a )a 2

    b 5

    c 15

    d 17 ! ( true ) ! ( 15 > 2 ) false

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    37/51

    TMK 3102-Chap 6: Operators &Ex ression

    37

    Com pound St at em ent

    Example:

    (a >= 1) && (b == 5)

    a 2

    b 5

    c 15

    d 17

    ( 2 >= 1 ) && ( b == 5 )

    true && ( b == 5 )

    true && ( 5 == 5 ) true && true

    true

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    38/51

    TMK 3102-Chap 6: Operators &Ex ression

    38

    Com pound St at em ent

    Example:

    (c >= ( b * 3 ) ) || (a == 3)

    a 2

    b 5

    c 15

    d 17

    ( c >= ( 5 * 3 ) ) || ( a == 3)

    true || ( a == 3 )

    true || ( 2 == 3 )

    true || false

    ( 15 >= 15 ) || ( a == 3)

    true

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    39/51

    TMK 3102-Chap 6: Operators &Ex ression

    39

    Com pound St at em ent

    Example:

    ! ( ( a < b ) || ( c > d ) )

    a 2

    b 5

    c 15

    d 17

    ! ( ( 2 < 5 ) || ( c > d ) )

    ! ( true || ( 15 > 17 ) )

    ! ( true || false )

    ! true

    ! ( true || ( c > d ) )

    false

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    40/51

    TMK 3102-Chap 6: Operators &Ex ression

    40

    Inc rem ent and Dec rement

    This operation contains only one operand, that is,the operand which value will be incremented/

    decremented

    Symbol Description Examplesof

    Expression

    Description

    ++ Incrementoperand by 1

    i++ i = i + 1

    -- Decrementoperand by 1

    i-- i = i 1

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    41/51

    TMK 3102-Chap 6: Operators &Ex ression

    41

    Inc rem ent and Dec rement

    Key-in a number: _Key-in a number: 26

    _

    Example:

    int num;

    System.out.println(Key-in a number: );

    num=in.nextInt();System.out.println(Value before being incremented: + num);

    num++;

    System.out.println(Value after being incremented: +num);

    num ??

    Key-in a number: 26

    Value before being incremented: 26

    _

    2627

    Key-in a number: 26

    Value before being incremented: 26

    Value after being incremented: 27 _

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    42/51

    TMK 3102-Chap 6: Operators &Ex ression

    42

    Pref ix and Post f ix

    Increment and Decrement operators canbe either in prefix or postfix forms

    Expression Description

    i++ Value of i is incremented after being used in theexpression

    ++i Value of i is incremented before being used inthe expression

    i-- Value of i is decremented after being used inthe expression

    --i Value of i is decremented before being used inthe expression

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    43/51

    TMK 3102-Chap 6: Operators &Ex ression

    43

    Pref ix and Post f ix

    Example:

    j = i++ - 2

    i 5

    similar to

    j = i 2;

    i = i + 1; j ??3

    6

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    44/51

    TMK 3102-Chap 6: Operators &Ex ression

    44

    Pref ix and Post f ix

    Example:

    j = ++i - 2

    i 5

    similar to

    i = i + 1;

    j = i 2; j ??4

    6

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    45/51

    TMK 3102-Chap 6: Operators &Ex ression

    45

    Som et h ing to ponder

    The earlier expressions

    contain only one type ata time. What if theexpression containsmore than one type?

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    46/51

    TMK 3102-Chap 6: Operators &Ex ression

    46

    Conversion Rules

    When performing a binary operation involvingtwo operands of different type, Javaautomatically converts the operand based onthe following rules

    If one of the operands is double, the other isconverted into double

    Otherwise, if one of the operands is float, theother is converted to float

    Otherwise, if one of the operands is long, theother is converted to long Otherwise, both operands are converted to int

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    47/51

    TMK 3102-Chap 6: Operators &Ex ression

    47

    Convers ion Problem s Ex am ple

    byte i = 100;long k = i * 3 + 4;

    double d = i * 3.1 + k / 2;

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    48/51

    TMK 3102-Chap 6: Operators &Ex ression

    48

    Type Cast ing

    Example:int x = (int) 3.0; // type narrowing

    int y = (int) 65.76;//truncationy 65

    x 3

    Casting is used to compute a value that is equivalent

    to its operands value (based on the stated data type)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    49/51

    TMK 3102-Chap 6: Operators &Ex ression

    49

    Type Cast ing Ex am ple

    Example:

    int x;

    float y = 35.87;

    x = (int) y;

    ( int ) 35.87

    35

    y 35.87

    x ??35

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    50/51

    TMK 3102-Chap 6: Operators &Ex ression

    50

    THINK!! !

    What is wrong?

    int i = 5;double d = 2.0;

    int x = i/d;

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 6

    51/51

    Thank You