Javase basic Knowledge (3)-Process Control structure

Source: Internet
Author: User
Tags case statement

First, sequential structure

1. Description
The program executes from top to bottom, without any jumps or selections in the middle.
2. Features
Variables must follow the forward reference
(Local variables must first be declared, assigned, and then used!) )

II. branching structure (conditions)

1. Description
Program from two or more paths, select one to execute

2. Classification

Grammar Execution order Precautions
If structure

1. Single Branch
if (condition) {
code block 1;
}

Determine whether the condition is set up first,

If this is true, code block 1 is executed. Otherwise, skip the if structure and proceed down

The result of the ① condition can only be a Boolean type, in the form of:
Relational expressions
Logical Expressions
Oolean variable
Boolean constant
An assignment statement for a Boolean variable
② code block can be 0 or more statements
The form can be output, input, branch, loop, etc.
③ If there is only one sentence in the code block, you can omit the curly braces

2. Dual Branch
if (condition) {
code block 1;
}else{
code block 2;
}

Determine whether the condition is set up first,

If this is true, code block 1 is executed. otherwise code block 2

3. Multiple branches
if (condition 1) {
code block 1;
}else if (condition 2) {
code block 2;
}...
else{
code block N;
}

First Judge condition 1,

If the execution code block 1 is set, otherwise the condition 2 is determined,

If this is true, execute code block 2, and so on, and if it does not, execute the code block n

4. Nested branches
if () {
if () {
}
}

First judge the outer conditions,

If it is, then the code block, that is, continue to determine the inner layer condition.

Switch structure Switch (variable) {
Case constant Value 1: code block 1;break;
Case constant Value 2: code block 2;break;
...
Default: code block N;break;
}

The value of the variable in the switch bracket is first derived,

Then each case is judged in turn, and if there is a match, the corresponding code block is executed until the break is met;

If none match, execute code block n in default until you meet break

Types of ① variables
can only be int, byte, short, Char
String (jdk1.7)
Enumeration (jdk1.5)
The constant value after ②case cannot be repeated
Can not be a variable after ③case
④break can be omitted, but may affect effect
⑤default can be omitted, position can be advanced, but may affect the effect

3. If and switch comparison ★

Same point: All belong to branch structure
Different points:
  If you can use switch, you can definitely use the IF
  can use if, not necessarily can use switch, must meet the following requirements:
① equivalent judgment
② the type of judgement must be int, byte, short, char, String (jdk1.7), enumeration (jdk1.5)
Note: using the switch structure is highly efficient!

Third, the cycle structure

1. Description

The structure of a piece of code is executed repeatedly on the basis of satisfying a particular condition

2. Four elements

① Loop Variable Initialization

② Cycle Conditions

③ Loop operation

④ Loop Variable Update

3. Benefits

① Improve Code Simplicity

② improve the maintainability and extensibility of code

4. Classification

While

Do While

For

After jdk5.0, the enhanced for loop is more

5, Concrete implementation steps ★

Question number one: know the frequency

Priority is given for

1. Frame First
for (int i=1;i<= times; i++) {Execute code;}

2. Analysis Cycle operation
3. Whether the detection cycle has four elements

Question number two: Do not know the frequency
1. Frame First
while () {
}
2, analysis cycle operation and cycle conditions
3. Whether the detection cycle has four elements

6, three kinds of cycle structure detailed introduction ★

(1) while

Grammar:

Cyclic variable initialization ①

while (loop condition) {②

Cyclic Operation ③
Cyclic variable update ④
}
Execution order: ①②③④②③④ .... Until ② is not established.

(2) Do While

Grammar:

Cyclic variable initialization ①
do{
Cyclic Operation ③
Cyclic variable update ④
}while (cyclic conditions); Ii
Execution order: ①③④②③④②③④ ... Until ② is not established.

(3) for

Grammar:

①②④
for (loop variable initialization; loop condition; cyclic variable update) {

Cyclic Operation ③
}
Execution order: ①②③④②③④ .... Until ② is not established.

7, three kinds of cycle structure comparison ★

Same point:
1, can solve any type of circulation problems
2, all have a circular four elements
Different points:

1. Different syntax

2. Different execution order
While and for first judgment after execution
Do and judge after first execution
3. Different implementation effect
If the first condition is true, the while, do, and for perform the same effect
If the first condition is not true, the while and for executes 0 times; Do while executes at least once!
4. Different implementation efficiency
Most efficient
5. Different application scenarios
Know the number of times, preference for
Do not know the number of times,
First Judge: While
Execute first: Do While

8. Nested Loops

Description: Another complete loop structure is nested within a loop structure
Note: such as outer loop, execute m times, inner loop, execute n times. The loop body of the inner loop finally executes m*n times

Four, jump structure

1. Description

Used to change the normal program flow
2. Classification
Break
Continue
Return
3, Jump structure detailed introduction

(1) Break
Features: typically used in loops or switch, jump out of the loop or switch
Grammar:
    Scenario 1: "Most used"
General collocation If structure is placed in a loop or directly in a case statement of switch
Scenario 2: "Understanding"
Directly with the tag, placed in any block of code

Example 1:
Label:if () {
Break label;
}
Example 2:
Label:while () {
while () {
if () {
Break label;
}
}
}
Note: If you do not match the tag, the default is to jump to the nearest switch or layer loop. If paired with a tag, you can jump out of the code block specified by the tag

(2) Continue
Features: can only be used in loops, end this cycle, continue the next cycle
Grammar:
Scenario 1: "Most used"
With the IF structure in the loop

while () {
if () {
Continue
}
}

Scenario 2: "Understanding"
Label:while () {
while () {
if () {
Continue label;
}
}
}
Note: Continue if you do not match the label, the default function is the one layer loop. If paired with a label, you can jump out of the loop specified by the label

(3) return
Features: Use in the method, jump out of the way

4. Jump Structure Comparison

Syntax application Scenarios Characteristics
Break Typically placed on a loop or switch Structure where the interrupt is located
Continue Can only be placed in the loop End this cycle and continue the next time
Return Used in the method Jump out of your way




Javase basic Knowledge (3)-Process Control 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.