Break & Continue in Java


By using break,you can force immediate termination of a loop, bypassing the conditional expression and any remaininh code in the body of the loop.When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.

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.

Source Code

public class break_continue {
    public static void main(String args[]) {
        for (int i = 1; i <= 10; i++) {
            if(i==5)
                continue;
            System.out.println(i);
            if(i==8)
                break;
        }
    }
}
 

Output

1
2
3
4
6
7
8
To download raw file Click Here

Basic Programs