Java
  Home arrow Java arrow Page 3 - Making Decisions, Decisions
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
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

Making Decisions, Decisions
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2005-05-05

    Table of Contents:
  • Making Decisions, Decisions
  • Creating Multidimensional Arrays
  • Comparing Data Values
  • Making Decisions
  • Understanding Variable Scope in Statement Blocks
  • How It Works
  • Understanding the Conditional Operator
  • Trying It Out: Working with the choose...when...when Construct
  • Understanding Loops and Iteration
  • Introducing Branching Statements
  • Trying It Out: Using Arrays

  • 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


    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:

    freeShipping = (itemCount >= 4 && totalCost >= 100);

    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:

    freeShipping = ((itemCount >= 10) || (totalCost >= 100));

    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:

    n < 0

    More Java Articles
    More By Apress Publishing


     

    Buy this book now. This article was excerpted from Beginning JSP 2: From Novice to Professional by Peter den Haan et. al. (Apress, 2004; ISBN: 1590593391). Check it out at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






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