The break
Statement
The break
statement has two forms: labeled and unlabeled. You saw the unlabeled form in the previous discussion of the switch
statement. You can also use an unlabeled break
to terminate a for
, while
, or do-while
loop
package breakcountinue; // Break Statement with Label public class Breakcountinue { public static void main(String[] args) { loop: for (int i = 0; i < 5; i++) { System.out.println("Ali"); break loop; System.out.println("Ibad"); } } }
package breakcountinue; public class Breakcountinue { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println("Ali"); break; System.out.println("Ibad"); } } }
The continue
Statement
The continue
statement skips the current iteration of a for
, while
, or do-while
loop. The unlabeled form skips to the end of the innermost loop’s body and evaluates the boolean
expression that controls the loop.
package breakcountinue; public class Breakcountinue { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println("Ali"); continue; System.out.println("Ibad"); } } }