The Truth About Logical Expressions
To master selection and repetition structures you need to be able to build logical expressions.
 
You can master this because logic expressions are about the same in all programming languages and in spread sheet software like MS Excel for formula writing. Learn it once and use it often. Actually you can write a program just to experiment with logical expressions.
 
Logical expressions result in true or false.
 
Java contains a primitive logical data type boolean.
 
Java also contains constants words true and false.
 
A logic operator is a component of a logical expression. Logical expressions may use any data types to result in a true or false condition.
 
The name assigned to the operators in logic expressions vary in documentation. The operators associated with logic expressions include:
 
Logical expression operators
============================
>       Greater than
<       Less than
>=      Greater than or equals to
<=      Less than or equals to
!=      Not equals to
==      Equals and in some languages the single = is used.
||      Or which connects two logic expressions as exclusive
!       Not which says the opposite is true.
&&      And which connects two logic expression as inclusive
 
The red operators are called relational, comparative, logic, and even Boolean depending on what you are reading. I like comparison operators for my thought process.
 
The purple operators are called Boolean, connecting, logic, and when logic fails you words I cannot mention.:-) I like calling them logic connecting operators.
 
A basic logic expression computes either true or false the only two values in logic.
 
A logic expression can contain most anything as long as they evaluate to true or false.
 
As we know the computer only handles numeric data in binary ( 1 and 0 format). So generally 1 for true and 0 for false works for many languages. In Java use the keyword true and false for clear reading code.
 
Further you may find code were any non-zero number is used to represent true or more specifically the programmer uses minus one (-1) or one (1), but although rare do not be surprised with a number like 1284 or -8731 for true.
 
The following examples show logical expressions. No variables were used to keep the expressions easy to read. More likely you will have variables and even functions used in logical expressions.
 
Examples using the relational operators:
System.out.println("3 > 4      " + ( 3   >  4   ) );
System.out.println("3 < 4      " + ( 3   <  4   ) );
 
System.out.println("3.5 > 4.0  " + ( 3.5 >  4.0 ) );
System.out.println("3.5 >= 4.0 " + ( 3.5 >= 4.0 ) );
System.out.println("3.5 > 3.0  " + ( 3.5 >  3.0 ) );
 
System.out.println("a > b      " + ( 'a' >  'b' ) );
System.out.println("b > a      " + ( 'b' >  'a' ) );
 
Output:
 
3 > 4      false
3 < 4      true

3.5 > 4.0  false
3.5 >= 4.0 false
3.5 > 3.0  true

a > b      false
b > a      true
Notice that the same data types need to be on both sides or the relational operators.
 
Examples using the relational operators and logical operators:
System.out.println("3 > 4 and 4 < 5             " 
                   + (   3 < 4 &&  4 > 5 ) );
      
System.out.println("3 > 4 or  4 > 5             " 
                   + (   3 < 4 ||  4 > 5 ) );
 
System.out.println("3 < 4 or  4 > 5  and 1 > 2  " 
                    + (   3 < 4 ||  4 > 5   &&  1 > 2 ) );
 
System.out.println("(3 < 4 or  4 > 5) and 1 > 2 " 
                    + ( ( 3 < 4 ||  4 > 5 ) &&  1 > 2 ) );
 
Output:
 
3 > 4 and 4 < 5             false

3 > 4 or  4 > 5             true

3 < 4 or  4 > 5  and 1 > 2  true

(3 < 4 or  4 > 5) and 1 > 2 false
Notice the operator precedence of && (and) and || (or). The && is higher precedence. You may need to use parenthesis to change the order of precedence and shown between the third and fourth print statement in the last example.
 
Debugging Logic Expressions
Logic expressions are the cause of logic bugs in programs. Generally the problem can be the errors or the data being compared. Add lines of code above like the above to display your logic expressions exactly as they are used in other statement. Then you can see the truth! :-)
 
Putting logical expressions to use:
Printing out the values of false and true are not the main use. More likely you will use a logical expression in loop and decision flow structures and the conditional expression. More on those in other posts.