Java basic syntax (3)

Source: Internet
Author: User

Java basic syntax (3)

Continue with the basic Java Syntax: JavaSE basics 5: Java BASICS (1) and JavaSE basics 6: Java BASICS (2 ).

Java commonly used three types of loops: while, do... while,.

(5) Java loop statement while

Syntax:

Execution Process:

1) Determine whether the condition after while is true/false ).

2) When the condition is set, execute the operation code in the loop, and then repeat 1), 2) until the loop condition is invalid.

Feature: first judge and then execute.

For example:

(6) do of Java loop statements... while

Do... the while LOOP syntax is similar to the while LOOP syntax, but the execution process is quite different.

Syntax:

Execution Process:

1) Perform a loop operation first, and then determine whether the cycle condition is true.

2) If the condition is true, continue to execute 1), 2) until the cycle condition is not true.

Feature: Execute the command first and then judge.

It can be seen that the do... while statement ensures that the loop is executed at least once. This is the difference from the while LOOP statement.

For example, I still output "I love Course Network" 1000 times. The implementation code of do... while is:

(7) Java loop statement

In the Java loop structure, besides the while and do... while, there are also for loops. The three loops can be replaced with each other.

Syntax:

Execution Process:

1) execute the initialization part of the loop variable and set the initial state of the loop. This part is only executed once throughout the loop.

2) judge the cyclic condition. If the condition is true, the code in the loop body is executed. If the condition is false, the loop is exited directly.

3) execute the change part of the loop variable to change the value of the loop variable for the next condition judgment.

4) re-execute them in sequence. 2), 3), 4) until the loop is exited.

Features: Compared with the while and do... while statements, the structure is simpler and easier to read.

For example, output 1000 times "I love Course Network" and use the for Implementation Code as follows:

Several small details to be aware:

1) The three expressions following the for keyword must be separated by ";". The three expressions can be omitted, but ";" cannot be ignored.

A. If "cyclic variable initialization" is omitted, the variable initialization operation can be performed by the assign value statement before the for statement, such:

B. Omitting the "loop condition" may cause the loop to be executed continuously, that is, the "endless loop" We often say, for example:

To avoid "Endless loops" during programming, you can use the break statement in the loop body to force the above Code to exit the loop.

C. If "cycle variable change" is omitted, you can change the cycle variable in the loop body, for example:

2) for Loop Variable initialization and loop variable change, you can use "," to initialize or change the values of multiple loop variables at the same time, such:

In the code, the initialization variable part grants initial values to the two variables I and j At the same time, and the variable variation part of the Loop Variable also changes the two variables at the same time. Running result:

3) in the cyclic condition section, expressions combined with logical operators can be used to represent complex judgment conditions. However, you must pay attention to the priority of calculation, for example:

In the code, the variable I must be less than 10 at the same time, And I is not equal to 5 before the loop is executed, the output variable I value.

(8) break statements for Java loop jump statements

In our daily life, we often interrupt our scheduled tasks for some reason. In Java, we can use the break statement to exit the specified loop,

Directly execute the Code following the loop.

For example, use a loop to output a value ranging from 1 to 10. If the value is greater than 2 and is a multiple of 3, the output is stopped.

Implementation Code:

Running result:

(9) continuation statement of Java loop jump statement

The continue statement skips the remaining statements in the loop body to execute the next loop.

For example, to print all the even numbers between 1 and 10, use the continue statement to implement the Code:

Running result:

(10) multiple loops of Java loop statements

The structure of the loop body containing the loop statement is called multiple loops. Three loop statements can be nested or nested. The most common one is double

Loop. In a double loop, the outer loop is executed every time, and the inner loop is executed once.

As follows:

For example, use * to print a rectangle:

Implementation Code:

Execution Process: When I = 1, the outer loop condition is true, enter the inner loop, and start to print the first line of content. At this time, j starts from 1, loops 8 times

Wrap the line after the layer loop ends to implement the 8 * output of the first line. Next, return the outer loop I to 2, prepare to print the next line, and push it like this until it is complete.

Rectangular printing.

Then let's make an output of a right triangle, for example:

Implementation Code:

 

Finally, let's add the conversion between the basic data types:

Basic data type conversion

1) The boolean type is not involved in conversion, because the values of false and true cannot be replaced by an integer of 0 or not. This is different from other advanced programming languages.

Same, be sure to pay attention to it.

2) integer, complex, and floating data types are converted to each other in hybrid operations. The conversion rules are as follows:

A default conversion

Small data types are automatically converted to large data types. data types are sorted by capacity:

Byte, short, char -- int -- long -- float -- double

Byte, short, and char are not converted to each other. During calculation, the data is directly converted to the int type for calculation.

B forced conversion

When you convert a data type with a large capacity to a data type with a small capacity, you must add a forced conversion character. However, this may reduce the precision or cause overflow. Pay attention to this when using the data type.

C. When there are multiple types of data mixed operations, the system prefers to automatically convert all the data to the data type with a large capacity before computation.

For more information about the examples, see Java basic syntax (1.

 

 

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.