Java Part 2: Syntax - Truth operations
(Page 4 of 8 )
Truth is represented in Java as a boolean type, which is either true or false. The truth operators are as follows:
Equals ==statement 1 == statement 2Statements must be equal for the entire statement to be true.
And &&statement 1 && statement 2Both statements must be true for the entire statement to be true.
Or ||statement 1 || statement 2One or both of the statements must be true for the entire statement to be true.
Not !!statement 1True if the statement evaluates to false, and vice-versa.
Greater than and less Than < >statement 1 < statement 2ORstatement 1 > statement 2Firstly, the result will be true if statement 1 is less than statement 2. Secondly, the result will be true if statement 1 is greater than statement 2.
If, Then, ElseThe if, then, else construct is a powerful Java conditional statement for decision making in your code. It works as follows:
if ( some truth statement )
{
// perform task
}
else if( some other truth statement)
{
// perform task
}
else
{
// all other cases
}Always enclose the conditional expressions in braces, as shown. This is not always essential but helps with readability. Also, indent the task performed by each condition by one tab space to enhance the readability of the code.
Next: Iterations and looping >>
More Java Articles
More By Chris Noack