Java
  Home arrow Java arrow Page 7 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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: 3 stars3 stars3 stars3 stars3 stars / 4
    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 - Understanding the Conditional Operator


    (Page 7 of 11 )

    Java provides a concise way of writing conditional logic with the ?: operator. Because this operator takes three operands, it’s known as the ternary operator. Most operators take two operands and are thus known as binary operators, and there are a few unary operators that take just one operand, such as the complement operator.

    operand1 ? operand2 : operand3

    operand1 is evaluated, and if it works out to true, then operand2 is returned; otherwise operand3 is returned. Look at the following line:

    boolean freeShipping = (itemCost > 100) ? true : false;

    In the previous example, freeShipping will be set to true if itemCost is greater than 100; otherwise it’ll be set to false.

    Take a look at another example:

    int x = 20;
    int y = (x > 50)? x + 10 : x - 15;

    Here the integer variable x is initialized to 20. Because (x > 50)evaluates to false, the variable y is assigned the value of the third operand, which has the value x - 15 (which in turn evaluates to 5). Although there’s no JSTL equivalent for the ?: operator, it can be useful in Java code elsewhere.

    Using the switch Statement

    You’ve already seen that there’s a way to deal with a selection of options using if...else if statements. However, this method can be somewhat untidy. A cleaner alternative is to use the switch statement, the basic syntax of which is as follows:

    switch (expression)
    {
      case <expressionValue 1>
        StatementBlock1;
        break;
      case <expressionValue 2> :
        StatementBlock2;
        break;
     
    default :
        DefaultStatementBlock;
        break;
    }

    The first thing to note is that there’s a single expression to evaluate, which follows the initial switch keyword. The switch keyword is then followed by a sequence of case statements, each of which has an associated expression value. At the end of the list of case statements, the default keyword may appear.

    This is what happens when a switch statement is encountered:

    1. The expression is evaluated, and the result is compared to each case statement’s expression value in turn.

    2. When a case statement is found where the expression value matches the result of the expression, the statements belonging to that case are executed.

    3. If no matching case statement is found, the statements given for the default case are executed.

    There are a couple of important things to note about the switch block: First, the expression must return a value of type byte, char, short, or int. Second, if a break statement isn’t placed at the end of a particular case statement’s code block, all the code for all subsequent case statements will be executed, too. The break statement exits the switch construct, and execution continues from the following statement. This is required because expression values can’t contain logical operators, and this behavior allows one block of code to match several case statements. However, it’s a pitfall that can often trap the unwary programmer.

    NOTE  If you don’t include the break statement, program flow will fall through to the remaining case options and execute all statement blocks until break is encountered or the switch statement ends.

    The JSTL contains no parallel, but you can create a similar effect using choose...when, as you’ll see in the “Trying It Out: Working with the choose...when...when Construct” section. The following example shows a switch statement that displays a list of options according to the value of the fruitChosen variable:

    switch (fruitChosen)
    {
     
    case 1 :
        System.out.println("Apple");
        System.out.println("Price : $0.40, Color: Green"); 
        break;
     
    case 2 :
        System.out.println("Orange");
        System.out.println("Price : $0.45, Color: Orange"); 
        break;
     
    default :
        System.out.println("No Fruit Chosen");
        break;
    }

    In the previous code snippet, if fruitChosen is 1, the output produced would be as follows:

    Apple
    Price : $0.40, Color: Green

    If, however, fruitChosen were 5, it wouldn’t match any case expression values, so the default code would be executed, displaying the message No Fruit Chosen.

    There’s no direct equivalent to the switch statement in the JSTL, but you can achieve a similar effect using the <choose> tag. In the next example, you’ll create a drop-down list that lets the user select a particular fruit and see details about that fruit by clicking a button. You’ll use a JSTL <choose> tag to select the details for the chosen fruit.

    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-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek