Instruções if, else e else if.
Autor: Luciano da Cunha | Publicado: 2012-02-07T16:00:00-08:00

Para que uma instrução if funcione é preciso ter um valor booleano dentro dos parênteses. Esse valor poderá ser uma literal, variável ou retorno de uma expressão.

No código abaixo, linha 25, apesar dos valores retornados serem false, a resposta para a pergunta feita é true.

Note também que no último if, linha 25, não foi utilizado chaves. Isso é possível, porém, somente a primeira linha depois do if é executada em função do retorno verdadeiro.



class TestesIf{

&nbsp;&nbsp;&nbsp; private boolean key;

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

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; TestesIf ti = new TestesIf();

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("Valor de key: " + ti.getKey());

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (true){

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ti.setKey(true);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("Primeiro if...Valor de key " + ti.getKey());

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

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (ti.getKey()){

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ti.setKey(false);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("Segundo if...Valor de key " + ti.getKey());

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

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (ti.getKey() == false)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("Terceiro if...Valor de key " + ti.getKey());

&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; void setKey(boolean b){

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; key = b;

&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; boolean getKey(){

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return key;

&nbsp;&nbsp;&nbsp; }
}

É possível ainda aninhar if com else if e else, desde que o else sempre venha depois das instruções else if.

Porém observe no código abaixo, na primeira estrutura, a saída que será gerada, em razão do fluxo percorrer os três primeiros if's.

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

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int x = 4, y = 0;

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Primeira estrutura.
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (x &gt; 1){
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (x &gt; 2){
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (x &gt; 3){
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (x &gt; 4){
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (x &gt; 5){
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("x &gt; 5");
&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;&nbsp;&nbsp; System.out.println("x &gt; 4");
&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.println("x &gt; 3");
&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.println("x &gt; 2");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("x &gt; 1");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } else if (x == 0){

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } else {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("x &gt; 6");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Segunda estrutura
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (y &gt; 8){
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("y &gt; 10");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } else if (y &gt; 9){
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("y &gt; 9");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } else if (y &gt; 8){
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("y &gt; 8");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } else if (y &gt; 7){
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("...atribuindo valor 10 a y");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; y = 10;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (y &gt; 10){
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("y &gt; 10");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } else {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; y++;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("...incrementando y");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } else {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("y == ?");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("...fim dos if's");
&nbsp;&nbsp;&nbsp; }
}
No código acima, na segunda estrutura, o else será executado somente se nenhuma das anteriores forem satisfeitas. Pois quando o fluxo é capturado pelo terceiro else if, ao sair daquela estrutura aninhada, o fluxo passa diretamente para o fim dos if's.