The if
statement is used to control the flow of execution based on a Boolean expression. There are two basic forms that can be used and there are several variations. The if
statement consists of the if
keyword, followed by a logical expression enclosed in parentheses and then by a statement. In the following diagram a graphical depiction of a simple if
statement is presented:
The following illustrates this form of the if
statement where we compare rate
to 100
and if it is equal to 100
we display a message to that effect:
if (rate==100) System.out.println("rate is equal to 100");
However, this is not as readable as the following equivalent example where we spilt the if
statement across two lines:
if (rate == 100) System.out.println("rate is equal to 100");
As we will see later, it is better to always use a block statement with if
statements. The following is logically equivalent to the previous if
statement but is more readable and maintainable:
if (rate == 100) { System...