A Overview
Java provides two basic process control structures: branching and looping structures. Where the branching structure is used to choose to execute a piece of code based on conditions, the loop structure repeats a piece of code according to the loop condition. The spoke statements have if and switch two, and the loop statements have a for, while, and do and three kinds. In addition to this, jdk1.5 also provides a foreach loop, while Java also provides a looping structure for break and continue to control the program.
A Body
1 , sequential structure
This is the most common structure of the program, if there is no process control, Java will be executed from the top to the next time each statement, this is nothing to say.
2. Branching Structure
2.1 If Condition statement
If there are three different forms:
The first type:
if (logic expression) { statement ...}
The second type:
if (logic expression) { statement ...} Else { statement ...}
The third type:
if (logic expression) { statement ...} Else if (logic expression) { ifelse{ statement ...}
Note:the contents of the if parenthesis can only be a logical expression, and the return value can only be true or false. The third form of multiline code, called a block of code, is usually performed as a whole unless you encounter a keyword such as break, return, Contin, or an exception.
2.2 Switch Branch statement
The format is as follows:
switch case Condition1: {statement (s) break ; case Condition2: {St Atement (s) break ; } ...... case Conditionn: {statement (s) break ; default : {statement ( s)}}
Note:The data type of expression expressions after the switch statement can only be byte, short, char, int four integer types, and enumeration types.
3 , cyclic structure
3.1 While Loop statement
The format is as follows:
while (expression) { statement;}
Note:while (expression), is a trap, you can not add a semicolon, otherwise it will cause a dead loop, and must ensure that while there is a time to jump out of the loop, otherwise it will become a dead loop.
3.2 Do and loop statements
The format is as follows:
Do { statement;} while (expression);
Note: Unlike while, there must be a semicolon after the do and this semicolon indicates the end of the loop, while the first condition is judged, and if the loop body is executed as true, then the Do while executes the loop body before judging the condition, or the true continuation, or the end.
3.3 For Loop
The format is as follows:
for ([init_statment];[ Expression]; [Iteration_statement]) { statement;}
Note:The Init_statment initialization statement executes only once and then determines whether expression is true (expression can only be logical) and, if true, executes the loop body and executes the iteration_ after the loop body finishes. Statement, and then Judge Expreeion, if True then continue, otherwise end.
4 , control loop structure
4.1 Ending a loop with break
Break is used to completely end a loop, and sometimes we need to force the loop to terminate when a certain condition occurs, rather than wait until the loop condition is false to exit the loop.
Note: The general break only ends the current loop, but if the break is followed by a label, you can end the outer loop identified by the label (the label only works before the loop statement). For example:
outer: for (int i=0; i<5; i++) { for (int j=0; j<3; j + +) { if (j = =1 )break outer;} }
4.3 Using the return End method
Return is not intended to end a loop, but rather to end a method. As follows:
Public class test{ publicstaticvoid main (string[] args) { for int i=0; i<3; i++) { if(i = =1 )return; }}}
When I==1 is running to return, the current method is terminated directly, here is the end of the main method (the main method ends also means that the program ends).
Three Summarize
This is my simple summary of the basic use and format of the process control structure, in fact there are more special cases, such as for (;;) and while (true) is an infinite loop. The loop can also be nested and used, and the cycle is set in small loops. For loop comparison while,for loop the variable is freed in memory after it is used, that is, after the loop is finished, it is not available, while the other is the opposite, the variable exists after the loop ends and can continue to be used.
Dark Horse Programmer--java Basic---process control