Fourth day essay

Source: Internet
Author: User

Fourth day notes

1. Circular Structure Overview:

Can be executed repeatedly, the same procedure

The loop is a certain condition, the condition is true, and the loop continues.

When you don't need a loop, the condition turns out to be false, and the loop stops.

2.for Cycle

Notation Format:

for (initialize variable; condition; increment) {

Loop body

The condition is true to execute the loop body

Condition is false, end loop

}

Initialize variables: Define variables and control the number of loops. Note: The scope of the variable

Condition: True to enter loop, false to end loop

Increment: Used for initialization variables

If you do not write three content, become a dead loop, will never end.

Code Demo:

Results:

3.while Cycle

Notation Format:

while (condition)

{

Loop body

The condition is true to execute the loop body

Condition is false, end loop

}

For and while when to use

For a loop that knows the number of cycles

While: for loops with ambiguous cycle times

Code Demo:

Number of daffodils printed:

Results Demo:

4.do while loop (preferably not used)

Notation Format:

do{

Loop body

}while (condition)

Loop unconditionally once, then judge the condition in while, if the condition is true, continue the loop, otherwise end the loop

Due to an unconditional run-time feature with a certain risk

Risk: Bulk transfer, using do While not judging the balance, direct advance transfer

5.break statements

Function: can be written in switch, can also be used in the loop, terminate the loop run

Code Demo:

Results:

6.continue statements

This keyword, the usage is very single, can only follow the loop appears, other places can not write continue. Terminates this cycle and begins the next cycle.

Code Demo:

Code Demo:

7.return statements

When a method encounters a return, it is over,

Return all the code below does not execute

8. Nesting loops

In one loop, another loop is written, and a nested for loop is

Outer loop for () {

Inner loop for () {

}

}

Code Demo: Printing triangles

Results:

Break-out effect in nested loops:

Terminates the loop in which the break is located

Terminate the outer loop loop, you can give the loop a name, terminate it.

Code Demo:

Results:

9. Method

methods, which are functions, are generally called methods in Java.

Method: The repeated and frequently used code is encapsulated together and invoked when used.

Concept: A method is a piece of code that can perform a function independently

10. Definition of the method (must be)

Format:

Modifier + type of return value + method name (parameter list 1, parameter List 2 ...) ){

Method body

Return;

}

Modifier: Fixed Write public static

Return value type: The data type of the result after the method is evaluated, if no return value is used, void

Method Name: Custom content, identifier specification

Parameter list: Method at the time of operation, there is no unknown data, if there is, write in the parameter list.

Call method: Write the name of the method to invoke and the parameter.

11. Considerations for the method definition:

A Do not execute without calling,

B In one method, no other method can be defined.

C method does not pass the data type at the time of the call

D To explicitly return a value, you must write a return.

E. If the return value of the method is Void,return can not write, you can also write the return directly;

F. If the method has a return value, it can be written directly in the output statement, and if there is no return value, it cannot be written in the output statement.

Code Demo:

Results:

12. Overloaded Properties of methods

Overloading of Methods (overload):

Concept: In the same class, a method with the same name can appear, as long as the method has a different argument list, which is

is an overload.

Parameter list is different: number, order, list

The method is not overloaded, and has no relation to the return value, modifier.

The JVM calls the overloaded method directly, depending on the type of the parameter.

Code Demo:

Results:

Fourth day essay

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.