You are on page 1of 4

In programming,

break means breaking out of loop and proceed the syntax next to loop
continue means continue the loop skipping syntaxes below continue within loop..
Eg::
for (int i=0;i<=5;i++)
{
if(i==3) continue;
System.out.println(i);
}
o/p -> 0 1 2 4 5 note:: at i=3 print i; is skipped and loop is continued
for (int i=0;i<=5;i++)
{
if(i==3) break;
System.out.println(i);
}
o/p -> 0 1 2 note:: at i=3 loop will not continue at all.

A continue statement stops the iteration of a loop (while, do or for) and causes execution to resume at
the top of the nearest enclosing loop. You use a continue statement when you do not want to execute
the remaining statements in the loop, but you do not want to exit the loop itself.
eg.
public class ContinueExample
{
public static void main(String[] args) {
System.out.println("Odd Numbers");
for (int i = 1; i <= 10; ++i) {
if (i % 2 == 0)
continue;
// Rest of loop body skipped when i is even
System.out.println(i + "\t");
}
}
}

The break statement transfers control out of the enclosing loop ( for, while, do or switch statement).
You use a break statement when you want to jump immediately to the statement following the
enclosing control structure.
eg.
public class BreakExample
{
public static void main(String[] args) {
System.out.println("Numbers 1 - 10");
for (int i = 1;; ++i) {
if (i == 11)
break;
// Rest of loop body skipped when i is eleven
System.out.println(i + "\t");
}
}
}

The break statement tells Java to exit a code block defined by opening and closing braces and used
in a loop or a switch statement.
Example:
class Demo {
public static void main (String args[]) {
char a = 'B'
switch(90) {
case 100:
System.out.println("100");
break;
case 90:
switch(a) {
case 'a':
System.out.println("A");
break;
case 'b':
System.out.println("B");
break;
}
case 80:
System.out.println("80");
break;
default:
System.out.println("No match.");
}
}
}
################
The continue statement is used within the body of a loop to tell Java to immediately return to the top
of the loop without executing statements that appear below the continue statement.
Example:
class Demo {
public static void main (String args[]) {
int x = 0;
while(x < 5) {
System.out.println("Before continue x = " + x);
x++;
if(x == 3)
continue;
System.out.println("After continue x = " + x);
}

}
}

You might also like