Basic Java syntax: process control

Posted by TheRealPenguin on Tue, 21 Dec 2021 21:01:41 +0100

The process control mode adopts three basic process structures specified in structured programming, namely sequence structure, branch structure and cycle structure.
Sequential structure: the program is executed line by line from top to bottom without any judgment and jump.
Branch structure: selectively execute a piece of code according to conditions. There are two branch statements: if... else and switch case.
Loop structure: execute a piece of code repeatedly according to loop conditions. There are three loop statements: while, do... While and for. Note: jdk1 5 provides a foreach loop to easily traverse sets and array elements.

Branching structure

Branch statement: if else

// Format I:
if(Conditional expression) {
	Execute code block;
}

// Format 2 (one out of two):
if(Conditional expression) { 
	Execute code block 1;
} else {
	Execute code block 2; 
}

// Format 3 (one out of many): else can be omitted
// When multiple conditions are "include" relationship, "small upper and large lower / child upper and parent lower"
if(Conditional expression 1) { 
	Execute code block 1;
} else if (Conditional expression 2) {
	Execute code block 2;
else {
	Execute code block n;
}

Branch statement: switch case

switch(expression) {    // The value of the expression must be of data type: byte, short, char, int, enumeration (jdk 5.0), String (jdk 7.0);
	case Constant 1:    // The value in the case clause must be a constant, not a variable name or an uncertain expression value;
		Statement 1;
		break;    // The break statement is used to make the program jump out of the switch statement block after executing a case branch;
	case Constant 2:
		Statement 2;
		// break; //  If there is no break, the program will execute to the end of switch in sequence;
	......
	case constant N: 
		sentence N;
		// break; 
	default:     // 1. The default clause can be omitted. 2. You can also put it in front of the first case. 3. If there is no matching case, execute default.
		sentence;
		// break; 
}

Comparison of if and switch statements

  • If the judged values are small and conform to byte, short, char, int, String, enumeration and other types, it is recommended to use swtich statement. Because the efficiency is slightly higher.
  • For interval judgment and boolean judgment, if is used. If is used in a wider range.
  • If switch case is used, it can be rewritten as if else. Otherwise, it does not hold.

Cyclic structure

Loop statement: for

/* 
   Syntax format:
   1 Initialization part: multiple variables can be declared, but they must be of the same type, separated by commas
   2 Loop condition part: boolean type expression. When the value is false, exit the loop
   4 Iteration part: multiple variables can be updated, separated by commas

   Execution process: 1 - > 2 - > 3 - > 4 - > 2 - > 3 - > 4 - > 2 - > 3 - > 4 -... - > 2 (false, exit)
*/
for (1 Initialization section; 2 Cycle condition section; 4 Iterative part) { 
	3 Circulating body part;
}

Loop statement: while

/* 
   Syntax format:
   for Loops and while loops can be converted to each other
   
   Execution process: 1 - > 2 - > 3 - > 4 - > 2 - > 3 - > 4 - > 2 - > 3 - > 4 -... - > 2 (false, exit)
*/
1 Initialization part
while(2 Cycle condition section) { 
	3 Circulating body part;
	4 Iterative part;        // Be careful not to forget the declaration 4 iteration section. Otherwise, the cycle will not end and become an dead cycle.
}

Loop statement: do while

/* 
   Syntax format:
   do-while Loop executes the loop body at least once.
   
   Execution process: 1 - > 3 - > 4 - > 2 - > 3 - > 4 - > 2-3-4 - >... - > 2 (false, exit)
*/
1 Initialization part;
do {
	4 Iterative part 
} while(2 Cycle condition section);

Nested loop

A nested loop is formed when one loop is placed in another loop. For, while, do... While can be used as an outer loop or an inner loop.

Keyword break

The break statement is used to terminate the execution of a statement block
When a break statement appears in a multi-layer nested statement block, a label can indicate which layer of statement block to terminate.

label1: for(int i = 0; i < 5; i++) {
label2:		for(int j = 0; j < 5; j++) {
			    if(i == 2 && j== 3) {
			    	break label1; // Terminate loop label1 (end of nested loop)
			    	// break;      //  If no label is specified, terminate the current loop label2; label1 continues the cycle (starting from i=3)
			    }
		   }
}

Keyword continue

continue can only be used in a loop structure
The continue statement is used to skip one execution of its circular statement block and continue the next cycle
When a continue statement appears in the body of a multi-level nested loop statement, a label can indicate which level of loop to skip

label1: for(int i = 0; i < 5; i++) {
label2:		for(int j = 0; j < 5; j++) {
			    if(i == 2 && j== 3) {
			    	continue label1; // The cycle of i=2 ends and continues the cycle of I = 3 and j = 0
			    	// continue;      //  Continue the cycle from I = 2 and j = 4
			    }
		   }
}

Keyword return

It is not specifically used to end a loop. Its function is to end a method. When a method executes a return statement, the method will be terminated.
Unlike break and continue, return directly ends the entire method, no matter how many layers of loops the return is in.

Comparison between break and continue

break can only be used in switch statements and loop statements.
continue can only be used in loop statements.
The functions of the two are similar, but continue is to terminate this cycle, and break is to terminate this layer of cycle.
There can be no other statements after break and continue, because the program will never execute the subsequent statements.
The label statement must immediately follow the head of the loop. Label statements cannot be used before acyclic statements.
Many languages have goto statements. Goto statements can transfer control to any statement in the program at will, and then execute it. But it makes the program error prone. break and continue in Java are different from goto.

Topics: Java for while