Testes Booleanos e Loops
Autor: Frutei - Espaço Saúde | Publicado: 2012-03-04T06:40:00-08:00


Testes Booleanos 



Uma condição booleana é qualquer expressão que retorne true ou false. Para criar um teste booleano simples que verifique o valor de uma variável podemos usar apenas alguns operadores de comparação como: 



&lt; (menor) 

&gt; (maior)

&lt; = (menor ou igual)

&gt; = (maior ou igual) 

= = (igual)



Observação: Observe a diferença entre o operador de atribuição ( apenas um sinal de igualdade) e o operador igual ( dois sinais de igualdade).

&nbsp; 

O If-Else



A sintaxe do if no Java é a seguinte:



&nbsp;&nbsp;&nbsp;&nbsp; if (condicaoBooleana) {



  


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; codigo;



  


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

Exemplo:



&nbsp;&nbsp;&nbsp;&nbsp; int idade =&nbsp; 10; &nbsp;&nbsp; 



  


&nbsp;&nbsp;&nbsp;&nbsp; if (idade &lt; 18) {



  


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Nao
pode entrar");



  


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



Além disso, podemos usar a cláusula else para indicar o comportamento que deve ser executado no caso da expressao booleana ser falsa.




<!--
 /* Font Definitions */
@font-face
 {font-family:"ＭＳ 明朝";
 mso-font-charset:78;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:1 134676480 16 0 131072 0;}
@font-face
 {font-family:"Cambria Math";
 panose-1:2 4 5 3 5 4 6 3 2 4;
 mso-font-charset:0;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:3 0 0 0 1 0;}
@font-face
 {font-family:Cambria;
 panose-1:2 4 5 3 5 4 6 3 2 4;
 mso-font-charset:0;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:-536870145 1073743103 0 0 415 0;}
@font-face
 {font-family:Monaco;
 panose-1:2 0 5 0 0 0 0 0 0 0;
 mso-font-charset:0;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:3 0 0 0 1 0;}
 /* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
 {mso-style-unhide:no;
 mso-style-qformat:yes;
 mso-style-parent:"";
 margin-top:0cm;
 margin-right:0cm;
 margin-bottom:10.0pt;
 margin-left:0cm;
 mso-pagination:widow-orphan;
 font-size:12.0pt;
 font-family:Cambria;
 mso-ascii-font-family:Cambria;
 mso-ascii-theme-font:minor-latin;
 mso-fareast-font-family:"ＭＳ 明朝";
 mso-fareast-theme-font:minor-fareast;
 mso-hansi-font-family:Cambria;
 mso-hansi-theme-font:minor-latin;
 mso-bidi-font-family:"Times New Roman";
 mso-bidi-theme-font:minor-bidi;
 mso-ansi-language:PT-BR;
 mso-fareast-language:JA;}
.MsoChpDefault
 {mso-style-type:export-only;
 mso-default-props:yes;
 font-family:Cambria;
 mso-ascii-font-family:Cambria;
 mso-ascii-theme-font:minor-latin;
 mso-fareast-font-family:"ＭＳ 明朝";
 mso-fareast-theme-font:minor-fareast;
 mso-hansi-font-family:Cambria;
 mso-hansi-theme-font:minor-latin;
 mso-bidi-font-family:"Times New Roman";
 mso-bidi-theme-font:minor-bidi;
 mso-fareast-language:JA;}
.MsoPapDefault
 {mso-style-type:export-only;
 margin-bottom:10.0pt;}
@page WordSection1
 {size:612.0pt 792.0pt;
 margin:72.0pt 90.0pt 72.0pt 90.0pt;
 mso-header-margin:36.0pt;
 mso-footer-margin:36.0pt;
 mso-paper-source:0;}
div.WordSection1
 {page:WordSection1;}
-->









&nbsp;&nbsp;&nbsp; int idade = 10;&nbsp; 





&nbsp;&nbsp;&nbsp; if (idade &lt; 18) {





&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Nao
pode entrar");





&nbsp;&nbsp;&nbsp; } else {





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





&nbsp;&nbsp;&nbsp; }



&nbsp;O While



O while é um comando usado para fazer um laço (loop), isto é, repetir um trecho de código algumas vezes. A idéia é que esse trecho de código seja repetido enquanto uma determinada condição permanecer verdadeira. Exemplo:




<!--
 /* Font Definitions */
@font-face
 {font-family:"ＭＳ 明朝";
 mso-font-charset:78;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:1 134676480 16 0 131072 0;}
@font-face
 {font-family:"Cambria Math";
 panose-1:2 4 5 3 5 4 6 3 2 4;
 mso-font-charset:0;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:3 0 0 0 1 0;}
@font-face
 {font-family:Cambria;
 panose-1:2 4 5 3 5 4 6 3 2 4;
 mso-font-charset:0;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:-536870145 1073743103 0 0 415 0;}
@font-face
 {font-family:Monaco;
 panose-1:2 0 5 0 0 0 0 0 0 0;
 mso-font-charset:0;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:3 0 0 0 1 0;}
 /* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
 {mso-style-unhide:no;
 mso-style-qformat:yes;
 mso-style-parent:"";
 margin-top:0cm;
 margin-right:0cm;
 margin-bottom:10.0pt;
 margin-left:0cm;
 mso-pagination:widow-orphan;
 font-size:12.0pt;
 font-family:Cambria;
 mso-ascii-font-family:Cambria;
 mso-ascii-theme-font:minor-latin;
 mso-fareast-font-family:"ＭＳ 明朝";
 mso-fareast-theme-font:minor-fareast;
 mso-hansi-font-family:Cambria;
 mso-hansi-theme-font:minor-latin;
 mso-bidi-font-family:"Times New Roman";
 mso-bidi-theme-font:minor-bidi;
 mso-ansi-language:PT-BR;
 mso-fareast-language:JA;}
.MsoChpDefault
 {mso-style-type:export-only;
 mso-default-props:yes;
 font-family:Cambria;
 mso-ascii-font-family:Cambria;
 mso-ascii-theme-font:minor-latin;
 mso-fareast-font-family:"ＭＳ 明朝";
 mso-fareast-theme-font:minor-fareast;
 mso-hansi-font-family:Cambria;
 mso-hansi-theme-font:minor-latin;
 mso-bidi-font-family:"Times New Roman";
 mso-bidi-theme-font:minor-bidi;
 mso-fareast-language:JA;}
.MsoPapDefault
 {mso-style-type:export-only;
 margin-bottom:10.0pt;}
@page WordSection1
 {size:595.0pt 842.0pt;
 margin:72.0pt 90.0pt 72.0pt 90.0pt;
 mso-header-margin:35.4pt;
 mso-footer-margin:35.4pt;
 mso-paper-source:0;}
div.WordSection1
 {page:WordSection1;}
-->









&nbsp;&nbsp;&nbsp;&nbsp; int idade = 15;





&nbsp;&nbsp;&nbsp;&nbsp; while ( idade &lt; 18) {





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





&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; idade = idade + 1;





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









O trecho dentro do bloco while será executado até o momento em que a condição idade &lt; 18 passe a ser falsa. E isso ocorrerá no momento em que idade == 18.&nbsp;



O For

Outro comando de loop extremamente utilizado é o for. A idéia é a mesma do while, fazer um trecho de código ser repetido enquanto uma condição continuar verdadeira. Mas além disso, o for isola também um espaço para inicialização de variáveis e o modificador dessas variáveis. Isso faz com que fiquem mais legíveis, as variáveis que são relacionadas ao loop.
 














<!--
 /* Font Definitions */
@font-face
 {font-family:"ＭＳ 明朝";
 mso-font-charset:78;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:1 134676480 16 0 131072 0;}
@font-face
 {font-family:"Cambria Math";
 panose-1:2 4 5 3 5 4 6 3 2 4;
 mso-font-charset:0;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:3 0 0 0 1 0;}
@font-face
 {font-family:Cambria;
 panose-1:2 4 5 3 5 4 6 3 2 4;
 mso-font-charset:0;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:-536870145 1073743103 0 0 415 0;}
@font-face
 {font-family:Monaco;
 panose-1:2 0 5 0 0 0 0 0 0 0;
 mso-font-charset:0;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:3 0 0 0 1 0;}
 /* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
 {mso-style-unhide:no;
 mso-style-qformat:yes;
 mso-style-parent:"";
 margin-top:0cm;
 margin-right:0cm;
 margin-bottom:10.0pt;
 margin-left:0cm;
 mso-pagination:widow-orphan;
 font-size:12.0pt;
 font-family:Cambria;
 mso-ascii-font-family:Cambria;
 mso-ascii-theme-font:minor-latin;
 mso-fareast-font-family:"ＭＳ 明朝";
 mso-fareast-theme-font:minor-fareast;
 mso-hansi-font-family:Cambria;
 mso-hansi-theme-font:minor-latin;
 mso-bidi-font-family:"Times New Roman";
 mso-bidi-theme-font:minor-bidi;
 mso-ansi-language:PT-BR;
 mso-fareast-language:JA;}
.MsoChpDefault
 {mso-style-type:export-only;
 mso-default-props:yes;
 font-family:Cambria;
 mso-ascii-font-family:Cambria;
 mso-ascii-theme-font:minor-latin;
 mso-fareast-font-family:"ＭＳ 明朝";
 mso-fareast-theme-font:minor-fareast;
 mso-hansi-font-family:Cambria;
 mso-hansi-theme-font:minor-latin;
 mso-bidi-font-family:"Times New Roman";
 mso-bidi-theme-font:minor-bidi;
 mso-fareast-language:JA;}
.MsoPapDefault
 {mso-style-type:export-only;
 margin-bottom:10.0pt;}
@page WordSection1
 {size:595.0pt 842.0pt;
 margin:72.0pt 90.0pt 72.0pt 90.0pt;
 mso-header-margin:35.4pt;
 mso-footer-margin:35.4pt;
 mso-paper-source:0;}
div.WordSection1
 {page:WordSection1;}
-->









&nbsp;&nbsp;&nbsp;&nbsp; for (inicializacao; condigcao; incremento) {



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; codigo;



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






 


Exemplo:

















<!--
 /* Font Definitions */
@font-face
 {font-family:"ＭＳ 明朝";
 mso-font-charset:78;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:1 134676480 16 0 131072 0;}
@font-face
 {font-family:"Cambria Math";
 panose-1:2 4 5 3 5 4 6 3 2 4;
 mso-font-charset:0;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:3 0 0 0 1 0;}
@font-face
 {font-family:Cambria;
 panose-1:2 4 5 3 5 4 6 3 2 4;
 mso-font-charset:0;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:-536870145 1073743103 0 0 415 0;}
@font-face
 {font-family:Monaco;
 panose-1:2 0 5 0 0 0 0 0 0 0;
 mso-font-charset:0;
 mso-generic-font-family:auto;
 mso-font-pitch:variable;
 mso-font-signature:3 0 0 0 1 0;}
 /* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
 {mso-style-unhide:no;
 mso-style-qformat:yes;
 mso-style-parent:"";
 margin-top:0cm;
 margin-right:0cm;
 margin-bottom:10.0pt;
 margin-left:0cm;
 mso-pagination:widow-orphan;
 font-size:12.0pt;
 font-family:Cambria;
 mso-ascii-font-family:Cambria;
 mso-ascii-theme-font:minor-latin;
 mso-fareast-font-family:"ＭＳ 明朝";
 mso-fareast-theme-font:minor-fareast;
 mso-hansi-font-family:Cambria;
 mso-hansi-theme-font:minor-latin;
 mso-bidi-font-family:"Times New Roman";
 mso-bidi-theme-font:minor-bidi;
 mso-ansi-language:PT-BR;
 mso-fareast-language:JA;}
.MsoChpDefault
 {mso-style-type:export-only;
 mso-default-props:yes;
 font-family:Cambria;
 mso-ascii-font-family:Cambria;
 mso-ascii-theme-font:minor-latin;
 mso-fareast-font-family:"ＭＳ 明朝";
 mso-fareast-theme-font:minor-fareast;
 mso-hansi-font-family:Cambria;
 mso-hansi-theme-font:minor-latin;
 mso-bidi-font-family:"Times New Roman";
 mso-bidi-theme-font:minor-bidi;
 mso-fareast-language:JA;}
.MsoPapDefault
 {mso-style-type:export-only;
 margin-bottom:10.0pt;}
@page WordSection1
 {size:595.0pt 842.0pt;
 margin:72.0pt 90.0pt 72.0pt 90.0pt;
 mso-header-margin:35.4pt;
 mso-footer-margin:35.4pt;
 mso-paper-source:0;}
div.WordSection1
 {page:WordSection1;}
-->









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



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



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



Galera, qualquer dúvida ou sugestão é só comentar!!&nbsp;