1011.Flow Control

Embed Size (px)

Citation preview

  • 8/6/2019 1011.Flow Control

    1/23

    Flow

    Control

  • 8/6/2019 1011.Flow Control

    2/23

    If -- Statement

    This is the simplest form of If statement. When we

    have only one condition to evaluate, we should usethis statement.

    if (expression)Statement;

    int a = 4if

    a>4

    printGreater than four

  • 8/6/2019 1011.Flow Control

    3/23

  • 8/6/2019 1011.Flow Control

    4/23

    If else -- Statement

    When we have two conditions to evaluate, we should

    use this form of if statement.

    int a = 4

    if

    a>4

    print Less than

    four

    print Greater than

    four

    Y

    N

    if (expression)

    {

    Statements;

    }

    else

    {

    Statements;

    }

  • 8/6/2019 1011.Flow Control

    5/23

    Example:

    // program displaying if else statement

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

    int a =4;if(a>4)

    System.out.println( Greater than four);else

    System.out.println( Less than four);}

    }

  • 8/6/2019 1011.Flow Control

    6/23

    If else if -- Statement

    When we have more than two conditions to evaluate,we should use this form of if statement.

    if (expression1) {

    Statements;

    }

    else if (expression2) {

    Statements;

    }

    else {

    Statements;

    }

  • 8/6/2019 1011.Flow Control

    7/23

    NY

    N

    int m

    if

    m>40

    Print Fail

    Y

    Read m

    if m > 60

    Print Ist Div Print IInd Div

    Exit

  • 8/6/2019 1011.Flow Control

    8/23

    Example:// program displaying if else ifstatement

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

    int marks = 86;

    if(marks > 40)System.out.println( Fail);

    else if(marks < 60)System.out.println( IInd Div);

    elseSystem.out.println( Ist Div);

    }}

  • 8/6/2019 1011.Flow Control

    9/23

    Else If Ladder

    When we have multiple conditions to evaluate, weshould use this form of if statement.

    if (expression1)

    Statements;

    else if (expression2)

    Statements;

    else if (expression2)

    Statements;else

    Statements;

  • 8/6/2019 1011.Flow Control

    10/23

    N

    N

    N

    N

    Y

    Y

    Y

    Ym < 60

    m < 70

    m < 80

    Grade A Grade O

    m < 90

    Grade B

    Grade C

    Grade D

    Exit

  • 8/6/2019 1011.Flow Control

    11/23

    Example:// program displaying else if Ladder

    public class ElseIfLadder {

    public static void main(String args[]) {int marks = 86;if(marks < 60)

    System.out.println(Grade - D);

    else if(marks < 70)System.out.println(Grade - C);

    else if(marks < 80)

    System.out.println( Grade - B);

    else if(marks < 90)System.out.println( Grade - A);else

    System.out.println( Grade - O);}

    }

  • 8/6/2019 1011.Flow Control

    12/23

    Switch Statement

    Switch statement is a

    multiple branchingdecision construct based

    on a number of conditions.

    It tests whether anexpression matches one ofthe numbers of constant

    values and the control is

    transferred to one of themany possible points.

    switch(expression) {

    case 1: { statements

    ..

    break;

    }case 2: { statements

    ..

    break;

    }

    default: { statements

    ..

    break;

    }

    }

  • 8/6/2019 1011.Flow Control

    13/23

    switch(a)

    a=1 a=2 a=3 a=4default

    One Two Three Four default

    EXIT

  • 8/6/2019 1011.Flow Control

    14/23

    Example:

    // program displaying switch Statementpublic class SwitchStat {

    public static void main(String args[]) {int num = 3;switch(a) {

    case 1: { System.out.println(ONE);

    break;}

    case 2: {System.out.println(TWO);break;

    }case 3: {System.out.println(THRRE);

    break;}

    case 4: {System.out.println(FOUR);

    break;}default: {System.out.println(INVALID VALUE);

    break;}

    }

    }

  • 8/6/2019 1011.Flow Control

    15/23

    LoopConstructor Looping

    Loop constructs are used to executecertain block of code repeatedly based

    on a condition.

    There are three types of loops in Java:

    ` for loop

    `while loop` do .. while loop

  • 8/6/2019 1011.Flow Control

    16/23

    For Statement

    ` Used to repeat an action for predeterminednumber of times

    ` Fixed iterations -- Lower bound and the upperbound are fixed.Syntax offor loop

    for(initialization; condition; updation) {

    Statements;

    }

    First expression is initialization, which is executed only once when

    the loop starts and is used to initialize loop variables. Second

    expression is condition expression, which is tested before each

    iteration of the loop. Third expression is an update expression and it

    is executed each time after the loop body executed.

  • 8/6/2019 1011.Flow Control

    17/23

    Example:// program displaying for Statement

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

    int num = 2;for(int i=0;i

  • 8/6/2019 1011.Flow Control

    18/23

    While Statement

    ` Although the task of while and for loops issame, the for loop is generally used when thenumber of iterations are known in advance and

    while loop is used where number of iterations

    are not known.

    First the condition is evaluated; if it is true then the statement in the body of the loop

    are executed. After the execution, again the condition is checked and if it is found to

    be true then again the statements in the body of loop are executed. This means that

    these statements are executed continuously till the condition is true and when it

    becomes false, the loop terminates and the control comes out of the loop.

    Syntax while loop

    while(condition) {

    Statements;.

    }

  • 8/6/2019 1011.Flow Control

    19/23

    Example:

    // program displaying for Statementpublic class WhileStat {

    public static void main(String args[]) {int num=2, i=1;

    while(i

  • 8/6/2019 1011.Flow Control

    20/23

    Do .. While Statement

    ` Used to implement infinite looping` First, the statements inside loop body are

    executed and then the condition is evaluated. Ifthe condition is true, then again the loop body is

    executed and this process continues until thecondition becomes false.

    Syntax while loop

    do { Statements;.

    } while(condition) ;

  • 8/6/2019 1011.Flow Control

    21/23

    Example:

    // program displaying for Statementpublic class DoWhileStat {

    public static void main(String args[]) {int num=2, i=1;do {

    System.out.println(Num = + num);num = num * i;i++;

    } while(i

  • 8/6/2019 1011.Flow Control

    22/23

    Break Statement

    ` The break statement terminates theinner loop and executes the statements,

    written after it.

    ` It can be used with while, until and forloops.

  • 8/6/2019 1011.Flow Control

    23/23

    Break Statement

    while truedo

    ..

    ..

    if then

    break

    fi

    ..

    done

    commands