Translate

Monday 18 August 2014

Break and Continue statement

Break:-
It is used to skip remaining statement of the loop and shift the control out of the loop or switch.
e.g.
while(condition)
{
    ....
    ....
    while(condition)
    {
        ....
        ....
        break;                        // breaks inner loop
    }
    break;                            //breaks outer loop
}

Unreachable code is not allowed in java it is compile time error.
while(condition)
{
    ....
    ....
    break;      
    ....                 //This statement is always unreachable as break will break the loop.
}                       // This is not allowed in java.

Continue:-
Continue is used to skip remaining statements of the loop and perform next iteration.
e.g.
while(condition)
{
    ....
    ....
    if(condition)
        continue;    //This will start the loop again.
    ....
    ....
}

Continue should never be used without condition otherwise loop will become infinite loop.

No comments:

Post a Comment

Total Pageviews