Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

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:

<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>

And here's the output:

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.

The Sound Of Breaking Loops

When dealing with loops, there are two important keywords you should be aware of: "break" and "continue".

The "break" keyword is used to exit a loop when it encounters an unexpected situation. A good example of this is the dreaded "division by zero" error - when dividing one number by another one (which keeps decreasing), it is advisable to check the divisor and use the "break" statement to exit the loop as soon as it becomes equal to zero.

As you've already seen, the "continue" keyword is used to skip a particular iteration of the loop and move to the next iteration immediately - it's demonstrated in the following example:

<%            
int x;            
for (x=1; x<=10; x++)            
{            
if (x == 7)            
{            
continue;            
}            
else            
{            
out.println(x + "&nbsp;");            
}            
}            
%>

In this case, JSP will print a string of numbers from 1 to 10 - however, when it hits 7, the "continue" statement will cause it to skip that particular iteration and go back to the top of the loop. So your string of numbers will not include 7 - try it and see for yourself.
<font size=1 color="#aaaaaa" face="Verdana,Arial,Helvetica">Copyright <a href="http://www.melonfire.com/">Melonfire</a>, 2000. All rights reserved.</font>

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links