Home arrow Java arrow Page 3 - Making Decisions, Decisions
JAVA

Making Decisions, Decisions


Most JavaServer Pages will need to perform one of several actions according to some condition. This is managed in the code through control statements, which come in three flavors: conditional, iterative, and branching. This article will explain the syntax Java provides for these statements, how and when to use arrays, and more. It is taken from chapter five of Beginning JSP 2 From Novice to Professional, written by Peter den Haan et. al. (Apress, 2004; ISBN: 1590593391).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 11
May 05, 2005
TABLE OF CONTENTS:
  1. · Making Decisions, Decisions
  2. · Creating Multidimensional Arrays
  3. · Comparing Data Values
  4. · Making Decisions
  5. · Understanding Variable Scope in Statement Blocks
  6. · How It Works
  7. · Understanding the Conditional Operator
  8. · Trying It Out: Working with the choose...when...when Construct
  9. · Understanding Loops and Iteration
  10. · Introducing Branching Statements
  11. · Trying It Out: Using Arrays

print this article
SEARCH DEVARTICLES

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


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials