Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

Lunch In Milan

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

OperatorWhat It MeansExampleTranslationEvaluates To
&&ANDdelta == gamma && delta > omegadelta equals gamma AND delta is greater than omegaTrue
&&ANDdelta == gamma && delta < omegadelta equals gamma AND delta is less than omegaFalse
||ORdelta == gamma || delta < omegadelta equals gamma OR delta is less than omegaTrue
||ORdelta > gamma || delta < omegadelta is greater than gamma OR delta is less than omegaFalse
!NOT!deltadelta isn't trueFalse

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";          
}          
         
%>

Switching Things Around

Finally, JSP rounds out its conditional expressions with the "switch" statement, which offers an alternative method of transferring control from one program block to another. Here's what it looks like:

switch (decision-variable)          
{          
case first_condition_is true:          
do this!          
         
case second_condition_is true:          
do this!          
         
case third_condition_is true:          
do this!          
         
... and so on...          
         
default:          
do this by default!          
         
}

The "switch" statement can best be demonstrated by rewriting the previous example using "switch" instead of "if-else if-else".

<%!          
int dayOfWeek = 3;          
String fortune;          
%>          
<%          
// the decision variable here is the day chosen by the          
user switch (dayOfWeek)          
{          
         
// first case          
case 1:          
fortune = "Adam met Eve and turned over a new leaf.";          
break;          
         
// second case          
case 2:          
fortune = "Always go to other people's funerals, otherwise          
they won't come to yours.";          
break;          
         
case 3:          
fortune = "An unbreakable toy is useful for breaking other toys.";          
break;          
         
case 4:          
fortune = "Be alert - the world needs more lerts.";          
break;          
         
case 5:          
fortune = "Crime doesn't pay, but the hours are good.";          
break;          
         
// if none of them match...          
default:          
fortune = "Sorry, closed on the weekend";          
break;          
         
}          
// print output          
out.println(fortune);          
%>

The first thing you'll notice is that the "day" variable from the previous example has been converted to a numeric "dayOfWeek" variable -- this is because the "switch" construct only works when the decision variable is an integer.

There are also a couple of important keywords here: the "break" keyword is used to break out of the "switch" statement block and move immediately to the lines following it, while the "default" keyword is used to execute a default set of statements when the variable passed to "switch" does not satisfy any of the conditions listed within the block.

And that's about it. You now know enough about JSP's conditional statements to begin writing simple programs - so go practice! And come back for the next issue, when we'll be talking about loops, demonstrating other String object methods, and even taking a quick look at the new Response object.

Note: All examples in this article have been tested on Linux/i586 with Tomcat 3.2 and JServ 1.1. Examples are illustrative only, and are not meant for a production environment. YMMV!
Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links