Basic Java syntax and java syntax

Source: Internet
Author: User

Basic Java syntax and java syntax

The last time we talked about basic type conversion and some common operators in Java, today we will talk about the basic syntax in Java.

Basic Java Syntax:

Condition Statement ------ execute different statements based on different conditions;

If

If .. else

If .. else if

If... else... if... else

Switch

Loop statement ----- repeats some 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 statement to be executed can be omitted {} but not recommended;

Do not add ";" behind if ";";

For Loop statement:

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

For Loop statement example:

Output 101 ~ Prime number within 200:

 

While & do 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 execution of a statement block in the loop statement body. For example:

Result:

The continue statement is used in the loop body to terminate a loop,

Skip the loop that is not executed under the continue statement in the loop, for example;

Result:

Switch Statement (conditional statement supplement ):

Switch (){

Case xx:

......

Case xx:

......

Case xx:

.....

}

Note: We recommend that you use the break statement for case-based penetration;

Default can be omitted, but not recommended;

The values in switch "()" must be int type, but byte, short, and char can be automatically converted to int type.

 

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.