Process Control in Java (II.)

Source: Internet
Author: User
Tags case statement

<title>On the process Control of Java programs (II.)</title> Process Control for Java programs (ii) 3.SWITCH selection statement

The switch statement is used to compare the value of an expression to many other values and to select which statements to execute according to the comparison results.

switch(表达式)
{
case 取值1:
语句块1
break;
……
case 取值n:
语句块n
break;

default:
语句块n+1
break;
}

Example: Print out the English words for the day of the week,

int  X=2 ; 
switch (x)
{
case 1 :
System.out.println (" Monday ");
break ;
case 2 :
System.out.println ( "Tuesday" );
break ;
case 3 :
System.out.println ( "Wednesday" );
break ;
default :
System.out.println ( "Sorry, I don ' t Know ");
}

The result of the program printing is:

Tuesday

The switch statement determines that the condition can accept the Int,byte,char,short type and cannot accept other types.

Note: The else if is no longer executed after the next else statement, while the case statement is just equivalent to defining a label position, when switch encounters the first case match, the program jumps to the label position and starts executing all of the program code in sequence. Regardless of whether the following case conditions match, until the break statement is encountered.
For example, if you delete the break statement after System.out.print ("Tuesday"), the result of the program printing will be:

Tuesday
Wednesday

If you use the same statement to handle multiple case conditions, the program can be written as follows:

case1:
case2:
case3:
System.out.println("you are very bed");
System.out.println("you must make great efforts");
break;
case4:
case5:
System.out.println("you are good");
4.while Loop statement

The while statement is a loop and a conditional judgment statement.

while(条件表达式语句)
{
执行语句
}

When the return value of the conditional expression is true, execution of the statement segment in {} is executed, and when the statement in {} is executed, the return value of the conditional expression is detected until the return value is False when the loop terminates.

int x=1;
while(x<3)
{
System.out.println("x="+x);
x++;
}

The program prints out:

x=1
x=2

Note: do not add ";" after the parentheses of the while expression, such as:

int x=1;
while(x<3);
{
System.out.println("x="+x);
x++;
}

The program will assume that to execute an empty statement, and enter an infinite loop, never execute the following code, the Java compiler will not error.

Process Control in Java (II.)

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.