Home arrow Java & J2EE arrow Page 6 - The JSP Files (part 2): Attack Of The Killer Fortune Cookies

Switching Things Around - 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.

  1. The JSP Files (part 2): Attack Of The Killer Fortune Cookies
  2. Flavour Of The Month
  3. Do It Or Else...
  4. Cookie-Cutter Code
  5. Lunch In Milan
  6. Switching Things Around
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 4
February 22, 2001

print this article


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.

>>> More Java & J2EE Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: