Use Break Statement in jsp code
The break statement is used to terminate the execution of near most enclosing loop or conditional statement. Now the next statement outside the loop is executed.
In the example given below the elements of an array are added one by one. After each addition, the sum is checked whether it is more than 12. If the sum is less than 12 then the message "we are in loop..." is displayed otherwise the control comes out of the loop and does not print the same line again for this iteration. The rest iterations are also not executed because the loop is now terminated and the control is on the next statement after loop.
break_statement_jsp.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> <HEAD> <TITLE>Using the break Statement</TITLE> </HEAD> <BODY> <H1>use break statement in jsp code</H1> <% double array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int sum = 0; for(int i = 0; i < array.length; i++) { sum += array[i]; // use break statement. if (sum > 12) break; out.println("we are in loop...<BR>"); } out.println("The sum exceeded the max allowed value 12."); %> </BODY> </HTML> |
Save this code as a jsp file named "break_statement_jsp.jsp"
in your application directory (user, for this example) in Tomcat and run this jsp page with
http://localhost:8080/user/break_statement_jsp.jsp
url in address bar of the browser.