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 - Comparing Data Values (Page 3 of 11 )
The data values being compared are known as operands, and these operands can be variables, constants, or expressions. The basic syntax for comparing two data values is as follows:
operand1 <relational_operator> operand2
where relational_operator is one of those shown in Table 5-1. The table also shows the JSTL equivalents. In a JSTL tag, you’re free to use either form of operator.
Operator
JSTL
Operator Name
Explanation
<
lt
Less Than
Evaluates to true if operand1 is less than operand2; otherwise false
<=
le
Less Than or Equal To
Evaluates to true if operand1 is less than or equal to operand2; otherwise false
>
gt
Greater Than
Evaluates to true if operand1 is greater than operand2; otherwise false
>=
ge
Greater Than or Equal To
Evaluates to true if operand1 is greater than or equal to operand2; otherwise false
==
eq
Equal To
Evaluates to true if operand1 is equal to operand2; otherwise false
!=
ne
Not Equal To
Evaluates to true if operand1 isn’t equal to operand2; otherwise false
Table 5-1. Relational Operators
Using Logical Operators
These relational operators form the basis of all logical tests, and they can be combined into more complex tests using the logical operators listed in Table 5-2 to construct more complex expressions that consist of two or more conditions. Remember that operands can be values, variables, or expressions.
Operator
Syntax
Explanation
||
operand1 || operand2
This is the logical OR operator, which returns true if operand1 oroperand2 is true. It only returns false if both the operands are false. It evaluates operand2 only if operand1 is false.
&&
operand1 && operand2
This is the logical AND operator, which returns true if operand1 and operand2 are true.In other words, if any of the operands are false, it returns false. Note that operand2 is evaluated only if operand1 is true.
!
! operand1
Returns true if operand1 is false. Else if operand1 is true, it returns false.
Table 5-2.Logical Operators
As an example, say an online store offers free shipping to customers who purchase four or more items of total value greater than $100. If you have a couple of variables that indicate number of items bought and total cost—called itemCount and totalCost, respectively—then the following expression will determine whether shipping is free:
itemCount >= 4 && totalCost >= 100
If you’re going to be using this expression regularly, you can store the result in a boolean variable, say freeShipping:
So for instance, if a customer buys six items and pays $130, then freeShipping will be true. Now say the store changes its policy so customers need to meet only one of the criteria (buying four or more items or spending $100 or more) to qualify for free shipping. You could implement this by simply changing the logical operator in the expression:
Another example demonstrates the use of the complement operator,!, applied to an integer,n:
!(n >= 0)
In this case, when n is negative, the part of the expression within parentheses results in false. The ! operator takes the complement of that to give the result of the expression as a whole—in other words, true when n is negative. Conversely, if n is zero or greater, then the part in brackets will be true, so the whole expression will evaluate to false. This expression is in effect just a more convoluted way of writing this: