Java Basic Learning Notes--8 (looping statements)

Source: Internet
Author: User

1. Cyclic structure

Cyclic three elements: A. Cyclic variable initial value B. Loop condition (boolean value) C. Loop variable increment (self-increment or decrement)

1) While Loop

Syntax: while (Boolean expression) {

statement block;

}

Execute: When the expression that follows the while is true, the statement block contents are executed until the Boolean expression is false and no longer executes.

The while loop is executed before it is judged and may not be executed once.

Case 17:

650) this.width=650; "Title=" 17_4zvhp00 (ajs5.png "src=" http://s3.51cto.com/wyfs02/M01/6C/F8/ Wkiol1vzywni41vcaae9pfcbadu580.jpg "alt=" Wkiol1vzywni41vcaae9pfcbadu580.jpg "/>

2) Do...while cycle

Syntax: do {

statement block;

} while (Boolean expression);

Execution: Executes the statement block content after do once, then determines the expression after the while, if the expression is true, then continues to execute the statement block, is no longer executed.

Do...while ... The Loop statement is executed first and then judged at least once.

Case 18:

650) this.width=650; "Title=" 18_u}1le (d6zlbw.png "src=" http://s3.51cto.com/wyfs02/M01/6C/FD/ Wkiom1vzydtsqk5taafrkb5a9qi174.jpg "alt=" Wkiom1vzydtsqk5taafrkb5a9qi174.jpg "/>

Case 19:

650) this.width=650; "title=" 19_%g8wd[7or]3g.png "src=" http://s3.51cto.com/wyfs02/M02/6C/FD/ Wkiom1vzyfxhtuipaaf7no5cw7o535.jpg "alt=" Wkiom1vzyfxhtuipaaf7no5cw7o535.jpg "/>

output: You can see that the while loop has no result, and the do...while has output results.

650) this.width=650; "Title=" 19_19_ (amg%bo18cgj.png "src=" http://s3.51cto.com/wyfs02/M02/6C/F8/ Wkiol1vzygedxx83aaar90ptofk338.jpg "width=" 548 "height=", "alt=" Wkiol1vzygedxx83aaar90ptofk338.jpg "/>"

3) for Loop

Syntax: for (expression 1; expression 2; expression 3) {

Statement block (loop body);

}

Perform:

1) Calculates the value of the expression 1, usually assigning the initial values to the loop variable;

2) evaluates the value of expression 2 (expression 2 is a logical expression), that is, determines whether the loop condition is true,

If the value is true then the loop body is executed once (statement block), otherwise jumping out of the loop;

3) Execute the loop body;

4) evaluates the value of the expression 3, where it is usually written to update the assignment expression of the cyclic variable;

5) The value of the expression 2 is calculated, if the value is True the loop body is executed, otherwise it jumps out of the loop;

6) This loop repeats until the value of expression 2 is false.

Case 20:

650) this.width=650; "title=" 20_@{%6%kmno3h.png "src=" http://s3.51cto.com/wyfs02/M02/6C/F8/ Wkiol1vzyorijpadaafeidovrp0870.jpg "alt=" Wkiol1vzyorijpadaafeidovrp0870.jpg "/>

Case 21:

650) this.width=650; "title=" 21_87k_hy ' Qfhri.png "src=" http://s3.51cto.com/wyfs02/M02/6C/F8/ Wkiol1vzyqfixnp1aaj2szgozt8545.jpg "alt=" Wkiol1vzyqfixnp1aaj2szgozt8545.jpg "/>

4) Enhanced for loop

The JDK introduced an enhanced for loop in version 1.5, which can be used for iterating over arrays and collections. Note: You must have a value in the collection, or exit directly (do not execute the loop).

① old cycle: self-maintenance cycle times, the loop body to maintain the method of acquiring elements.

Int[] Array=new int[]{1,2,3,4,5,6,7}; Defines the way the array is, and later

for (int i=0; i<array.length; i++) {//Maintenance cycle count

int element=array[i];//Get array elements

System.out.print (Element);

}

② New loop: Automatically maintains the number of cycles (determined by the length of the traversed array or collection) and automatically gets the elements of each iteration.

Int[] Array=new int[]{1,2,3,4,5,6,7};

for (int element:array) {

System.out.print (Element);

}

③ new Loop execution flow: Iterates through each element of the array, assigns the element once to element and enters the loop body until all elements have been iterated and exits the loop.

④ Note: With a new loop, the element type should be the same as the array of the loop iteration or the type of the elements in the collection! At least if the compatibility type! The internal implementation of the new loop is the iterator that is done with the iterator.

2. Break and Continue keywords

Break:break is used in loop statements, which can terminate loops and are often used in conjunction with conditional statements.

The Continue:continue statement is used in a looping statement to continue the next loop in order to skip the secondary loop.

Case 22:

650) this.width=650; "Title=" 22mh17$qxym (~p.png "src=" http://s3.51cto.com/wyfs02/M01/6C/F8/ Wkiol1vzza3a0eckaag2m7gxawe500.jpg "alt=" Wkiol1vzza3a0eckaag2m7gxawe500.jpg "/>

Output Result:

650) this.width=650; "title=" 222xfg5rzson8wu.png "src=" http://s3.51cto.com/wyfs02/M01/6C/FD/wKiom1VZYq_ Rorswaabjzsuvzoc553.jpg "alt=" Wkiom1vzyq_rorswaabjzsuvzoc553.jpg "/>

3. Exercises

Case 23:

650) this.width=650; "style=" width:674px;height:466px; "title=" 23RWSI () 6%jtn4.png "src=" Http://s3.51cto.com/wyfs02 /m00/6c/fe/wkiom1vzy6gzjicoaamjfviahx0380.jpg "width=" 828 "height=" 535 "alt=" wkiom1vzy6gzjicoaamjfviahx0380.jpg "/ >

Output Result:

650) this.width=650; "Title=" 2323tqmrhrr6a) (7.png "src=" http://s3.51cto.com/wyfs02/M00/6C/FE/wKiom1VZY_ Ajaletaad9ylki6bk384.jpg "alt=" Wkiom1vzy_ajaletaad9ylki6bk384.jpg "/>

Case 24:

650) this.width=650; "Title=" 24~3g0qkvg ({5d.png "src=" http://s3.51cto.com/wyfs02/M01/6C/FE/ Wkiom1vzza-bxbjfaalbreu9alc813.jpg "width=" 670 "height=" 567 "alt=" wkiom1vzza-bxbjfaalbreu9alc813.jpg "/>

This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1652250

Java Basic Learning Notes--8 (looping statements)

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.