Skip to main content

Decision Making and Branching Statements / Control Statements

 Introduction

    Java is a set of statements which executed sequentially in the order in which they appear. This will happen when option or repetitions of certains are not necessary. However, We have a number of situation, where we may have to change the order of execution of statement on bases of certain conditions. This involves a kind of dicision making to see the particular condition occured or not and the direction of execution will execute according to it.

    When program braks the sequential flow and jump on the another part of code then it's called branching. When branching is on particular condition then it's called conditional branching. If branches executes without any particular conditions then it's call unconditional branching.

    Java supports such decision making capabilities and supports following statements known as Control statement or Decision making statement.

  1.     if statements
  2.     switch statement
  3.     conditional operator statement.

1. if statement:

    The if statement is powerful decision making statement and is use to control the flow of execution of statements.It is basically two-way decision statement base on condition or expression. If statement may be implemented in different forms depending on complexity of conditions to be tested.

  • Simple if statement
  • if....else statement
  • Nested if.....else statement
  • else if ladder.

syntax:

    if(text condition){

        statement-block

    }

    statements;

The statement-block may be a single statement or group of statements. If the text condition is true then statement-block will executed, otherwise statement-block will skipped and other statemnts will executed.

                                                    Fig. Simple if control

if....else statement:

    The if....else statement is an extension of simple if statement. The general form is:

if(text condition){

    true-block;

}

else{

     false block;

}

statement-x;

    If the text condition is true, then true blok will execute otherwise false block is executed after that the statement-x will executed.


nested if....else statement:
    This is involved of if....else statement.

2. Switch Statement:
    Java has developed built-in multiway decision statement known as swithch. The switch statement tests the value of given variable agains a list of case values and match found, a block of statement asociated with that case is executed otherwise default case will executed. 
switch (expression){
    case value-1:
            block 1;
            break;
    case value-2:
            block 2
            break;
    .
    .
    .
    case value-n:
            block n;
            break;
    default:
            default block;
            break;
}
statement-x;
The expression is an integer expression or characters. value-1, value-2, .... , value-n are constants or constant expression. When it mach then statements associated with that constant is executed otherwise default block will executed.

3. Conditional Operator ( ? : operator)
    The java language ha an unusal operator which is making for two-way decisions. This operator is combination of ? and : and this is commanly called as conditional operators.
  exp1 ? exp2 : exp 3
Here the exp1, exp,2 and exp3 are the expression. In normal if....else function is as follows:
if(a>b)
    x=a;
else
    x=b;
In above example if first statement, a>b is true then the a value will assign to x, if first condition, a>b is false b value will be assign to x. We can do the same thing into conditional operators like,
        x=(a>b) ? a : b;

Comments