Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
The JSP Files (part 2): Attack Of The Killer Fortune Cookies
Conditional expressions, logical and comparsion operators, and a basket full of fortune cookies. What more could you ask for?

| Adding It All Up |

You'll remember how, in the first part of this tutorial, we used the + operator to add numbers and strings together. And just as you have the + operator for addition, JSP comes with a bunch of other arithmetic operators designed to simplify the task of performing mathematical operations.

The following example demonstrates the important arithmetic operators available in JSP:


<html>
<head>
</head>
<body>

<%!
// declare variables
int alpha = 25;
int beta = 5;
int sum, difference, product, quotient, remainder;
%>

<%
// perform operations
out.println("The sum of " + alpha + " and " + beta + " is " + (alpha + beta) + "<br>");
out.println("The difference of " + alpha + " and " + beta + " is " + (alpha - beta) + "<br>");
out.println("The product of " + alpha + " and " + beta + " is " + (alpha * beta) + "<br>");
out.println("The quotient after division of " + alpha + " and " + beta + " is " + (alpha / beta) + "<br>");
out.println("The remainder after division of " + alpha + " and " + beta + " is " + (alpha % beta) + "<br>");
%>


</body>
</html>


And here's the output:


The sum of 25 and 5 is 30
The difference of 25 and 5 is 20
The product of 25 and 5 is 125
The quotient after division of 25 and 5 is 5
The remainder after division of 25 and 5 is 0


As with all other programming languages, division and multiplication take precedence over addition and subtraction, although parentheses can be used to give a particular operation greater precedence. For example,


<%
out.println(10 + 2 * 4);
%>


returns 18, while


<%
out.println((10 + 2) * 4);
%>


returns 48.

In addition to these operators, JSP comes with the very useful auto-increment [++] and auto-decrement [--] operators, which you'll see a lot of in the next article. The auto-increment operator increments the value of the variable to which it is applied by 1, while the auto-decrement operator does the opposite. Here's an example:


<%!
int x = 99;
%>

<%
// x = 99
out.println("Before increment, x = " + x + "<br>");
x++;
// x = 100
out.println("After increment, x = " + x);
%>


JSP also comes with a bunch of comparison operators, whose sole raison d'etre is to evaluate expressions and determine if they are true or false. The following table should make this clearer.


Assume x=4 and y=10

Operator   What It Means      Expression      Result
--------------------------------------------------------------------------------------
==   is equal to          x == y      False

!=   is not equal to      x != y      True

>   is greater than      x > y      False

<   is less than      x < y      True

>=   is greater than or equal to   x >= y      False

<=   is less than or equal to   x <= y      True



How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback