Home arrow Java arrow Page 10 - Making Decisions, Decisions
JAVA

Making Decisions, Decisions


Most JavaServer Pages will need to perform one of several actions according to some condition. This is managed in the code through control statements, which come in three flavors: conditional, iterative, and branching. This article will explain the syntax Java provides for these statements, how and when to use arrays, and more. It is taken from chapter five of Beginning JSP 2 From Novice to Professional, written by Peter den Haan et. al. (Apress, 2004; ISBN: 1590593391).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 11
May 05, 2005
TABLE OF CONTENTS:
  1. · Making Decisions, Decisions
  2. · Creating Multidimensional Arrays
  3. · Comparing Data Values
  4. · Making Decisions
  5. · Understanding Variable Scope in Statement Blocks
  6. · How It Works
  7. · Understanding the Conditional Operator
  8. · Trying It Out: Working with the choose...when...when Construct
  9. · Understanding Loops and Iteration
  10. · Introducing Branching Statements
  11. · Trying It Out: Using Arrays

print this article
SEARCH DEVARTICLES

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:

  • break

  • continue

  • return

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.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials