The JSP Files (part 3): Black Light And White Rabbits - Doing More With Loops
(Page 2 of 7 )
There's one caveat with the "while" loop. If the conditional expression evaluates as false the first time, the code within the curly braces will never be executed. If this is not what you want, take a look at the "do-while" loop, which comes in handy in situations where you need to execute a set of statements *at least* once.
Here's what it looks like:
do
{ do this! } while (condition)
For example, the following lines of code would generate no output whatsoever, since the conditional expression in the "while" loop would always evaluate as false.
<%
int bingo = 366;
while (bingo == 699) { out.println
("Bingo!"); break; }%>
However, the construction of the "do-while" loop is such that the statements within the loop are executed first, and the condition to be tested is checked after. Using a "do-while" loop implies that the code within the curly braces will be executed at least once - regardless of whether or not the conditional expression evaluates as true.
<%
int bingo = 366;
do
{
out.println ("Bingo!");
break;
} while (bingo == 699);
%>
Try it yourself and see the difference.
Next: For-gone Conclusion >>
More Java Articles
More By Vikram Vaswani and Harish Kamath, (c) Melonfire