Java Programming/Statements/if

The ... statement is used to conditionally execute one of two statements, depending on the result of a boolean condition.

Example:

(list == null) { {{Java://|this block of statements executes if the condition is true } {   {{Java://|this block of statements executes if the condition is false }

An statement has two forms:



and



Use the second form if you have different statements to execute if the is true or if it is false. Use the first if you only wish to execute if the condition is true and you do not wish to execute alternate statements if the condition is false.

The following example calls two methods, and, stores the results, then uses an statement to test if   is less than y and if it is, the body will swap the values. The end result is x always contains the larger result and y always contains the smaller result.

x = f; y = y; ( x < y ) { z = x;  x = y;   y = z; }