Java Basics-Learning Notes (iv)-Process Control

Source: Internet
Author: User

1. Sequential structure if statement

if (expression) {EXECUTE statement block} (variable = boolean expression?) Statement 1: Statement 2 expression is true, statement 1 is executed, and statement 2 is executed instead)

if (expression) {EXECUTE statement block} else {}

if (expression 1) {EXECUTE statement block} else if (expression 2) {}else

If statements can also be nested within an if statement, else matches the nearest if

2. Select the structure switch statement

Look at the following code, is really drunk, I can write such a ~ but it can also learn some lessons, case followed by the only constant, the expression after switch can accept Byte, short, int, char type

1  Public Static voidMain (String agrs[])2     {3         intgrade=85;4         Switch(grade)5         {6             /*Case (Grade < 60)://Error Incompatible type: Boolean cannot be converted to int (must be constant after case)7 System.out.println ("You are unpassed!");*/8              Case1://You idiot! Grade is 85, definitely not equal to 1 ah, how can go to execute if statement, the final output is definitely it's wrong!9                 if(grade<60)TenSystem.out.println ("You Are unpassed!"); One              Case2: A                 if((grade>60) && (grade<80)) -System.out.println ("You Got b!"); -              Case3: the                 if((grade>80) && (grade<=100)) -System.out.println ("You Got a!"); -             default: -System.out.println ("It ' s wrong!"); +         } -}

Write one more right.

1  Public Static voidMain (String agrs[])2     {3         intX=3;4         Switch(x)5         {6              Case1:7System.out.println ("It ' s Monday");8              Case2:                9System.out.println ("It ' s Tuesday");Ten              Case3: OneSystem.out.println ("It ' s Wednesday"); A              Case4: -System.out.println ("It ' s Thursday"); -              Case5: theSystem.out.println ("It ' s Friday"); -             default: -System.out.println ("It ' s weekend!"); -         } +}

Output Result:

It ' s Wednesday
It ' s Thursday
It ' s Friday
It ' s weekend!

In order to avoid this problem, it is necessary to add a break out of the loop after each case if it encounters a case that satisfies the condition, not only executing the statement in the case, but also executing its subsequent contents.

1  Public Static voidMain (String agrs[])2     {3         intX=3;4         Switch(x)5         {6              Case1:7System.out.println ("It ' s Monday");8              Case2:                9System.out.println ("It ' s Tuesday");Ten              Case3: One              Case4: ASystem.out.println ("It ' s Thursday"); -                  Break; -              Case5: theSystem.out.println ("It ' s Friday"); -             default: -System.out.println ("It ' s weekend!"); -         } +}//output It ' s Thursday

As you can see from the code above, adding a break can jump out of the loop, and it also shows a problem, that is, when the statements to be executed in many cases are the same, we can use the same statement block 3 and 4 as shown above

3. Circular statements

while (expression) {statement}//when the expression is true, the statement is executed

Do

{

Statement

}while (expression); Executes the statement first, executes the expression in the while, and if true, resumes execution until the expression is false

for (initialize expression; loop condition expression; post-loop action expression) loop body

Break jumps out of the current loop body

Continue jumps out of the current loop statement, then executes the next loop

1  Public Static voidMain (String agrs[])2     {3         intI=1;4          for(; i<11;i++)5         {6             if(i%3==0)7                  Break;8 System.out.println (i);9         }Ten}//output
1  Public Static voidMain (String agrs[])2     {3         intI=1;4          for(; i<11;i++)5         {6             if(i%3==0)7                 Continue;8 System.out.println (i);9         }Ten}//Output 1,2,4,5,7,8,10

PS: When break is marked, as shown below, after the break St is executed, the program jumps out of the outermost while (that is, the marked while loop), with caution

St:while (True)

{

while (true)

{

Break St;

}

}

Java Basics-Learning Notes (iv)-Process Control

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.