Home arrow Java arrow Page 7 - Making Decisions, Decisions

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
  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

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>
  case <expressionValue 2> :
default :

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("Price : $0.40, Color: Green"); 
case 2 :
    System.out.println("Price : $0.45, Color: Orange"); 
default :
    System.out.println("No Fruit Chosen");

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

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.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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