First, the application of the background
Write Java program must master the Process Control statements, only master the Process Control statements, write Java program can DXN should also launch. A process is the order in which statements are executed when the program is run. A Process Control statement is the order in which each statement executes in a control program.
II. Classification of Process Control
1, sequential structure: according to the order of the statement to execute sequentially
2. Branch structure (or select structure): executes by selecting one of the multiple branches according to the criteria. Simply put, the program runs to a place where there is a judgment statement, it will be judged, according to the conditions of the selective downward execution. But sometimes there are multiple layers of judgment that need to be based on the actual situation to analyze the program's execution flow, analysis of each step to design what the conditions and requirements to execute after the statement. If the condition statement exceeds two, you need to enclose it in curly braces, and the switch statement does not forget the break and the default keyword.
(1), one-way condition selection: if (conditional expression) {
Statement sequence;
}
The execution of the program will be judged here if the condition of the if is entered into if, otherwise the statement inside the IF condition will not execute directly after the IF statement.
(2), two-way condition selection: if (conditional expression) {
Statement sequence 1;
}else{
Statement sequence 2;
}
The execution of the program here will be judged if the condition of the if is satisfied to enter if, otherwise it will execute ielse inside the statement;
(3), multiple conditional selection statement: if (conditional expression 1) {
Statement sequence 1;
}else if (conditional expression 2) {
Statement sequence 2;
}
.....
else if (conditional expression 2) {
Statement sequence N;
}
else {
Statement sequence n+1;
}
The execution of the program here will be judged if the conditions of a certain if to enter if, are not satisfied is to execute ielse inside the statement;
(4), switch SELECT statement:
switch (expression)
{
Case constant expression:
Statement sequence 1;
Break
Case constant expression:
Statement sequence 2;
Break
.
.
Case constant expression:
Statement sequence N;
Break
Default
Statement sequence n+1;
}
A multiple-condition selection statement similar to if (), else if (), selects the statement that meets the criteria to execute. Do not forget the break and default (the statement will be executed if the condition is not met)
The expression in switch can only be an integer expression or an enumeration constant, an integer expression that can be implicitly converted to an int type, such as Byte,short,char, so long and string cannot.
3, Cycle structure: Under certain conditions, repeated execution of a program, called the loop structure, repeated execution of the statement called the loop body, a certain condition is the cycle conditions.
(1), while statement: The execution process is the first to judge the condition, if true, the execution of the loop body, after the end of judgment, and then execute, until the judgment to the condition is false.
while (conditional expression) {
The loop body;//Can be a single statement or a compound statement
}
(2), Do-while statement: The execution is similar to the while loop, the difference is that it executes at least once, first executes before judging the condition.
Do
{
Circulation body;
}
while (conditional expression);
(3), for statement: is a more functional, a more extensive use of a circular structure. The execution process is the initial expression 1, after the judgment is satisfied with the expression 2, if satisfied, then execute the loop body, or exit the loop, and then execute the expression 3 (corresponding changes to the expression), then a new round of judgment, the execution of judgment body, until the expression 2, not satisfied (for false), exit the loop.
Three of these expressions can be empty, and that will turn into a dead loop, which requires a loop to be written out of the body.
for (expression 1; expression 2; expression 3) {
Circulation body;
}
Three, multiple cycles
If the loop statement has a looping statement, it is called a multi-loop, also called a nested loop, which is commonly used in sorting algorithms.
Iv. The statement of jumps in the loop
1, the function of the break statement is to make the process of the program from the switch statement or loop of the loop body to jump out, execute the other Next statement. Often used for switch statements, ending a case statement, and ending a loop statement.
2, the Continue statement must be used in the loop structure. The function is to terminate the current round of the loop, jump to the rest of the cycle of the statement, directly proceed to the next round. In the while and do-while loops, the Continue statement causes the process to jump directly to the conditional expression, and in the For statement, it jumps directly to expression 2, evaluates and modifies the loop variable, and then determines the loop condition.
3. The return statement is used to return the program from the method and returns a value for the method, and if the return statement does not appear in the method, the program executes the last statement of the method and automatically returns to the main program.
Process Control in Java