Java
  Home arrow Java arrow Page 4 - The JSP Files (part 2): Attack Of The ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

The JSP Files (part 2): Attack Of The Killer Fortune Cookies
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2001-02-22

    Table of Contents:
  • The JSP Files (part 2): Attack Of The Killer Fortune Cookies
  • Flavour Of The Month
  • Do It Or Else...
  • Cookie-Cutter Code
  • Lunch In Milan
  • Switching Things Around

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    The JSP Files (part 2): Attack Of The Killer Fortune Cookies - Cookie-Cutter Code


    (Page 4 of 6 )

    The "if-else" construct certainly offers a smidgen more flexibility than the basic "if" construct, but still limits you to only two possible courses of action. If your script needs to be capable of handling more than two possibilities, you should reach for the "if-else if-else" construct, which is a happy combination of the two constructs you've just been reading about.


    if (first condition is true) { do this! } else if (second condition is true) { do this! } else if (third condition is true) { do this! } ... and so on ... else { do this! }
    Take a look at it in action:

    <%! // declare temperature variable int temp = 20; %> <% // check temperature and display output // what happens if temp is less than 25 degrees if (temp > 25) { out.println("Man, it's hot out there!"); } // what happens if temp is between 25 and 10 degrees else if (temp < 25 && temp > 10) { out.println("Great weather, huh?!"); } // what happens if temp is less than ten degrees else if (temp < 10) { out.println("Man, it's freezing out there!"); } // this is redundant, included for illustrative purposes else { out.println("Huh? Somebody screwed up out there!"); } %>
    In this case, depending on the value of the "temp" variable, the appropriate code branch is executed, thereby making it possible to write scripts which allow for multiple possibilities.

    One important point to be noted here: control is transferred to successive "if" branches only if the preceding condition(s) turn out to be false. Or, in English, once a specific conditional expression is satisfied, all subsequent conditional expressions are ignored.

    Here's another example, this one using the day of the week to decide which fortune cookie to display. Alter the "day" variable to see a different cookie each time.

    <%! String day = "Monday"; String fortune; %> <% // check day and set fortune if (day.equals("Monday")) { fortune = "Adam met Eve and turned over a new leaf."; } else if (day.equals("Tuesday")) { fortune = "Always go to other people's funerals, otherwise they won't come to yours."; } else if (day.equals("Wednesday")) { fortune = "An unbreakable toy is useful for breaking other toys."; } else if (day.equals("Thursday")) { fortune = "Be alert - the world needs more lerts."; } else if (day.equals("Friday")) { fortune = "Crime doesn't pay, but the hours are good."; } else { fortune = "Sorry, closed on the weekend"; } // print output out.println(fortune); %>

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


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway