Continue & Break
Autor: MARCO ANTONIO BIDÓIA | Publicado: 2012-05-07T16:30:00-03:00


&nbsp; &nbsp;Neste exemplo vemos qual a aplicação do "continue;" neste caso
vamos fazer um for de 0 até 20 e quando o valor do "i" for maior que
10 e menor que 15 ele interrompe o ciclo e segue para a próxima execução. Aqui
vemos a diferença entre o "continue;" e o "break;".

&nbsp; &nbsp;O "
continue;" passa para a próxima execução do "
for&nbsp;", enquanto o
"break;" interrompe a execução do mesmo.



Exemplo com continue



/**

&nbsp;* @author marco.bidoia@gmail.com

&nbsp;*/

public class ForComContinue {



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] args) {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i
&lt;=20; i++){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (i &gt; 10
&amp;&amp; i &lt; 15){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; continue;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print(i + "-");

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }



}



Resultado da execução acima:

0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 15 - 16 - 17 - 18 - 19 - 20

&nbsp; Vemos que a execução não nos trouxe os números 11, 12, 13, 14 que são maiores que 10 e menos que 15, o "&nbsp;continue;" &nbsp;não interrompe a execução apenas passa para o próximo laço de execução do "for".



&nbsp; O mesmo exemplo só que agora usamos o "break;"&nbsp;em vez do "continue;".



/**

&nbsp;* @author marco.bidoia@gmail.com

&nbsp;*/

public class ForComBreak {



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] args) {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i
&lt;=20; i++){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (i &gt; 10
&amp;&amp; i &lt; 15){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print(i + "-");

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }



}



Resultado da execução acima:


0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10

&nbsp; Neste caso vemos que quando ele encontrou um número que era maior que 10 e menor que 15 ele&nbsp;interrompe a execução imediatamente.&nbsp;


&nbsp; Com estes dois exemplos acima podemos ver a diferença entre o "continue;" e o "break;" que nos trouxeram resultados diferentes sendo utilizados em programas iguais.