Java Looping structure

Source: Internet
Author: User

The syntax of a switch structure (switching Statement)

Switch (expression [dream1]) {

Case Constant 1[dream2]:

Statement Block 1

break[dream3];

Case Constant N[dream4]:

Statement block N

Break

Default[dream5]:

Statement block

Break

}

second, switch structure use occasions: int \char 2 types of equivalence judgment to use

Iii. the similarities and differences between switch structure and if structure

Same point: equal judgment can be achieved

Different points:

L syntax is different

L Use different occasions.

N if structure, focusing on range judgment

N-switch structure, focusing on equivalence judgments, types can only be int, char type

L Efficiency

N in the equivalence judgment, The switch structure efficiency is higher than the IF structure

Iv. exception Handling--hasnextint ();

Determines whether an integer

If (input.hasnextint ()) {//boolean type

code block

}

second, While loop

1, the advantages of the cycle?

Reduce the coding of repetitive code, The program will be more concise

2. grammar

While (expression) {

1. The expression is a "loop condition" and the result must be a Boolean type

2, the code in {}, that is, "loop operation (body)"

3. "iteration section" changes the loop condition to end the loop

}

3, characteristics: First judge, then execute

4. While loop has 2 common ways

Mode 1: "P98 page--example code 5"

While (1, loop condition: determines whether strings are Equal) {

2. Cyclic operation

3. Receive the string in the keyboard again, change the loop condition, to end the loop

Method 2: "P97 Page Example 4 code"

While (1, loop condition: int variable combined with relational operator for Comparison) {

2. Cyclic operation

3, iteration part, int variable + + or--, to change the loop condition, end the loop operation

}

third,Do....while cycle

1, applicable occasions: First cycle 1 times, according to the conditions of judgment, recycling

2. grammar

do{

2. Circulating body (cyclic Operation)

3. Change the cycle condition

}while (conditions); 1. Cyclic conditions

Sixth chapter

one, for the use of the cycle of the occasion

L While Loop--first judge, recycle

while (1, conditional expression ) {

//2 , cyclic operation

//3 , changing the loop condition expression

}

L Do...while--cycle first, then judge

do{

//2 , cyclic operation

//3 , changing the loop condition expression

}while (1. Conditional Expressions );

The above 2 cycles, generally used in the case of uncertain cycle times

Number of known cycles, preferred for loop

Ii. syntax for A For loop

for (1. Initial Part  ; 2. condition judgment ; 3. Iteration Section ) {

4. cyclic operation

}

Iii. order of execution for a For loop

Section 1 Secondary Cycle

L Perform the initial part first ( only executed at 1 cycles )

L RE-JUDGE the Condition.

N If the condition is met, the loop operation is performed

N If the condition is not met, the loop ends

Section 2~n Sub-cycle-condition Satisfaction

After the 1th time the loop operation is completed

Perform the Iteration section first

And then the condition is Judged.

If the condition is met, the loop operation is performed

If the condition is not met, the loop ends

four, 2 Keywords

L Break " General and if conditional statements are used together "

To end a switch statement in a switch statement

To end or terminate a loop in a loop structure

L Continue

In the loop, end the loop and go to the next loop.

Double cycle

I. Review of 3 cycle structures

1. while

L Syntax

The initial value of the conditional expression;

While (conditional Expression) {

Cyclic operation;

Change the statement of the conditional expression;

}

L features: First judge, then execute, there may be no cycle

L applicable occasions: The number of cycles is unknown

L Presentation Form

n first: number of cycles determined

U conditional expression, which determines the variable of an integral type in a range

U while (i<=5) {//do not need to receive user input}

N Second: unlimited number of cycles, user input a value end loop

U conditional expression, Judging by the value entered by the user

U while (user-entered value is compared to a Value) {

such as input 0 or (yes/no) end loop

u}

2, Do...while

L Syntax

The initial value of the conditional expression;

do{

Cyclic operation;

Change the statement of the conditional expression;

}while (conditional expression);

L features: First execution, then judgment, at least 1 cycles

L for applications, same while loop

L representation, Same While loop

3. for

L Syntax

For (initial value; Conditional expression; Iteration Part) {

Looping operations

}

L Characteristics: the same while loop, are first judged, then executed, there may be 1 cycles are not executed

L Application: Fixed cycle times

Ii. 2 common statements in a cycle

Break--terminate loop (when loop is not complete, end loop prematurely)

Continue--end this cycle and go to the next cycle

Java Looping structure

Related Article

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.