Java
  Home arrow Java arrow Page 10 - Making Decisions, Decisions
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Making Decisions, Decisions
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2005-05-05

    Table of Contents:
  • Making Decisions, Decisions
  • Creating Multidimensional Arrays
  • Comparing Data Values
  • Making Decisions
  • Understanding Variable Scope in Statement Blocks
  • How It Works
  • Understanding the Conditional Operator
  • Trying It Out: Working with the choose...when...when Construct
  • Understanding Loops and Iteration
  • Introducing Branching Statements
  • Trying It Out: Using Arrays

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Java Articles
    More By Apress Publishing


     

    Buy this book now. 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.

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway