Java Foundation 3_ Loop statement, array

Source: Internet
Author: User

Loops in Java:

The 3 loop structure is available in Java: While the do-while for

① the function of cyclic structure? The code in the loop structure can be repeatedly executed;

② the above 3 cycle structure functions are the same, but the structure is not the same;

Control of the number of ③ cycles

④ the syntax and execution flow of various cyclic structures;

⑤ can distinguish each of the characteristics and know how to choose

While syntax:

while (condition) {

Loop body, the code of things that needs to be repeated

}

Execution process:

The loop structure above may perform n cycles

First time: Condition (TRUE)---> Executes a loop body;

Second time: Condition (TRUE)---> Executes a loop body;

。。。。

N: Condition (FALSE)---> End entire while loop

Do-while

bo=

Loop body, the code of things that needs to be repeated

}while (conditions);

Do-while will do it once, and the interpretation is to see if the next loop is executed.

For

For (① initialization statement; ② condition judgment; ③ post-loop operation) {

④ Loop Body

}

In a For loop structure, ① only executes once

Execution process:

1 Cycles: ①--② (True)--④--③

2 cycles: ② (True)--④--③

3 Cycles: ② (True)--④--③

4 Cycles: ② (True)--④--③

。。。

N times ② (false)--end

It has a total cycle of n-1 times, judging n times

for (;;) {}----"Dead loop"

summary of three cycle characteristics:

The while statement is first judged in execution: the number of judgements is n+1 times, the number of executions is n times

The Do_while statement performs the re-judgment first, the same number of executions and the number of judgements

For loop judgment n+1 times, execute n times

Loop in control statements

Break

End the current layer of loops

Continue continue

Return End Current method

Method: A block of code capable of completing a function independently

Feature: Called only when needed, and can be called repeatedly

Definition method: ① determine the parameter list

② Determining the return value type

Overload: The method name is the same, the parameter list is different

Array

① must first have an array (define array, declare array)

Declaration format: array of element type [] array name;

Example: int[] ages; Represents a variable that declares an array type.

And there's no real space allocated in memory.

Creation of a array-dynamic creation

1 first declaration after creation;

Int[] ages;

ages = new Int[5];

2. Assign values immediately after the declaration;

Int[] ages = new INT[5]; Indicates that an array of length 5 has been created

This array can hold data of 5 int types

② storing data in arrays

Array name + Index

Ages[0] = 18; Put 18 in the first lattice of the ages

③ take out the data in the array

Direct print System.out.println (ages[0]) after removal;

can also participate in the Operation System.out.println (Ages[0] + ages[1]);

From the parameter passing mechanism in Java:

Value pass or reference pass?

The base data type is the value pass, and the reference type is the reference pass

It can also be said that all are value passing (when a reference type is passed the value is the address of the reference)

Array creation-static mode

When you create an array, you know what values you need to store, and when you create them, write the values directly.

General wording: int[] age = new int[]{11,22,33,44,55};

Age[0] = 10;

Shorthand for static creation: int[] age = {11,22,33,44,55}; Must be declared immediately after assigning a value!

Int[] age;

Age = {11,22,33,44,55}; Error!!

Array, once an array object is created, its length is fixed (static, dynamically created)

Java Foundation 3_ Loop statement, array

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.