Java basic grammar 02

Three flow control statements

1, the order of the structure

Any programming language is the most common program structure is the order structure. Order from top to bottom row by row configuration program is executed, there is no intermediate judgment, and jump.

2, branch structure

if (conditional expression) {
 statements thereof;
}

Implementation process

  • First, determine the conditional expressions see the result is true or false

  • If the statement is true on the implementation of the body

  • If false statement is not executed somatic

if (relational expression) {
 statement body. 1;
} {the else
 sentence 2;
}

if (judgment condition 1) {
 execute statement 1;
} the else if (judgment condition 2) {
 execute statement 2;
}
...
} the else if (n-determination condition) {
 execute statement n-;
} {the else
 executing statements n + 1;
}

if..else nesting

If the statement in the block, or else statement is a block, and further includes a judgment condition (may be a single branch, two branches, a multi-branch)

Execution features:

(1) If the nested if statements in the block only if the condition is satisfied when the external, to judge the condition inside only

(2) if it is nested within the else block only when the external conditions are not met if, after entering else, only to judge the condition inside

if()

else // When the statement is executed only one article, we can omit the curly braces

switch selection structure

switch (expression) {
  case 1 constant value:
    a block of statements 1;
    {break;}
  case constant value 2:
    a block of statements 2;
    {break;}
  . . .
  [Default:
    statement block n + 1;
  {break;}
}

(1) Entry

① When a constant value and the value of the latter case switch (expression) match, the process proceeds from the Case;

② constant value does not match all the values ​​following the case when the switch (expression), finding default branch to enter; no matter where default

(2) Once the "entry" into the Switch, execution continues downward until it encounters the "export" may occur through

(3) exports

① natural outlet: encountered the end switch}

② interrupt exports: encountered a break, etc.

note:

(1) the value of the type of switch (expression), can only be: four basic data types (byte, short, int, char), two kinds of reference data types (enumeration after JDK1.5, after JDK1.7 String )

(2) the rear case must be a constant value, and can not be repeated

3, loop

Regardless of what kind of programming language, will provide two basic flow control structure: branch structure and loop structure. Wherein the branch structure for realizing a piece of code to selectively performed depending on the condition, the loop structure for implementing the code are repeatedly performed according to a certain cycle conditions.

for loop

Loops may be a case where the loop condition repeatedly execute a section of code, the code period is repeated is referred to as loop statement, when this loop is repeatedly executed, so that the need to modify the circulation loop variable determination condition is false , thus ending the cycle, otherwise the loop will always execute it, forming an endless loop.

for (initialization statement ①; ② loop conditional statement; iteration statement ④) {
  loop statement ③
}
for (;;) {
  loop statement block; // if there is no statement in the loop out of the loop body, it is an endless loop
}

(1) in two for (;;); it is no more and no less

(2) The cycling conditions must be a boolean

(3) If the loop condition statement ② omitted, then it defaults to cycling conditions are satisfied

Implementation process:

  • Step 1: Execute initialization statement ①, to complete the loop variable initialization;

  • The second step: the implementation of conditional statements cycle ②, look at the value of the loop condition statement is true, or false;

    • If true, the implementation of the third step;

    • If it is false, loop aborted cycle is not executed.

  • The third step: execution loop statement ③

  • Step four: Perform iterative statements ④, re-assignment for the loop variable

  • Step 5: The new value of the loop variable re-start from the second step and then execute it again

while loop

while (loop condition statement ①) {
  loop statement ②;
}
while (to true) {
  loop statement; // if this time is not circulating loop statement out, but also endless loop
}

while (loop condition) and cycling conditions must be a boolean

Implementation process:

  • Step 1: Execute cycle conditional statements ①, see the value of circulating conditional statement is true, or false;

    • If true, the second step;

    • If it is false, loop aborted cycle is not executed.

  • The second step: the implementation of the loop statement ②;

  • The third step: after the implementation of the loop statement, then start again from the first step to perform again

2, while the extended loop format:

Initialization statement ①;
the while (loop condition statement ②) {
  loop statement ③;
  iteration statement ④;
}

Implementation process:

  • Step 1: Execute initialization statement ①, to complete the loop variable initialization;

  • The second step: the implementation of conditional statements cycle ②, look at the value of the loop condition statement is true, or false;

    • If true, the implementation of the third step;

    • If it is false, loop aborted cycle is not executed.

  • The third step: execution loop statement ③

  • Step four: Perform iterative statements ④, re-assignment for the loop variable

  • Step 5: The new value of the loop variable re-start from the second step and then execute it again

do ... while loop

do {
  loop statement ①;
} the while (loop condition statement ②);

(1) while (loop condition) and cycling conditions must be a boolean

(2) do {} while (); Finally there is a semicolon

(3) do ... while loop sentence structure that will execute at least once, and for this and while is not the same

Implementation process:

  • The first step: the implementation of the loop statement ①;

  • The second step: the implementation of conditional statements cycle ②, look at the value of the loop condition statement is true, or false;

    • If true, the implementation of the third step;

    • If it is false, loop terminates, the cycle is not executed.

  • The third step: conditional statements after the implementation cycle, and then start again from the first step to perform again

2, do ... while loop Extended format:

Initialization statement ①
do {
  loop statement ②;
  iteration statement ③;
} the while (loop condition statement ④);

Implementation process:

  • Step 1: Execute initialization statement ①, to complete the loop variable initialization;

  • The second step: the implementation of the loop statement ②;

  • The third step: the implementation of the iteration statement ③, reassigned for the loop variable;

  • Step four: Perform loop conditional statements ④, look at the value of the loop condition statement is true, or false;

    • If it is true, according to the new value of the loop variable, and then start again from the second step to perform again;

    • If it is false, loop aborted cycle is not executed.

The difference between the loop

  • From the analysis of the number of cycles

    • do ... while loop executes at least once a loop statement

    • for while loops and conditional statements first loop is established, and then decide whether to execute the loop body is executed at least zero loop statement

  • From the perspective of the life cycle of the loop variable analysis

    • for loop the loop variable () declared in for, at the end of the loop, can not be accessed;

    • while and do ... while loop the loop variable declared as outside, so while and do ... while the end may continue to be used;

  • how to choose

    • Traversing significant demand cycles (range), selected for loop

    • Traversal no demand significant number of cycles (range), the cycle while loop

    • If the loop body block of statements executed at least once, consider do ... while loop

    • In essence: between the three cycles can be converted to each other, can realize the function of circulation

  • Three loop structure has four elements:

    • Initialization expression (1) of the loop variable

    • (2) The cycling conditions

    • Modified iterative expressions (3) loop variable

    • (4) loop statement block

Control statements break

Usage scenarios: termination switch or the current cycle, there is use to leave the scene does not make sense

continue

Usage scenarios: the end of this cycle, the next cycle continues

Nested loop

The so-called nested loops , a loop means is another circulation loop. For example, there is also a for loop for loop is nested loops. = Total number of cycles cycles cycles * the outer. Of course, any three loop may be nested with each other.

for (initialization statement ①; loop condition statement ②; iteration statement ⑦) {
  for (initialization statement ③; ④ conditional statement cycle; iteration statement ⑥) {
    loop statement ⑤;
  }
}

for (;;) // When only one block of statements, or if, when the for statement {} may be omitted, also known as compound statement. But not recommended

  Statement block;

  

Guess you like

Origin www.cnblogs.com/Open-ing/p/11857488.html