if - else

 

  You can use a single equals sign ( = ) inside an if statement with two booleans and it will compile successfully.  However at execution time it just makes the stated boolean assignment and  then always returns the right-hand value.  i.e.

 

boolean b = true; 

if (  b = false )   compiles and returns false.  See assignments

 

  You cannot put a non-boolean on the right hand side if the expression has a single equals sign. i.e.

 

double d1 = 1;

double d2 = 2;

if ( d1 = d2 ) will not compile.

 

  You cannot put more than one else statement after an if, although you can have any number of subsequent else if statements.  i.e.

The top group of if else ifs below will compile but the bottom group of elses will not:

 

int x = 0;

char c = 'F';

 

if ( x > 89 ) c = 'A';

else if ( x > 79 ) c = 'B';                                 // correct - compiler sees multiple else ifs

else if ( x > 69 ) c = 'C';

else if ( x >59 )  c = 'D'; 

 

if ( x > 89 ) c = 'A';

else { if ( x > 79 )  c = 'B'; }                           // incorrect - compiler sees multiple elses

else { if ( x > 69 )  c = 'C'; }

else { if ( x > 59 )  c = 'D'; }