The Complete Reference
A programming language uses control statements to cause the flow of execution to advance and branch based on changes to the state of a program. Java's program control statements can be put into the following categories: selection, iteration, and jump. Selection statements allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable Iteration statements enable program execution to repeat one or more statements (that is, iteration statements form loops). Jump statements allow your program to execute in a nonlinear fashion. All of Java's control statements are examined here.
Java's Selection Statements
Java supports two selection statements: if and switch. These statements allow you to control the How of your program's execution based upon conditions known only
during run time. If your background in programming does not include C/C++, you will be pleasantly surprised by the power and flexibility contained in these two
The if statement was introduced in Chapter 2. It is examined in detail here. The if statement is Java's conditional branch statement. It can be used to route program
execution through two different paths. Here is the general form of the if statement :
Here, each statement many be a single statement a compound statement enclosed in . curly braces (that is, block). The condition is any expression that returns a Boolean
value. The else clause is optional.The if works like this: If the condition is true, then siaiementl is executed. Otherwise, statementing (if it exists) is executed. In no case will both statements be executed. For exmple, consider the following:
int a, b.
/ / ...
if(a < b) a
else b = 0.
Here, if a is less than b, then a is set to zero. Otherwise, b is set to zero. In no case are they both set to Zero Most often, the expression used to control the if will involve the relational operators. However, this is not technically necessary. It is possible to control the if using a single bo~lean variable.as shown in ·this code fragment:
boolean data Available;
Remember, only one statement can appear directly.after the if or the else. If you
if (bytesAvailable >' 0)-
Here, both statements within the if block will execute if bytcs Availablc is greater than zero. Some programmers find it convenient to include the curly braces when using the if, even when there is only one statement in each clause. 111ismakes it easy to add another statement at a later date, and you don't have to worry about forgetting the braces. In fact, forgetting to define. a block when one is needed is a common cause of errors. For example, consider the following code fragment.
if (bytesAvailable > 0)
bytesAvailable -= ni
el se .
bytcsAvailaole = n;
It seems clear that the statement available = It; was intended to be executed inside the else clause, because of the indentation level. However.us your call white space is insignificant to Java, and there is.no way for photoelectrically know what was intended.This. code will compile without complair H but itill behave mcoriettly when run. The preceding examples fixed in the-code that follows.
I I ...
if (byteaAvailable > 0)
bytesAvailable = n
A nested if is an if statement that is the target of another if or else, Nested very common in programming. When you nest ifs, the main thing to remember is that else statement always refers to the nearest if statement, that 'is within the same the else and that is not already associated with an else: Here is an example:
As the comments indicate, the final else is not-associated with f(j<20),because i~is not in the same block (even though it is the nearest if without an else). Rather, the final else is associated with if(i==10). The inner else refers to if(k>100), because it is the closest if within the same block.