Question:
plz tell me the difference between break & continue statement in java with examples?
chinu 2
2008-10-19 04:56:11 UTC
pl tell me
Six answers:
anonymous
2008-10-19 05:16:13 UTC
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");

}

}

}



I hope it's clear for you now.



Goldest
santoro
2016-12-26 20:31:01 UTC
Continue Statement Java
earls
2016-09-28 17:02:55 UTC
Java Continue Statement
anonymous
2008-10-19 05:13:41 UTC
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.
Arc
2008-10-19 05:33:43 UTC
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);

}

}

}
atkison
2016-12-03 07:15:02 UTC
destroy ends the loop thoroughly on the present element in execution, and alternatives up on the subsequent training after the top of the loop. proceed stops the present new launch of the loop on the present element in execution, yet particularly than passing to the top of the loop, it is going lower back up and does the loop lower back. this in basic terms isn't the comparable element.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...