Control Flow Functions

   

Control flow is the heart of any program. It is the capability to adjust (control) the way that a program progresses (flows). By directing the paths of execution that your code takes, you build programs that behave dynamically based on their inputs. Without control flow, programs would not be able to do anything more than several sequential operations.

if Statements

The simplest form of control flow is the if statement. An if statement checks its conditional expression (typically derived through any of the means described in the first half of this chapter) and, if the value is true, directs the interpreter to execute the next block of code. The general syntax for the if statement is as follows :

 if (expression)   statement; 

If the expression evaluates to false, the computer skips the following statement and continues on. An example of an if statement is shown in the following code fragment:

 if (userNameIsRebeccah)   System.out.println("Hi Rebeccah"); System.out.println("Welcome to the system"); 

If the value of userNameIsRebeccah is true, the computer prints out the following when this fragment runs:

 Hi Rebeccah Welcome to the system 

However, if the value is false, the program skips over the line after the if and the result is as follows:

 Welcome to the system 

In many situations, you will want to execute more than one line of code based on a condition. To do this, you can place a code block, which is a series of statements inside a pair of curly braces, after the if statement. The following code fragment shows just such an example:

 int transferAmount = 100; if (checkingAccount.transferApproved(transferAmount)) {   checkingAccount.withdraw(transferAmount);   savingsAccount.deposit(transferAmount);   System.out.println("Transfer completed"); } System.out.println("After transfer request"); 

Although the use of braces is only required when you define a block that contains more than one statement, it is common practice to also use braces for single-statement blocks to improve the readability of your code. Notice that in the first example given here, the only indication that the if statement applies only to the first statement following it is an arbitrary use of whitespace. This code executes the same even if this indentation is removed because all whitespace is ignored by the compiler. You should always use indentation even if you choose not to use braces, but as you'll see in the next section, indentation alone can be deceiving if it isn't applied carefully .

Note

Although the preceding examples used boolean variables or expressions as conditions, you can also use boolean literals. The result of such a control statement is obviously known in advance, so this provides one possible way to simulate conditional compilation. Any code that is tied to a false condition at compile time will not be included in the compiled class file.


if - else Statements

Only slightly more advanced than a simple if, the if - else statement evaluates a condition and then, if the condition is true, executes the statement (or block) after the if. Otherwise the statement (or block) after the else is executed. Only one or the other set of code is run. The general syntax for an if - else is as follows:

 if (expression)   statement1; else   statement2; 

Our earlier example can be expanded slightly to use an if - else statement as follows:

 int transferAmount = 100; if (checkingAccount.transferApproved(transferAmount)) {   checkingAccount.withdraw(transferAmount);   savingsAccount.deposit(transferAmount);   System.out.println("Transfer completed"); } else {   System.out.println("Transfer refused"); } System.out.println("After transfer request"); 

The if - else statement complicates the issue of if statement readability further if indentation, or preferably, braces, are not properly used. This can be seen when looking at how else blocks are evaluated when there are nested if statements. Consider the following code:

 if (firstVal==0)    if (secondVal==1)       firstVal++; else    firstVal--; 

When is the else executed? The indentation implies that it is executed whenever firstVal is not equal to 0. However, the rules governing if - else statements are independent of any use of whitespace. An if - else expression forms a single statement, so an else always belongs to the most recent if.

This means that, in this example, the else is executed whenever firstVal is 0 and secondVal is not 1. The following two code fragments show better ways to write this example:

 // better to at least get the indentation right if (firstVal==0)    if (secondVal==1)       firstVal++;    else       firstVal--; // still better to use braces if (firstVal==0) {    if (secondVal==1) {       firstVal++;    }    else {       firstVal--;    } } 

The use of braces also allows you to control which if statement an else is associated with as shown in the following:

 if (firstVal==0) {    if (secondVal==1) {       firstVal++;    } } else {    firstVal--; } 

Because a block counts as a single statement, the else is now associated with the first if.

Another variation on the if - else statement is known as the compound if:

 if (firstVal==0)    if (secondVal==1)       firstVal++;    else if (thirdVal==2)       firstVal--; 

In this example, firstVal is only decremented when firstVal is 0, secondVal is not 1, and thirdVal is 2. Follow this last example through to verify to yourself that this is the case.

Note

Some languages use a separate keyword, ELSIF for example, to represent a compound if statement. Java does not add a keyword but instead uses both the else and if keywords as part of the same statement.


   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net