Use of JSP switch statements

Source: Internet
Author: User

If you want to choose to execute one of several blocks of code, you can use the switch statement:

Grammar:
Switch (n)
{
Case 1:
Execute code block 1
Break
Case 2:
Execute code block 2
Break
Default
If n is not 1 or 2, then this code is executed
}
How it works: The (n) behind the switch can be an expression, or it can (and usually) be a variable. The values in the expression are then compared to the numbers in the case, and if they match one, the subsequent code is executed. The function of a break is to prevent the code from automatically executing to the next line.

<HTML>
<HEAD>
<title>using the Switch statement</title>
</HEAD>

<BODY>
<%
int day = 3;

switch (day) {


Case 0:


out.println ("It's Sunday.");


break;


Case 1:


out.println ("It's Monday.");


break;


Case 2:


out.println ("It's Tuesday.");


break;


Case 3:


out.println ("It's Wednesday.");


break;


Case 4:


out.println ("It's Thursday.");


break;


Case 5:


out.println ("It's Friday.");


Break


Default:


out.println ("It must be Saturday.");


        }


%&gt;


&lt;/BODY&gt;


&lt;/HTML&gt;

Another situation

<HTML>
<HEAD>
<title>testing for multiple conditions</title>
</HEAD>

<BODY>
<%
int temperature = 64;

switch (temperature) {


Case 60:


Case 61:


Case 62:


out.println ("Sorry, Too cold!");


break;


Case 63:


Case 64:


Case 65:


out.println ("pretty cool.");


Break


Case 66:


Case 67:


Case 68:


Case 69:


out.println ("nice!");


break;


Case 70:


Case 71:


Case 72:


Case 73:


Case 74:


Case 75:


out.println ("fairly warm.");


break;


Default:


out.println ("Too hot!");


        }


%&gt;


&lt;/BODY&gt;


&lt;/HTML&gt;

The above is the three networking for you to introduce the JSP switch statement usage, I hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.