17. Java Process Control

Source: Internet
Author: User

First, IF

1. The first form: if

if (logical expression) {    statement 1;    Statement 2;    ...}

You can omit {} when there is only one statement in the IF

2, the second type: If-else

if (logical expression) {        else  {        statement block 2;}

3, the Third kind of novice: If-else If-else

if (logical expression)        {elseif(logical expression) {        else  {        statement block 3; }

Second, switch statement

Switch (int expression) {         case integer constant value 1:     // entry 1              statement 1;             Statement 2;               Break  ;           Case Integer constant value 2:     // entry 2              statement 3;               Break  ;             ...          Default:               // defaults to entry              statement n;}

1 . Integer expression: char, Byte, short, int, and enum JDK 7.0 added string type

2, break function is to jump out of the switch statement, if not encountered break will continue to execute the Next statement:

 int  a=1 switch   (a) { case  0     0 case  1: System.out.println ( 1 Span style= "color: #000000;"    >);  case  2: System.out.println ( 2 Span style= "color: #000000;"        >);     break  ;  case  3: System.out.println ( 3 Span style= "color: #000000;"        >);  break  ;}  

Because A=1, so the entry in case 1: here, so immediately executes the statement System.out.println (1); found there is no break, so continue to execute System.out.println (2); Then exit the switch statement

3, in many cases, switch-case can replace the else if structure, and switch-case to achieve the efficiency of branching function than the else if structure , and the structure is clearer, so it is recommended. Starting with JDK 7.0, switch-case can support string expressions, which will make it easier for the program to operate.

The difference between the three, while and Do-while statements

While is first judged, in execution; Do-while is performed first in judgment. So do-while, in any case, executes more than the while.

Iv. for statement

1. For general wording

  For (expression 1; expression 2; expression 3 ) {

Statement block

}

For example:

 for (byte i=0;i<100;i++) {    System.out.println (i);}
View Code

2, for the special wording

The first type:

int sum = 0 ; int i = 1;  for (  ; I <=; i + + ) {    + = i;} System.out.println (
View Code

The second type:

int  sum = 0 ;  for int i = 1; I <= ;) {    + = i    ; ++ ;} System.out.println ("1 to 10 and for:" + sum);
View Code

The third type:

 for  (   ;   ;   ) {    System.out.println ("I want to learn ...")
View Code

This method will form a dead loop, and the same effect as while (true)

It is not difficult to see through the above several special wording, expression 1, expression 2 and expression 3 can be omitted, but the semicolon cannot be omitted

The fourth type:

 for int  I =1, j = 6  ;  I <= 6  ;  I +=2, J-=2 ) {    = "+ i +", "+ J");}
View Code

The fifth type:

int [] arr={1,2,3,4,5,6,7,8};  for (int  i:arr) {    System.out.println (i);}
View Code

  

17. Java Process Control

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.