Java is easy to get started with. java is easy to get started.

Source: Internet
Author: User

Java is easy to get started with. java is easy to get started.

1. syntax of the if structure:

<1> simple if: if (condition ){

// Code block

} Else {

// Code block

}

 

<2> multiple if: if (condition ){

Code block

} Else if (condition ){

Code block

} Else {

Code block

}

<3> nested if: if (condition ){

If (condition ){

// Code block

} Else {

// Code block

} Else {

// Code block

}

<4> switch (expression ){

Case constant 1:

// Code block

Break;

Case constant:

// Code block

Break;

........

Default:

// Code block

Break;

}

<5> while loop: while (loop condition ){

Cyclic Operation

}

Execution sequence: declare and initialize cyclic Variables

Determine whether the conditions are met, met, and executed. Otherwise, exit.

After the cyclic operation is completed, judge the cyclic conditions again and decide to continue or exit

Features: first judgment and then execution

<6> do {

Cyclic Operation
} While (Cyclic condition)

Execution sequence: declare and initialize cyclic Variables

Perform a loop operation

Judge the cycle condition. If the cycle condition is met, the loop continues to be executed; otherwise, the system exits.

Feature: Execute the command first and then judge

<7> for (expression 1, expression 2, expression 3 ){

// Loop body

}

Execution sequence: First executes the initialization part, then executes the condition judgment, then executes the loop body, and then calculates the iteration part. If the condition is not true, the loop jumps out.

Method to generate a random number: int random = (int) (Math. random () * 10 );

Common logical operators: & (and, and), | (or, or ),! (Not)

Basic if selection structure: handling single or combination Conditions

If-else select structure: a simple conditional branch structure can be processed.

Multi-if selection structure: it can handle condition branches in consecutive intervals

Nested if selection structure: it can handle complicated condition branches

2. break and continue:

<1> roles of the break statement
(1) The break statement can only be used in the loop body and switch statement body.
(2) When the break appears in the switch statement body of the loop body, it only jumps out of the switch statement body.
(3) When the break appears in the loop body but is not in the switch statement body, it jumps out of the loop body at the current layer after the break is executed.
(4) In the loop structure, the break statement is applied to make the process jump out of the cycle body of the Current layer, so as to end the cycle of the current layer in advance.

<2> Functions of the continue statement
(1) The general form of the continue statement is contonue;
(2) its function is to end this cycle, that is, to skip the remaining statements in the cycle body that have not been executed, and then judge the conditions of the cycle again.
(3) Note: The execution of the continue statement does not terminate the entire loop. In the while and do-while loops, The continue statement causes the process to jump directly to the test section of the loop control condition, and then determines whether the loop continues.
(4) In a for loop, when a continue is encountered, skip the remainder statement in the loop body and evaluate the value of "expression 3" in the for statement, then perform the conditional test of expression 2, and determine whether to execute the for Loop Based on the value of expression 2. In the loop body, no matter what statement the continue is, it will be executed according to the above function, which is different from break.

 

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.