...mais estruturas de repetição.
Autor: Luciano da Cunha | Publicado: 2012-02-07T16:10:00-08:00

...cansado dessa estruturas, resolvi não comentar muito..somente dentro dos próprios códigos.


//Demonstra a utilização do do...while().
class DoWhile{

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

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int x = 0;

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Este é o típico loop que será executado incondicionalmente pelo menos
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // uma vez. Note que não há teste antes.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; do {

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.print(x + " " );
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ++x;

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Importante: O valor dentro dos parenteses (expressão) se variável,
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // sua declaração deverá ser feita antes do while.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while ( x &lt;= 50);

&nbsp;&nbsp;&nbsp; }

}

//Demonstra a utilização do while().
class WhileDo{

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

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int x = 0;

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Existem caso que esse loop nunca será executado, pois o teste é feito
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // antes.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (x &lt;= 50)&nbsp; {

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.print(x + " " );
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ++x;

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // O loop é automático, não é necessário e nem permitido uma instrução
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // do.

&nbsp;&nbsp;&nbsp; }

}

//Demonstra a utilização do for básico.
class LoopsFor{
&nbsp;&nbsp;&nbsp; public static void main(String[] args){

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int x = 0;

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // for básico.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 10; ++i){
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // for básico.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.printf("%02d ", i);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; x = i;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* O for básico, apresenta uma estrutura simples, possuindo três seções:
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* - A declaração de uma variável;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* - Controle de repetição, expressão booleana;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* - e incremento;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* Todas são facultativas, obiviamente que a alteração dessa estrutura
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* implicará, em alguns casos, em mudança de comportamento do for.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* for sem declaração de variável, porém, usando uma variável já
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* existente. Perceba que da mesma forma que o if, mesmo não colocando
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* os colchetes, o loop se restringe somente à primeira linha.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*/
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for ( ; x &lt; 20; x++)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.printf("%02d ", x );
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.printf("Segunda linha");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* ...deixando para imprimir a expressão "Segunda linha" somente no fim
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* do loop.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*/
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("");

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* for que explora outras formas na seção da expressão boolena.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* além disso, demonstra outras possibilidades na seção do iterador.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*/
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (int z = x; (z &lt; 30) &amp;&amp; (x&nbsp; &lt; 30); z = ++x)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.printf("%02d ", z );

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("");

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* for que omite a seção da declaração da variável e a seção da
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* expressão booleana. Perceba que para conseguir sair do loop em
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* algum momento, há a necessidade de declaração um condição dentro do
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* bloco, do contrário ocorreria um loop infinito.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*/
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (/** lembra do x? */ ; /** cade a expressão booleana? */; x++){

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.printf("%02d ", x);

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Condição que provoca um saída forçada.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (x &gt; 39){break ;}

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

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* Outras expressões de saída forçada:
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* break;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* return;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* System.exit();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* note que o x foi utilizado por todo o método em razão de ter sido
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;* declarado fora dos loops, o que não o fez uma variável de bloco.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*/
&nbsp;&nbsp;&nbsp; }
}