4. Control the execution process and control the execution process

Source: Internet
Author: User

4. Control the execution process and control the execution process

I. the comma operator can define multiple variables in the for statement, but they must be of the same type.

In the control expression initialization and step-by-step Control Section, you can use a series of statements separated by commas, and those statements are executed independently.

 

Ii. foreach

1 float f[] = new float[10];2 3 for(float x: f){4      //...      5 }

The code above defines a float variable x, and then assigns each f element to x

In addition, foreach can be used for any Iterable

For the following for statement, foreach cannot play a role

1 for(int i = 0; i < 100; i++)

However, the range () function can be used to complete the loop.

1  for(int i : range(10)) 2 3  for(int i : range(5, 10)) 4 5  for(int i : range(5, 20, 3))

 

3. The range () method has three reloads.

1. range (10) indicates from 0 to 9

2. range (5, 10) indicates from 5 to 9

3. range (5, 20, 3) indicates from 5 to 20. The step value is 3.

 

4. The break statement indicates that the loop is forcibly exited and the remaining statements are not executed.

The continue statement indicates that the current iteration is stopped, and the next iteration starts at the beginning of the returned loop.

 

5. goto in Java

 1 lable1: 2 outer-iteration { 3     inner-iteration { 4          //... 5          break; 6          //... 7          continue; 8          //... 9          continue lable1;10          //...11          break lable1;12     }  13 }

1. break interrupts the internal iteration and returns to the external iteration.

2. continue enables the execution of point shifting to start at the internal iteration

3. continue label1 interrupts both internal and external iterations and directly transfers them to label1.

4. break label1 also interrupts all iterations and returns to label1, but does not re-enter the iteration.

The only reason to use tags in Java is that there is loop nesting, and you want to use break or continue from multi-layer nesting.

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.