[summary of java foundation and core]

Java execution control flow

The control process in Java is actually the same as that in C. in Java, process control involves if else, while, do while, for, return, break and select statement switch. This is analyzed below.

Conditional statement

Conditional statements can execute different statements according to different conditions. Including if conditional statements and switch multi branch statements.

if conditional statement

if statement can judge the result of the expression separately and represent the execution result of the expression, for example:

int a = 10;
if(a > 10){
  return true;
}
return false;

if... else conditional statement

If statements can also be used in conjunction with else. Usually, if certain conditions are met, some processing will be performed, otherwise another processing will be performed.

int a = 10;
int b = 11;
if(a >= b){
  System.out.println("a >= b");
}else{
  System.out.println("a < b");
}

The expression in () after if must be boolean. If true, execute the compound statement after if; If false, the compound statement after else is executed.

If... else if multi branch statement

If... else in the above is the judgment of single branch and two branches. If there are multiple judgment conditions, you need to use if... else if.

int x = 40;
if(x > 60) {
  System.out.println("x The value of is greater than 60");
} else if (x > 30) {
  System.out.println("x The value of is greater than 30 but less than 60");
} else if (x > 0) {
  System.out.println("x The value of is greater than 0 but less than 30");
} else {
  System.out.println("x The value of is less than or equal to 0");
}

switch case multi branch statement

A more elegant way than * * if... else if * * statements is to use switch multi branch statements. Examples are as follows:

switch (week) {
  case 1:
    System.out.println("Monday");
    break;
  case 2:
    System.out.println("Tuesday");
    break;
  case 3:
    System.out.println("Wednesday");
    break;
  case 4:
    System.out.println("Thursday");
    break;
  case 5:
    System.out.println("Friday");
    break;
  case 6:
    System.out.println("Saturday");
    break;
  case 7:
    System.out.println("Sunday");
    break;
  default:
    System.out.println("No Else");
    break;
}

Circular statement

Circular statement is to repeatedly execute an expression under certain conditions until the requirements of circular statement are met. The loop statements used mainly include * * for, do... while(), while * *.

while loop statement

The loop mode of the while loop statement is to use a condition to control whether to continue to execute the statement repeatedly. The format of the while loop statement is as follows:

while(Boolean value){
  expression
}

It means that when (Boolean value) is true, the following expression is executed. When the Boolean value is false, the loop is ended. In fact, the Boolean value is also an expression, such as:

int a = 10;
while(a > 5){
  a--;
}

do... while loop

The only difference between while and do... While loops is that the do... While statement is executed at least once, even if the first expression is false. In the while loop, if the first condition is false, the statements in it will not be executed at all. In practical application, while is more widely used than do... While. Its general form is as follows:

int b = 10;
// do ··· while loop statement
do {
  System.out.println("b == " + b);
  b--;
} while(b == 1);

for loop statement

The for loop is a loop method we often use. This form will be initialized before the first iteration. Its form is as follows:

for(initialization; Boolean expression; Stepping){}

Boolean expressions are tested before each iteration. If the result obtained is false, the code after the for statement will be executed; At the end of each cycle, the next cycle is executed according to the step value.

comma operator

One that cannot be ignored here is the comma operator. The only comma operator used in Java is the for loop control statement. In the initialization part of the expression, you can use a series of comma separated statements; With the comma operator, you can define multiple variables within a for statement, but they must have the same type.

for(int i = 1,j = i + 10;i < 5;i++, j = j * 2){}

For each statement

Java JDK 1.5 also introduces a more concise and convenient method for traversing arrays and collections, that is, for each statement. Examples are as follows:

int array[] = {7, 8, 9};

for (int arr : array) {
     System.out.println(arr);
}

Jump statement

In the Java language, there are three jump statements: break, continue, and return.

break statement

We have seen the break statement in switch. It is used to terminate the loop. In fact, the break statement is used to forcibly exit the current loop in the for, while and do ··· while loop statements, for example:

for(int i = 0;i < 10;i++){
	if(i == 5){
    break;
  }
}

continue statement

continue can also be placed in a loop statement. It has the opposite effect to the break statement. It is used to execute the next loop instead of exiting the current loop. The above example is mainly used:

for(int i = 0;i < 10;i++){

  System.out.printl(" i = " + i );
	if(i == 5){
    System.out.printl("continue ... ");
    continue;
  }
}

return statement

The return statement can return from a method and give control to the statement that calls it.

public void getName() {
    return name;
}

Keywords: Java JavaEE

Added by geek_girl_2020 on Sun, 28 Nov 2021 09:39:56 +0200