1. Program Flow Control#
1.1 Program Flow Control
- Flow control statements are used to control the execution order of statements in a program, and can combine statements into small logical modules that can perform certain functions.
- Its flow control method adopts the three basic flow structures specified in structured program design, namely:
Sequential structure
Branch structure
Loop structure
Sequential structure
The program is executed line by line from top to bottom without any judgment or jump in between.
Branch structure
Execute a certain code segment selectively based on conditions.
There are two types of branch statements: if...else and switch-case.
Loop structure
Repeatedly execute a certain code segment based on a loop condition.
There are three types of loop statements: while, do...while, and for.
Note: JDK1.5 provides the foreach loop, which is convenient for traversing collection and array elements.
2. Sequential Structure#
Sequential structure
Valid forward references are used when defining member variables in Java. For example:
public class Test{
intnum1 = 12;
intnum2 = num1 + 2;
}
Incorrect form:
public class Test{
intnum2 = num1 + 2;
intnum1 = 12;
}
3. Branch Statement 1:#
3.1 if-else Structure#
Three formats of if statement:
1. if(condition){
code block;
}
2. if(condition){
code block1;
}
else{
code block2;
}
3. if(condition1){
code block1;
}
else if (condition2){
code block2;
}
......
else{
code blockn;
}
Program Flow Control: if-else Structure#
1. The condition expression must be a boolean expression (relational expression or logical expression), and when there is only one execution statement in the code block, a pair of {} can be omitted, but it is recommended to keep it.
2. The if-else statement structure can be nested as needed.
3. When the if-else structure is "choose one", the last else is optional and can be omitted as needed.
4. When multiple conditions are "mutually exclusive", the order of condition judgment statements and execution statements does not matter.
5. When multiple conditions are "inclusive", "small up and big down / child up and parent down"
Example of if-else statement application#
public class AgeTest{
public static void main(String args[]){
int age = 75;
if (age< 0) {
System.out.println("Impossible!");
} else if (age>250) {
System.out.println("It's a monster!");
} else {
System.out.println("The age is " + age + ", not bad!");
}
}
}
3.2 Branch Statement 2:#
switch-case Structure#
switch(expression){
case constant1:
statement1;
//break;
case constant2:
statement2;
//break;
......
case constantN:
statementN;
//break;
default:
statement;
//break;
}
Example of switch statement 1#
public class SwitchTest {
public static void main(String args[]) {
int i = 1;
switch (i) {
case 0:
System. out.println("zero");
break;
case 1:
System. out.println("one");
break;
default:
System. out.println("default");
break;
}
}
}
Example of switch statement 2#
String season = " summer ";
switch (season) {
case "spring":
System. out.println("Spring is here");
break;
case "summer":
System. out.println("It's summer");
break;
case "autumn":
System. out.println("Autumn is coming");
break;
case "winter":
System. out.println("Winter is coming");
break;
default:
System. out.println("Invalid season");
break;
}
Rules for switch statement#
1. The value of the expression in switch(expression) must be one of the following types: byte, short, char, int, enum (jdk 5.0), String (jdk 7.0);
2. The values in the case clause must be constants, not variable names or uncertain expression values;
3. In the same switch statement, the constant values in all case clauses must be different from each other;
4. The break statement is used to exit the switch statement block after executing a case branch; if there is no break, the program will execute sequentially until the end of the switch
5. The default clause is optional. Also, its position is flexible. When there is no matching case, execute the default clause.
4. Loop Structure#
Loop structure
Repeatedly execute specific code under certain conditions
Loop statement classification
for loop
while loop
do-while loop
Four components of a loop statement
Initialization part (init_statement)
Loop condition part (test_exp)
Loop body part (body_statement)
Iteration part (alter_statement)
4.1 Loop Structure 1:#
for loop#
Syntax
for(①Initialization part;②Loop condition part;④Iteration part){
③Loop body part;
}
Execution process:
①-②-③-④-②-③-④-②-③-④-.....-②
Explanation:
②Loop condition part is a boolean type expression, and when the value is false, the loop exits
①Initialization part can declare multiple variables, but they must be of the same type, separated by commas
④There can be multiple variable updates, separated by commas
Demonstration of for loop execution#
Example of application
public class ForLoop {
public static void main(String args[]) {
int result = 0;
for (int i = 1; i <= 100; i++) {
result += i;
}
System. out.println("result=" + result);
}
}
4.2 Loop Structure 2:#
while loop#
Syntax
①Initialization part
while(②Loop condition part){
③Loop body part;
④Iteration part;
}
Execution process:
①-②-③-④-②-③-④-②-③-④-...-②
Explanation:
Be sure not to forget to declare ④Iteration part. Otherwise, the loop will not end and become an infinite loop.
for loop and while loop can be converted to each other
Example of application
public class WhileLoop {
public static void main(String args[]) {
int result = 0;
int i = 1;
while (i <= 100) {
result += i;
i++;
}
System. out.println("result=" + result);
}
}
4.3 Loop Structure 3:#
do-while loop#
Syntax
①Initialization part;
do{
③Loop body part
④Iteration part
}while(②Loop condition part);
Execution process:
①-③-④-②-③-④-②-③-④-...②
Explanation:
The do-while loop executes the loop body at least once.
Example of application
public class DoWhileLoop {
public static void main(String args[]) {
int result = 0, i = 1;
do {
result += i;
i++;
} while (i <= 100);
System. out.println("result=" + result);
}
}
4.4 Nested Loop#
Nested loop (multiple loops)
Putting one loop inside another loop forms a nested loop. Among them,
for, while, and do...while can be used as outer loops or inner loops.
In essence, the nested loop treats the inner loop as the loop body of the outer loop. When only the loop condition of the inner loop is false, the inner loop can be completely skipped, and the current loop of the outer loop can be ended to start the next loop.
Assuming that the number of outer loop iterations is m and the number of inner loop iterations is n, the inner loop body actually needs to be executed m*n times.
5. Usage of Special Keywords:#
break, continue#
Special flow control statements 1#
break statement
The break statement is used to terminate the execution of a certain statement block
{ ......
break;
......
}
When the break statement appears in a nested statement block, you can specify which level of statement block to terminate by using a label
label 1 : { ......
label 2 : { ......
label 3 : { ......
break label 2 ;
......
}
}
}
Special flow control statements 1#
Usage of break statement
public class BreakTest{
public static void main(String args[]){
for(inti = 0; i<10; i++){
if(i==3)
break;
System.out.println(" i =" + i);
}
System.out.println("Game Over!");
}
}
Special flow control statements 2#
continue statement
continue can only be used in loop structures
The continue statement is used to skip the current execution of the loop statement block and continue to the next loop
When the continue statement appears in a nested loop body, you can specify which loop to skip by using a label
Usage of continue statement
public class ContinueTest{
publicstaticvoidmain(Stringargs[]){
for(inti= 0 ;i< 100 ;i++){
if(i% 10 == 0 )
continue;
System.out.println(i);
}
}
}
Additional: Special flow control statements 3#
return: It is not specifically used to end a loop, but to end a method.
When a method reaches a return statement, the method is terminated.
Unlike break and continue, return directly ends the entire method, regardless of whether the return is within multiple layers of loops.
Explanation of special flow control statements#
break can only be used in switch statements and loop statements.
continue can only be used in loop statements.
The two have similar functions, but continue terminates the current loop, while break terminates the current layer of the loop.
There should be no other statements after break or continue, because the program will never execute the statements after them.
Label statements must be immediately after the loop. Label statements cannot be used before non-loop statements.
Many languages have goto statements, which can transfer control to any statement in the program and execute it. However, goto is prone to errors. Java's break and continue are different from goto.