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).
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:
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:
The expression is evaluated, and the result is compared to each case statement’s expression value in turn.
When a case statement is found where the expression value matches the result of the expression, the statements belonging to that case are executed.
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.