introduction to programming using java - lecture 3

Upload: rochana-ramanayaka

Post on 04-Jun-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    1/30

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    2/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    2

    An operatoris a function that has a special

    symbolic name and is invoked by using that

    symbol with an expression.Examples of expressions that include

    Java operators :

    n = 2

    n += 3

    ++nn / 3

    n % 3

    OperatorsOperators

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    3/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    3

    OperatorsOperators

    Assignment Operator (=)

    Example

    A=15;

    num1 = num2 = num3 = A + 5;

    Right side of Assignment operator

    is evaluated first and then the

    Assignment takes place

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    4/30

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    5/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    5

    Arithmetic OperatorsArithmetic Operators

    x = y / 2;

    in the above expression, x and y are

    variables, 2 is a literal and = and / areoperators.

    This expression states that the y variable is

    divided by 2 using the division operator (/),and the result is stored in x using the

    assignment operator (=).

    Notice the expression was described from

    right to left. It is how the compiler itself

    analyzes expressions to generate code.

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    6/[email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    6

    Arithmetic OperatorsArithmetic Operators

    It is also possible to use the shortcuts for arithmetic

    operators as given below.a + = b; is the same as a = a + b;

    a - = z; is the same as a = a - z;

    a * = 9; is the same as a = a * 9;

    a / = 3 is the same as a = a / 3;

    a % = 3 is the same as a = a % 3;

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    7/[email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    7

    OperatorsOperators

    class Arithmetic {

    public static void main (String args[]) {

    int x=17, y=5;

    System.out.println(x=+x);

    System.out.println(y=+y);

    System.out.println(x+y=+(x+y));

    System.out.println(x-y=+(x-y));

    System.out.println(x*y=+(x*y));

    System.out.println(x/y=+(x/y));

    System.out.println(x%y=+(x%y));

    }

    }

    x=17

    y=5

    x+y=22

    x-y=12

    x*y=85

    x/y=3

    x%y=2

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    8/[email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    8

    Special OperatorsSpecial Operators

    class SpecialArithmetic {

    public static void main (String args[]) {

    int x=17,a,b;a=x++;b=++x;

    System.out.println(x=+x+ a=+a);

    System.out.println(x=+x+ b=+b);a=x--;b=--x;

    System.out.println(x=+x+ a=+a);

    System.out.println(x=+x+ b=+b);

    }

    }

    x=19 a=17

    x=19 b=19

    x=18 a=19

    x=17 b=17

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    9/[email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    9

    Bitwise OperatorsBitwise Operators

    Operator Meaning

    & Bitwise AND

    | Bitwise OR^ Bitwise XOR

    >> Right Shift

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    10/30

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    11/[email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    11

    Bitwise OperatorsBitwise Operators

    Bit-wise Operators

    Operator Meaning

    x &= y AND Assignmentx |= y OR Assignment

    x ^= y XOR Assignment

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    12/[email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    12

    Bitwise OperatorsBitwise Operators

    class Bitwise {

    public static void main (string args[]) {

    int x=5, y=6;System.out.println(x=+x);

    System.out.println(y=+y);

    System.out.println(x&y=+(x&y));

    System.out.println(x|y=+(x|y));

    System.out.println(x^y=+(x^y));

    }

    }

    x=5

    y=6

    x&y=4

    x|y=7

    x^y=3

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    13/[email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    13

    Bitwise OperatorsBitwise Operators

    class Shift {

    public static void main (String args[]) {

    int x=7;

    System.out.println(x=+x);

    System.out.println(x >>2=+(x>>2);System.out.println(x>>1));}

    }

    x=7

    y>>2=1

    x>1=3

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    14/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    14

    Control StatementsControl Statements

    Control Statements

    If ....Else Statements

    if ( Expression )

    statement1;

    elsestatement2;

    If Expression true then statement1 is executed;

    otherwise statement2 is executed. else is optional.

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    15/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    15

    If ..else statementsIf ..else statements

    If more Statements are to be performed, all the

    statements should be included within curly brackets.

    If ( Expression) {

    statement a;

    statement b;

    }

    else { statement c;

    statement d;

    }

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    16/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    16

    Control StatementsControl Statements

    True False

    If - else Statement Contd..

    Statement bStatement d

    Statement cStatement a

    Expression

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    17/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    17

    Conditional Operator ?:Conditional Operator ?:

    Expression ? Statement 1 : Statement 2

    If expression is true If expression is false

    X= ((marks>50) ? Pass : Failed);

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    18/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    18

    Logical OperatorsLogical Operators

    Operator Operator Meaning

    & && Logical AND

    | || Logical OR

    ! Logical NOT

    Short Circuit Operators

    Condition Operator

    Boolean Operators

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    19/30

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    20/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    20

    Logical OperatorsLogical Operators

    || (Logical OR) Operator

    Expression Result

    condition 1 condition 2 condition 1 || condition 2

    (You know politico?) OR (You know thugs?) (You are rich)

    true true true

    false true truetrue false true

    false false false

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    21/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    21

    Logical OperatorsLogical Operators

    When evaluating && and ||, Java will stop the

    evaluation of the rest of the expression as soon as

    the outcome becomes clear.

    In (dnom!=0 && num / denom>10)

    second part will not be evaluated if (dnom =0)

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    22/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    22

    Logical OperatorsLogical Operators

    ! Operator

    Expression Resultcondition ! (condition)

    true falsefalse true

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    23/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    23

    Relational OperatorsRelational Operators

    Operator Meaning

    != not equal to== is equal to

    < less than> greater than

    >= greater than or equal to

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    24/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    24

    Relational OperatorsRelational Operators

    class Relational{

    public static void main (String args[]) {

    int x=7, y=11, z=11;

    System.out.println(x=+x);System.out.println(y=+y);

    System.out.println(z=+z);

    System.out.println(xy));

    System.out.println(y=y));

    System.out.println(y==z =+(y==z));

    System.out.println(x!=y =+(x!=y));

    }

    }

    x=7

    y=11

    z=11

    xz=false

    y=y =false

    y==z =true

    x!=y =true

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    25/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    25

    Operator PrecedenceOperator Precedence

    Determines the order in which expressions are

    evaluated

    ExampleExampley = 6+ 3*2

    the value of y will be 12 because 3*2 will

    be evaluated first as * has a higher precedence

    than +

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    26/30

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    27/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    27

    Operator PrecedenceOperator Precedence

    Operator Precedence

    &^

    |

    &&||

    ?:= += - = ect.

    &= |=

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    28/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    28

    Operator PrecedenceOperator Precedence

    Notes on Operator Precedence

    General order of evaluation is

    Increment,Decrement operators

    Arithmetic Operators

    Comparison Operators

    Logical Operators,

    Assignment Operators

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    29/30

    [email protected]@cmb.ac.lkUCSC 2005. All rights reserved. No part of this material may be reproduced and sold.

    29

    Operator PrecedenceOperator Precedence

    Notes on Operator Precedence

    Order of evaluation can be changed by usingParenthesis

    (3+2) will beEvaluated FirstEg: (3+2) / 2 +6

  • 8/13/2019 Introduction to programming using JAVA - lecture 3

    30/30