Making Decisions, Decisions - Introducing Branching Statements
(Page 10 of 11 )
Statements that direct the flow of execution to a particular point in a program are known as branching (or flow control) statements. You already saw one of these when you looked at the switch construct, where the break statement was needed at the end of case blocks to exit the switch at that point and continue at the line immediately after the switch.
You’ll look at this statement in more detail in the following sections, along with two more:
You’ll start with the break statement.
Using the break Statement
As well as exiting a switch statement, break can terminate any enclosing for, do...while, or while loop. Program control continues at the statement immediately following the enclosing loop.
An example of the break statement in a for loop is as follows:
String[] names = {"Matt", "Scott", "Ben", "Laura"};
for (int i=0 ; i < names.length ; i++) {
if(names[i] == "Ben") {
System.out.println("Found Ben");
break;
}
System.out.println("Not found Ben");
}
In the previous code snippet, if the value of the current array item is equal to "Ben", the break statement is executed. This will result in the termination of the enclosing for statement, so the for loop will never reach Laura in the array. The output of the previous code snippet will therefore be as follows:
Not found Ben
Not found Ben
Found Ben
You should note that a labeled break statement is also available. This is useful because it means you can jump to the end of any particular enclosing statement using break and a label. To illustrate this, consider what would happen if you nested the for statement from the previous example inside another for statement:
OuterLoop:
for(int counter = 1; counter <= 2; counter++) {
InnerLoop:
for(int num = 1; num <= 5; num++) {
System.out.println(num);
if (num == 3) {
break OuterLoop;
}
}
} // break directs execution flow to this point
Note that the for statements are labeled with OuterLoop and InnerLoop labels, respectively, and that break is also labeled:
break OuterLoop;
Now when program execution reaches this line, it will jump to the end of the outer for statement, not to the end of the inner for statement (as an unlabeled break would) because you specifically stated OuterLoop. The break statement always causes execution to drop to the end of the enclosing code block.
Note that this use of break is generally considered poor programming, and you should use better encapsulation in its place (here, for instance, you could put the InnerLoop in a separate method and replace the break statement with a return). However, sometimes it’s valid to “break the rules,” so you should be aware of this possible usage.
You’ll see that you can label other types of statement in a similar way.
Using the continue Statement Inside a for,do...while, or while loop, the continue statement will skip to the next iteration of the loop. You can use continue with a label to skip the current iteration of the loop that’s referred to by that label.
An example of the continue statement is as follows:
String[] orders = {"Done", "Outstanding", "Done", "Outstanding"};
int outstanding = 0;
int done = 0;
for (int i=0 ; i < orders.length ; i++) {
if(orders[i] == "Outstanding") {
outstanding++;
continue;
}
done++;
}
System.out.println("No. of outstanding orders: " + outstanding);
System.out.println("No. of completed orders: " + done);
When an order is outstanding, the expression in the if statement evaluates to true, the count of outstanding orders is increased, and the continue statement is called. This terminates the current iteration of the for loop at that point, without the count of the completed orders being increased. Therefore, this for statement will show the following:
No. of outstanding orders: 2
No. of completed orders: 2
Using the return Statement The return statement is used within a method, and it returns control back to the code that called the method. That is, on reaching a return statement, the method ends, and the calling code continues from where it left off. If the method should return a value, then the return statement must include a variable of the required data type. You’ll see many examples of this in later chapters.
In the following example, you’ll put your newly found knowledge of arrays and JSTL iteration to use in a JSP page.
Next: Trying It Out: Using Arrays >>
More Java Articles
More By Apress Publishing
|
This article was excerpted from Beginning JSP 2: From Novice to Professional by Peter den Haan et. al. (Apress, 2004; ISBN: 1590593391). Check it out at your favorite bookstore. Buy this book now.
|
|