Java Learning (eight)

Source: Internet
Author: User

Double cycle

I. Review of 3 cycle structures

1. While

Grammar

The initial value of the conditional expression;

while (conditional expression) {

Cyclic operation;

Change the statement of the conditional expression;

}

Features: First judge, then execute, there may be no cycle

Applicable occasions: The number of cycles is unknown

form of expression

First: The number of cycles is determined

A conditional expression that determines the variable of an integral type in a range

while (i<=5) {//Do not need to receive user input}

The second type: Unlimited number of cycles, user input a value end loop

Conditional expressions, judging by the values entered by the user

While (the user-entered value is compared to a value) {

such as input 0 or (yes/no) end loop

}

2, Do...while

Grammar

The initial value of the conditional expression;

do{

Cyclic operation;

Change the statement of the conditional expression;

}while (conditional expression);

Features: First execution, then judgment, at least 1 cycles

Suitable for occasions, same while loop

Representation, same while loop

3. For

Grammar

for (initial value; conditional expression; iteration part) {

Looping operations

}

Features: The same while loop, are first judged, then executed, there may be 1 cycles are not executed

Applicable occasions: fixed cycle times

Ii. 2 common statements in a cycle

break--Terminate Loop (when loop is not complete, end loop prematurely)

continue--end this cycle and go to the next cycle

Iii. Review of the array

1. Declaring arrays

Data type "" Array name;

"" can also be placed after the array name

2. Allocate space (length) for an array

Mode 1: "Length" to allocate space, element only default value

data type [] array name =new data type [length];

An array of type int, the default value of the element is 0

An array of type string, with a default value of NULL

Method 2: Determine the allocated space based on the number of values

data type [] Array name ={value list};

int[]nums={1,2,3};

Method 3: Determine the allocated space based on the number of values

data type [] array name =new data type []{value list};

int [] nums= new int[]{1,2,3};

3, traversal of the array, the general situation is used for the loop value or dynamic assignment

for (int variable = initial value; conditional expression; variable change) {

Subscript >=0 and Subscript < array name according to subscript value . Length }

Four or two heavy cycle

1, in the loop, nested another loop, the inner layer of the loop, as a circular operation of the outer loop

2, the common double cycle

Form 1: outer while or do...while

Inner layer for loop

Form 2: Outer and inner layers are for loops

Form 3: Outer layer is for loop, inner is while or do...while

3. Order of execution of double cycle

1, first execute 1 outer loop;

2, after entering the outer loop, it is necessary to cycle all the inner layers,

To proceed with the outer 2....N cycle.

continue and break in the double cycle "recent principles"

1, continue: End the current cycle from its "recent cycle structure", into the cycle structure of the next cycle

2. Break: Terminates the cycle from its "nearest loop structure"

Six or two heavy cycle common ways

1. For loops nesting each other

for () {

For () {

}

..........................

0}

2. For loop, nested while or do...while

for () {

While () {

}

or

do{

}while ();

}

Java Learning (eight)

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.