Java Basic syntax

Source: Internet
Author: User

The last time we talked about basic type conversions in Java and some common operators, today we talk about the underlying syntax in Java

Java Basic Syntax:

Conditional statements------Execute different statements according to different conditions;

If

If.. Else

If.. else if

If.. else if: else.. If.. Else

Switch

Loop statements-----repeat certain actions;

for (JDK1.5 syntax)

While

Do.. While

If statement:

If

If.. Else

If.. else if

If.. else if: else if;

If.. else if: else if: Else

Only one sentence that needs to be executed is to omit {} but not recommended;

Be careful not to add ";" After the IF ;

For Loop statement:

for (expression 1; expression 2; expression 3) {statement; ...;}

Example of a For loop statement:

Prime numbers in the output 101~200:

While & does while statement:

While statement:

while (logical expression) {statement; ...;}

Do While statement:

Do while{statement, ...;} while (logical expression);

Break & Continue

The break statement is used to terminate the execution of a block of statements in the body of a loop statement: for example:

Results:

The continue statement is used in the Loop statement body to terminate a cyclic process,

Skips a loop that is not executed under the Continue statement for example;

Results:

Switch statement (conditional statement supplement):

Switch () {

Case XX:

......

Case XX:

......

Case XX:

.....

}

Note: It is recommended to use the break statement for case penetration;

Default can be omitted, but omitted is not recommended;

The value inside the Switch "()" must be of type int, but Byte, Short,char is OK, because they are automatically converted to int.

Java Basic syntax

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.