Java provides two selection statements: if
and switch
. These statements let you control the course of your program’s execution based upon conditions known particularly during run time. You will be pleasingly amazed by the power and versatility contained in these two statements.
IF Statement in Java
The if
statement is examined in detail in this article. The if
statement is Java’s conditional branch statement. It can be used to route program execution using two different paths. Here is the usual form of the if statement:
if (condition) statement1; else statement2;
Here, every statement may be a single statement or a compound statement contained in curly braces. The condition is any expression that gives back a boolean value. The else
clause is optional.
The if
acts like this: If the condition is true, then statement1 is executed. Otherwise, statement2 (if it exists) is performed. In no circumstance will both statements be executed. For example, consider the following:
int c, d; // ... if(c < d) c = 0; else d = 0;
Here, if c is less than d, then c is set to zero. Otherwise, d is set to zero. In no circumstance are they both set to zero.
Generally, the expression used to control the if
will include the relational operators. Nevertheless, this is not technically mandatory. It is possible to control the if
employing a single boolean variable, as presented in this code fragment:
iboolean dataAvailable; // ... if (dataAvailable) ProcessData(); else waitForMoreData();
Always Remember, only one statement can appear directly after the if
or else
. If you wish to include more statements, you’ll need to create a block, as in this fragment:
int bytesAvailable; // ... if (bytesAvailable > 0) { ProcessData(); bytesAvailable -= n; } else waitForMoreData();
Here, both statements in the if
block will execute if bytesAvailable
is more significant than zero. Some programmers find it helpful to insert the curly braces while using the if
, even when there is one statement in each clause.
int bytesAvailable; // ... if (bytesAvailable > 0) { ProcessData(); bytesAvailable -= n; } else waitForMoreData(); bytesAvailable = n;
bytesAvailable
= n; was meant to be executed inside the else clause, because of the indentation level. This code will run without any complaint, but it will function incorrectly when run. The above example is fixed in the code that follows:int bytesAvailable; // ... if (bytesAvailable > 0) { ProcessData(); bytesAvailable -= n; } else { waitForMoreData(); bytesAvailable = n }
Java Nested IF
A nested if
is an if
statement that is the target of another if
or else
. When you nest ifs, the foremost thing to remember is that an else
statement always refers to the nearest if
statement that is inside the same block as the else
and that is not already associated with an else
. Here is an example:
if(i == 10) { if(j < 20) a = b; if(k > 100) c = d; // this if is else a = c; // associated with this else } else a = d; // this else refers to if(i == 10)
As the comments show, the final else is not associated with if(j<20) because it is not in the same block. Instead, the final else
is related with if(i==10). The inner else
points to if(k>100) because it is the nearest if
within the same block.
The if-else-if Ladder
A usual programming construct that is based upon a sequence of nested
ifs
is the if-else-if ladder. It looks like this:
if(condition) statement; else if(condition) statement; else if(condition) statement; . . . else statement;
The if
statements run from the top down. As soon as one of the conditions checking the if
is true, the statement linked with that if
is executed, and the rest of the ladder is avoided. If none of the conditions is true, then the final else
statement will be performed.
else
statement is executed. If there is no final else
and, all other conditions are false, then no operation will take place.Here is a program that utilizes an if-else-if
ladder to determine which season a particular month is in.
// Demonstrate if-else-if statements. class IfElse { public static void main(String args[]) { int month = 4; // April String season; if(month == 12 || month == 1 || month == 2) season = "Winter"; else if(month == 3 || month == 4 || month == 5) season = "Spring"; else if(month == 6 || month == 7 || month == 8) season = "Summer"; else if(month == 9 || month == 10 || month == 11) season = "Autumn"; else season = "Bogus Month"; System.out.println("April is in the " + season + "."); } }
April is in the Spring.
You might want to try this program before moving on. As you will see, no matter what value you give month, one and only one assignment statement within the ladder
will be executed.