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

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.