Wednesday, November 13, 2013

Flow Controls - Jump Statements

java supports three jump statements : break, continue, and return,These statements transfer control to another part of your program, Each is examined here.

Note: in additional to the jump statements discussed here, java supports one other way by which you can change your program flow of execution, through exception handling,Exception handling provides a structured method by which run-time error can be trapped and handled by your program, it is supported by the keywords try, catch ,throw , throws and finally 

Using break 

in java the break statement has three users, First, as you have seen, it terminates a statement sequence in a switch statement, Second, it can be used to exit a loop, Third it can be used ad a "civilized " form of goto. the last two uses are explained here,

Using break to exit a loop 

By using break, you can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop, when a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop
class UsingBreak {

    public static void main(String[] args) throws IOException {

       for(int i=0; i<100; i++){
           if (i==10) break;
           System.out.println("i is :"+i);
       }
        System.out.println("loop Complete.");
    }
}
OUTPUT 
i is :0
i is :1
i is :2
i is :3
i is :4
i is :5
i is :6
i is :7
i is :8
i is :9

loop Complete.

As you can see, although the for loop is designed to run form 0 to 99 the break statement causes it to terminated early, when i equals 10
the break statement can be used with any of java's loop, including intentionally infinite loops for example here is the preceding program coded by use of while loop, The output if this is the same as just shown
class UsingBreak {

    public static void main(String[] args) throws IOException {
        int i = 0;
        while (i<100) {
            if (i == 10) break;
            System.out.println("i is :" + i);
            i++;
        }
        System.out.println("loop Complete.");
    }
}
When used inside a set of nested loop, the break statement will only break out of the innermost loop.
class UsingBreak {

    public static void main(String[] args) throws IOException {
        for(int i =0; i<3; i++){
            System.out.print("pass "+i+" : ");
            for(int j=0; j<100; j++){
                if(j==10)break;
                System.out.print(j+" ");
            }
            System.out.println("");
        }
        System.out.println("Loops Complete.");
    }
}
OUTPUT
pass 0 : 0 1 2 3 4 5 6 7 8 9 
pass 1 : 0 1 2 3 4 5 6 7 8 9 
pass 2 : 0 1 2 3 4 5 6 7 8 9 

Loops Complete.

As you can see, the break statement in the inner loop only causes termination of the loop, the outer loop is unaffected,
Here are two other points to remember about break, first more then one break may appear in a loop.
However be careful too many break statements have the tendency to restructure you code, Second the break, that terminates a switch statement affects only the switch statement and not any enclosing loop.

Using continue

Sometime it is useful to force an early iteration of a loop that is, you might to continue running the loop but stop processing the remainder of the code in its body for this particular iteration, this is, in effect, a goto just past the body of the loop, to the loop's end the continue statement performs such an action. In while and do while loops a continue statement causes control to be transferred directly to the conditional expression that controls the loop, in a for loop , control goes first to the iteration portion of the for statement and then to the conditional expression, for statement loops. any intermediate code is by passed, 
Here is an example program that uses continue to cause two numbers to be printed on each line: this code use the % operator to check if i is even, if it is the loop continues without printing a new line .
class UsingContinue {

    public static void main(String[] args) throws IOException {
        for(int i=0; i<10; i++){
            System.out.print(i+" ");
            if(i%2 == 0)continue;
            System.out.println("");
        }
    }
}
OUTPUT
0 1 
2 3 
4 5 
6 7 

8 9 

return 

The return statement is used to explicitly return from a method. That is it causes program control to transfer back to the caller of the method. As such, it is categorized as a jump statement. Although for a full discussion of return, You must until methods are discussed, we present a brief look at return, 
At any time in a method the return statement can be used to cause execution to branch back to the caller of the method. Thus, the return statement immediately terminates the method in which it is executed, The following examples illustrates this point. Here return causes execution to return to the java run-time system,Since it is the run time system that calls main method (  public static void main(String[] args) )
class UsingReturn {

    public static void main(String[] args) throws IOException {
        boolean t = true;
        System.out.println("Befor the return ");
        if(t) return;
        System.out.println("this is not executed");
    }
}

No comments:

Post a Comment