HomeJava & J2EE Page 5 - The JSP Files (part 2): Attack Of The Killer Fortune Cookies
Lunch In Milan - Java
The second part of our introductory JSP tutorial discussesarithmetic, logical and comparison operators, together with simple examplesand illustrations. You'll also learn the basics of JSP's numerousconditional expressions, including the "if", "if-else" and "switch"statements, and find out a little more about the String object.
If you take a close look at the last-but-one example above, you'll notice that the conditional expression
(temp < 25 && temp > 10)
is slightly different from the ones you've been used to
thus far. This is because JSP also allows you to combine multiple conditions into a single expression, with the help of an animal called a "logical operator".
The following table should make this clearer.
Assume delta = 12, gamma = 12 and omega = 9
Operator
What It Means
Example
Evaluates To
&&
AND
delta == gamma && delta > omega
delta equals gamma AND delta is greater than omega
True &&
AND
delta == gamma && delta < omega
delta equals gamma AND delta is less than omega
False ||
OR
delta == gamma || delta < omega
delta equals gamma OR delta is less than omega
True ||
OR
delta > gamma || delta < omega
delta is greater than gamma OR delta is less than omega
False !
NOT
!delta
delta doesn't exist
So, instead of something as ugly as this
<%
if (day == "Thursday")
{
if (time == "12")
{
if (place == "Italy")
{
lunch = "pasta";
}
}
}
%>
you could have something as elegant as this.
<%
if (day == "Thursday" && time == "12" && place == "Italy")
{
lunch = "pasta";
}
%>