Java Tutorial/JSP/While
Finding a Factorial
<HTML>
<HEAD>
<TITLE>Finding a Factorial</TITLE>
</HEAD>
<BODY>
<H1>Finding a Factorial</H1>
<%
int value = 6, factorial = 1, temporaryValue = value;
while (temporaryValue > 0) {
factorial *= temporaryValue;
temporaryValue--;
}
out.println("The factorial of " + value + " is " + factorial + ".");
%>
</BODY>
</HTML>
Finding Reciprocals
<HTML>
<HEAD>
<TITLE>Finding Reciprocals</TITLE>
</HEAD>
<BODY>
<H1>Finding Reciprocals</H1>
<%
double values[] = {4, 3, 2, 1, 0, 1, 2, 3, 4};
int i = 0;
while (values[i] != 0){
out.println("The reciprocal = " + 1 / values[i++] + ".<BR>");
}
%>
</BODY>
</HTML>
Using the while Loop
<HTML>
<HEAD>
<TITLE>Using the while Loop</TITLE>
</HEAD>
<BODY>
<H1>Using the while Loop</H1>
<%
int value = 5;
while (value > 0) {
out.println("The value is now " + value-- + ".<BR>");
}
%>
</BODY>
</HTML>