Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
The JSP Files (part 3): Black Light And White Rabbits
More String object methods, and a tour of the various control structures available in JSP

| For-gone Conclusion |

Both the "while" and "do-while" loops continue to iterate for so long as the specified conditional expression remains true. But there often arises a need to execute a certain set of statements a specific number of times - for example, printing a series of thirteen sequential numbers, or repeating a particular set of <TD> cells five times. In such cases, clever programmers reach for the "for" loop...

The "for" loop typically looks like this:


for (initial value of counter; condition; update counter)
{
do this!
}


Looks like gibberish? Well, hang in there a minute...the "counter" here is a JSP variable that is initialized to a numeric value, and keeps track of the number of times the loop is executed. Before each execution of the loop, the "condition" is tested - if it evaluates to true, the loop will execute once more and the counter will be appropriately incremented; if it evaluates to false, the loop will be broken and the lines following it will be executed instead.

And here's a simple example that demonstrates how this loop can be used:

[code]
<html>
<head>
<basefont face="Arial">
</head>

<body>
<center>Turning The Tables, JSP-Style!</center>
<br>
<%!
// define the number
int number = 7;
int x;
%>
<%
// use a for loop to calculate tables for that number
for (x=1; x<=15; x++)
{
out.println(number + " X " + x + " = " + (number*x) + "<br>");
}
%>

</body>
</html>
[code]

And here's the output:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
Turning The Tables, JSP-Style!

7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70
7 X 11 = 77
7 X 12 = 84
7 X 13 = 91
7 X 14 = 98
7 X 15 = 105
').'
'

Let's dissect this a little bit:

Right up front, a variable is defined, containing the number to be used for the multiplication table; we've used 7 here - you might prefer to use another number.

Next, a "for" loop has been constructed, with "x" as the counter variable. If you take a look at the first line of the loop, you'll see that "x" has been initialized to 1, and is set to run no more than 15 times.

Finally, the println() function is used to take the specified number, multiply it by the current value of the counter, and display the result on the page.


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-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback