introduction to programming using java - lecture 4

Upload: rochana-ramanayaka

Post on 04-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

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

    1/20

    Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    1

    Introduction to ProgrammingIntroduction to Programming((Using Java)Using Java)

    Nandika KasunNandika Kasun

    University of Colombo School of Computing (UCSC)University of Colombo School of Computing (UCSC)

    University of ColomboUniversity of Colombo

    Sri LankaSri Lanka

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

    2/20

    Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    2

    Control StatementsControl Statements

    Control Statements

    If ....Else Statements

    if ( Expression )

    statement1;

    else

    statement2;

    If Expression true then statement1 is executed;

    otherwise statement2 is executed. else is optional.

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

    3/20

    Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    3

    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 4

    4/20

    Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    4

    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 4

    5/20

    Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    5

    Control StatementsControl Statements

    The switch Statement

    switch (Test) {case valueOne :

    resultOne;

    break;case valueTwo :

    resultTwo;

    break;

    default : defaultResult;

    }

    Variable orExpression

    that evaluates

    tobyte,char orint

    cannot be float,

    long String

    or any other

    object

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

    6/20

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

    7/20Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    7

    Control StatementsControl Statements

    The switch Statement contd..

    Multiple statements within a case value can be used

    without curly brackets

    the test condition is limited to testing simple equality

    unlike in if statements

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

    8/20Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    8

    The do . while Statement

    do {statement 1;

    statement 2;..

    ..

    statement n} while (Boolean expression)

    Control StatementsControl Statements

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

    9/20Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    9

    The do . while Statement contd..

    The enclosed Statements will be executed

    at least once irrespective of the Boolean

    expressions result.

    As long as the Boolean expressions yieldstrue the Statements will be repeated.

    The curly brackets are not necessary for asingle Statement.

    Control StatementsControl Statements

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

    10/20Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    10

    Boolean

    Expression ?

    Statement 1;Statement 2;

    ..

    Statement n;True

    False

    The do..whileLoop

    do while statement

    Control StatementsControl Statements

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

    11/20Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    11

    The while Statement

    while (Boolean expression) {statement 1;

    statement 2;..

    ..

    Statement n}

    Control StatementsControl Statements

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

    12/20Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    12

    The while Statement

    TheBoolean expression is checked first.

    If it yields true, Statements 1 to n areexecuted.

    Boolean expression is once again checked. if yields true Statements are executed again.

    This process is repeated tillBooleanexpression yields false.

    Control StatementsControl Statements

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

    13/20Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    13

    BooleanExpression ?

    Statement 1;Statement 2;

    ..

    Statement n;

    True

    False

    The while Loop

    Control StatementsControl Statements

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

    14/20Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    14

    The for Statement

    for (initilization; Boolean expression; increment) {

    statement 1;

    statement 2;..

    ..Statement n

    }

    If there is only

    a single statementCurly brackets are

    not necessary

    Control StatementsControl Statements

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

    15/20

    Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    15

    The for Statement

    Initialization;Boolean Expression ;Increment)for (

    Statement 1..

    Statement n

    {

    1

    4

    3

    2

    false

    true

    5

    Steps 3,4 & 5

    is repeated till

    expression becomes

    false

    Control StatementsControl Statements

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

    16/20

    Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    16

    Jump StatementsJump Statements

    break statementjumps out of a loop and effectively bypass

    the loop condition.

    continue statement

    jumps out of the current iteration of the loop

    return statement

    terminates the method (function). Jumps to

    the place immediately after the function call.

    (break, continue,(break, continue, return)return)

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

    17/20

    Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    17

    Labeled loops

    A label breakstatement terminates the current

    loop and proceeds to the first statement thatfollows the loop that is labeled by the identifierthat follows the keyword break.

    for (int i=0; i

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

    18/20

    Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    18

    RecursionRecursion

    Methods that invoke themselves are known as

    Recursive methods.

    Recursion Example

    class Factorial {

    static long calcFact(int n) {

    if n < 2

    return 1;return n*calcFact(n-1);

    {

    }Recursive call

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

    19/20

    Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    19

    RecursionRecursion

    Class TestFactorial {public static void main (String args[ ]) {

    System.out.print(Factorial of 5 is);

    System.out.println( Factorial.calcFact(5));

    }

    }

    To test the Factorial Class

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

    20/20

    Kasun@[email protected] 2003. All rights reserved. No part of this material may be reproduced and sold.

    20

    SummarySummary

    Exercise:Exercise:

    Implement a for loop using a while loopImplement a for loop using a while loopWrite a program which will display the followingsWrite a program which will display the followings

    **

    **********

    ********

    **

    ******

    **********

    This week we learned control statements