Java-i Lesson3 Op Foc1

download Java-i Lesson3 Op Foc1

of 38

Transcript of Java-i Lesson3 Op Foc1

  • 8/6/2019 Java-i Lesson3 Op Foc1

    1/38

    Lesson3: JAVA

    INTRODUCTION TO JAVA PROGRAMMING

    Operators and Flow of Control

  • 8/6/2019 Java-i Lesson3 Op Foc1

    2/38

    Relational Operator

    The Relational Operators determine therelationship that one operand has to theother.

    e.g. Equality and OrderingOperator Result== Equal to

    != Not equal to

    > Greater than

    < Less than

    >= Greater than or equal to

  • 8/6/2019 Java-i Lesson3 Op Foc1

    3/38

    Boolean Logical Operator

    Boolean logical operators operate only on booleanoperands.Operator Result

    & Logical AND

    | Logical OR^ Logical XOR(exclusive OR)|| Short-Circuit OR&& Short-Circuit AND! Logical Unary NOT== Equal to!= Not Equal to

  • 8/6/2019 Java-i Lesson3 Op Foc1

    4/38

    Boolean Logical Operator

    Logical Operation Effect

    A B A|B A&B A^B !A

    0 0 0 0 0 11 0 1 0 1 0

    0 1 1 0 1 1

    1 1 1 1 0 0

  • 8/6/2019 Java-i Lesson3 Op Foc1

    5/38

    Boolean Logical Operator (Code)

    class Logic{public static void main (String Args[]){

    boolean a = true;

    boolean b = false;

    boolean c = a | b;

    boolean d = a & b;

    boolean e = a ^ b;

    boolean f = (!a & b) | (a & !b);

    boolean g = !a;

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

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

    System.out.println(" a|b=" + c);System.out.println(" a&b=" + d);

    System.out.println(" a^b=" + e);

    System.out.println(" !a&b|a&!b=" + f);

    System.out.println(" !a=" + g);

    }

    } // Code is From The Complete Reference JAVA 2, Herbert Schildt

  • 8/6/2019 Java-i Lesson3 Op Foc1

    6/38

    Flow of Control

    Sequence

    Selection

    Iteration

  • 8/6/2019 Java-i Lesson3 Op Foc1

    7/38

    Sequence

    Key points:

    order of operations may or may not beimportant

    order of evaluation within a singlestatement can be critical

    example:a = b++;

    a = b = read(input);

  • 8/6/2019 Java-i Lesson3 Op Foc1

    8/38

    Order of precedence

    parentheses before operators

    then multiplication and division

    then addition and subtraction left to right for equal precedence ops

    RHS of an assignment before LHS

    if in doubt use brackets use brackets anyway (to clarify to other

    readers!)

  • 8/6/2019 Java-i Lesson3 Op Foc1

    9/38

    Selection

    if (condition) {

    statement1;

    }else {

    statement2;

    }

  • 8/6/2019 Java-i Lesson3 Op Foc1

    10/38

    Selection

    if (condition){

    statement1; condition must

    } be of type boolean

    else {

    statement2;}

  • 8/6/2019 Java-i Lesson3 Op Foc1

    11/38

    Selection

    if (condition) {

    statement1;

    }else {

    statement2;

    } else branch is optional

  • 8/6/2019 Java-i Lesson3 Op Foc1

    12/38

    Selection

    if (condition) {

    statement1;

    }

    else {

    statement2;

    } braces may be

    omitted if they onlycontain a single

    statement

  • 8/6/2019 Java-i Lesson3 Op Foc1

    13/38

    Selection

    if (condition)

    statement;

    Brackets around theCondition are

    essential

  • 8/6/2019 Java-i Lesson3 Op Foc1

    14/38

    Selection

    if (condition1) {

    if (condition2) {

    statement1;

    }

    else {statement2;

    }

    }

    else {statement3;

    }

    if statements can be

    nested indentation ignored by

    the compiler but helps

    (or can mislead!) humanreaders

  • 8/6/2019 Java-i Lesson3 Op Foc1

    15/38

    Example conditions

    if ((i < j) && (eof != true)) .

    if ((i < j) && !eof) .

    if ((i < j) && !eof()) .

    if ((n = input.read())== 0)

    Dont use = when you mean ==

  • 8/6/2019 Java-i Lesson3 Op Foc1

    16/38

    Example Program

    class Larger {public static void main(String[] args)

    {int x=20;int y=20;

    if(x >= y){if (x==y){System.out.println("x="+x+" and y="+y+ " are Equal");

    }else {

    System.out.println("Both are unequal");}

    System.out.println(x+" is the larger.");}

    else {System.out.println(y+" is the larger.");}

    }}

    l 2

  • 8/6/2019 Java-i Lesson3 Op Foc1

    17/38

    Example 2class Largest {

    public static void main(String[] args){

    // Read three integers and print which is the larger.

    SimpleInput keyboard = new SimpleInput();

    System.out.println("Please type three integers.");

    int x = keyboard.nextInt(), y = keyboard.nextInt(), z = keyboard.nextInt();

    // A variable to hold the answer.

    int largest;

    if(x >= y){ // It must be either x or z.

    if(x >= z){

    largest = x;}

    else{

    largest = z;

    }

    }

    else{ // It must be either y or z.

    if(y >= z){

    largest = y;

    }

    else{

    largest = z;

    }

    }

    System.out.println("The largest is: "+largest);

    }} // Program Code is courtesy of David J. Barnes.

  • 8/6/2019 Java-i Lesson3 Op Foc1

    18/38

    Practice Exercise

    int month=10;

    String season;If (month==12 || month ==1 || month ==2)

    season =Winter;

    else if(month==3 || month ==4 || month ==5)

    season=Spring;else if(month==6 || month ==7 || month ==8)

    season=Summer;

    else if(month==9 || month ==10 || month ==11)

    season=Autumn;elseseason= Bogus Month;

    System.out.println(October is in the +season +.);

  • 8/6/2019 Java-i Lesson3 Op Foc1

    19/38

    Practice Exercise Continue

    Write a Java Programme which will tellwhether X (a variable) is Even or Odd.

    Write a Java Programme which will specifywhether a birth year was in 1950s, 1960s,1970s, 1980s, 1990s.

  • 8/6/2019 Java-i Lesson3 Op Foc1

    20/38

    Selection-switch

    switch (Condition){

    Case 1:Statement1;

    Case 2:Statement 2;Break;........

    Default:Statement n;Break;

    }

    Condition should be int,short, byte or a char type

    Switch Selection is

    replacement of If-else for

    multiple selections and for

    multiple conditions.

  • 8/6/2019 Java-i Lesson3 Op Foc1

    21/38

    Selection-switch

    switch (Condition){

    Case 1:Statement1;

    Case 2:Statement 2;break;........

    default:Statement n;Break;

    }

    Break Used to break the

    condition

    If none of the Case

    Statement satisfies

    Default condition will

    work

    This Should be an

    Integer or Constant

    value

    Remember this is

    COLON sign notSemiColon or Delimeter

  • 8/6/2019 Java-i Lesson3 Op Foc1

    22/38

    Example-switch Statement

    class switchT{

    public static void main(String[] Args){

    int x=1;

    switch(x){case 1:

    System.out.println("Out put is 1: "+x);case 2:

    System.out.println("Out put is 2");break;

    case 3:System.out.println("Out put is 3");break;

    default:

    System.out.println("We were expecting X");break;

    }}

    }

  • 8/6/2019 Java-i Lesson3 Op Foc1

    23/38

    Practice Switch Statement

    int month=10;String season;switch(month){case 12:case 1:case 2:season=Winter;

    break;}System.out.println(October is in the + season+.);

  • 8/6/2019 Java-i Lesson3 Op Foc1

    24/38

    Iteration

    while simplest

    for most common

    do .. While uncommon

  • 8/6/2019 Java-i Lesson3 Op Foc1

    25/38

    While Statement

    while ( condition)

    {

    body statements;

    }

    while the conditionevaluates to true,repeatedly loop executingthe body statements

    condition is tested

    before each passthrough the body

    body statements should

    have some effect on thecondition or you could

    loop indefinitely

  • 8/6/2019 Java-i Lesson3 Op Foc1

    26/38

    While Statement

    int a=10;

    while (a > 0) {

    System.out.println (a);a = a-1;

    }

    System.out.println (You Lost The Game");

  • 8/6/2019 Java-i Lesson3 Op Foc1

    27/38

    Practice While Statement

    int a=10;while (a > 0) {

    System.out.println (a);

    while(a>0 && a

  • 8/6/2019 Java-i Lesson3 Op Foc1

    28/38

    Practice While Statement

    int n=10;

    while (n != 1) {

    System.out.println (n);

    if (n%2 == 0) { // n is even

    n = n / 2;

    }

    else { // n is odd

    n = n*3 + 1;

    }

    }

    // Work Out Yourself to find the Output

  • 8/6/2019 Java-i Lesson3 Op Foc1

    29/38

    do... while Statement

    do {

    body statements;

    }while ( condition);

    similar to while statement

    BUT condition is tested after

    each pass through the body

    so body statements will

    always be executed at least

    once

    body statements should

    have some effect on the

    condition or you could loop

    indefinitely

  • 8/6/2019 Java-i Lesson3 Op Foc1

    30/38

    do...while statement

    int a=10;

    do{

    System.out.println (a);a = a-1;

    } while (a > 0);

    System.out.println (You Lost The Game");

  • 8/6/2019 Java-i Lesson3 Op Foc1

    31/38

    for statement

    for (initialisation; while condition; end of loop operation) {

    body statements;

    }

    for KeywordVariable Initialisation

    Loop Condition

    Loop termination Condition

  • 8/6/2019 Java-i Lesson3 Op Foc1

    32/38

    for statement

    most convenient method for

    counter based iterations

    sequence of execution:

    initialisation

    is while condition true?

    if so, execute body statements execute end of loop operation

    is while condition true

    etc

  • 8/6/2019 Java-i Lesson3 Op Foc1

    33/38

    for statement Difference and common features ofwhile and for loop

    for (INITIALIZER; CONDITION; INCREMENTOR) {

    BODY

    }

    INITIALIZER;

    while (CONDITION) {

    BODY

    INCREMENTOR

    }

  • 8/6/2019 Java-i Lesson3 Op Foc1

    34/38

    for statement

    for (int i=0; i

  • 8/6/2019 Java-i Lesson3 Op Foc1

    35/38

    for statement

    break statement has importance in loop

    statements

    After reading break statement our compiler

    came out of loop and read the next statement

    after loop

  • 8/6/2019 Java-i Lesson3 Op Foc1

    36/38

    Practice for statementint number;

    boolean flag = true;

    number = 4;

    for(int i=2; i

  • 8/6/2019 Java-i Lesson3 Op Foc1

    37/38

    Nested for Loops Like any other programming Language Java does

    allow loops

    for(int i=0; i

  • 8/6/2019 Java-i Lesson3 Op Foc1

    38/38

    Extra Information on for Loops

    you can nest loops

    you can break execution and jump outside

    the loop