Java process control: select structure

Time:2020-11-20

1、 Select structure

  • The selection structure is used to judge the given conditions and control the program flow according to the judgment results.
  • The syntax of choice structure in Java is mainly divided into‘if… Else’ statementand‘switch… Case ‘statement
  • In Java, select structure statements can be nested at will in syntax, but it is not recommended to nest too deep, which will reduce the readability of the code.

2、 ‘if… Else’ statement

Description:The word ‘if’ meansIfThe word ‘else’ meansotherwise。 Java uses’ if… Else ‘related statementsConditional judgment

Classification:

  1. ‘if’ single choice structure
  2. ‘if’ double selection structure
  3. ‘if’ multiple selection structure

I. single selection structure of ‘if’

Grammar:

* / (Boolean expression){
	/*If the Boolean expression of true is executed*/
}

Description:

  • An ‘if’ statement contains a Boolean expression and one or more statements.
  • If the value of the Boolean expression is’ true ‘, the code block in the’ if ‘statement is executed, otherwise the code after the’ if ‘statement block is executed.

Example:

/*The result is the console output: This is the if statement*/
public static void main(String args[]){
    int x = 10;
    if(x < 20){
    	System.out.print ("this is an IF statement");
    }
}


II. Double selection structure of ‘if’

Grammar:

* / (Boolean expression){
	/*The statement that will be executed if the value of the Boolean expression is true*/
}else{
	/*The statement that will be executed if the value of the Boolean expression is false*/
}

Description:

  • The ‘if’ statement can be followed by a ‘else’ statement. When the Boolean expression value of the ‘if’ statement is’ false ‘, the’ else ‘statement block will be executed.

Example:

/*The run result is the console output: This is the else statement*/
public static void main(String args[]){
    int x = 30;
    if(x < 20){
    	System.out.print ("this is an IF statement");
    }else{
        System.out.print ("this is an else statement");
    }
}


III. The ‘if’ multiple selection structure

Grammar:

If (/ * Boolean expression 1 * /){
	/*If the value of Boolean expression 1 is true, the statement that will be executed*/
}Else if (/ * Boolean expression 2 * /){
	/*If the value of Boolean expression 1 is false and the value of Boolean expression 2 is true, the statement that will be executed*/
}Else if (/ * Boolean expression 3 * /){
	/*If the value of Boolean expression 1 and Boolean expression 2 are both false, and the value of Boolean expression 3 is true, the statement that will be executed*/
}else{
	/*If the value of Boolean expression 1, Boolean expression 2, and Boolean expression 3 is false, the statement that will be executed*/
}

Description:

  • ‘if’ statement can be followed by ‘else if Else ‘statement, and the’ else if ‘statement can be reused, which can detect a variety of possible situations.

be careful:

  • The ‘if’ statement has at most one ‘else’ statement, and the ‘else’ statement follows all ‘else if’ statements.
  • The ‘if’ statement can have several ‘else if’ statements, which must precede the ‘else’ statement.
  • onceOne of the ‘else if’ or ‘if’ statements detected as trueOther ‘else if’ and ‘else’ statements willSkip execution

Example:

/*The result is the console output: This is the second if else statement*/
public static void main(String args[]){
    int x = 30;
    if(x < 20){
    	System.out.print ("this is an IF statement");
    }else if(x < 30){
        System.out.print ("this is an if else statement");
    }else if(x < 40){
        System.out.print ("this is the second if else statement");
    }else{
        System.out.print ("this is an else statement");
    }
}


3、 ‘switch ‘multi selection structure

Grammar:

switch(variable){
	case value1:
		/*If the value of the variable is the same as value1, the statement that will be executed*/
		The break; / * break statement is optional*/
	case value2:
		/*If the value of the variable is the same as Value2, the statement that will be executed*/
		break;
	case value3:
		/*If the value of the variable is the same as value3, the statement that will be executed*/
		break;
	Default: / * the default statement is optional*/
		/*If the value of the variable is different from value1, Value2, value3, the statement that will be executed*/
}

Description:

  • ‘switch… Case ‘statementDetermine whether a variable is equal to a value in a series of valuesEach value is called a branch.

be careful:

  • The variable type in the ‘switch… Case’ statement can bebyte、short、int、char。 Starting with javase 7, the ‘switch… Case’ statement is supportedStringThe case tag must be a string constant or literal.
  • The ‘switch’ statement can have more than one ‘case’ statement. Each ‘case’ is followed by a value to compare and a colon. The value in the ‘case’ statement must have the same data type as the variable, and can only beConstant or literal constant
  • When the value of the variable is equal to the value of the ‘case’ statement, the statement after the ‘case’ statement is executed,Until the ‘break’ statement appearsThe code under the ‘switch’ code block will be executed.
  • The ‘case’ statement does not have to contain a ‘break’ statement.If no ‘break’ statement appears, the program continues to execute the next ‘case’ statementUntil the ‘break’ statement appears.
  • The ‘case’ statement can be reused multiple times.
  • The ‘switch’ statement can contain a‘default ‘statementBranch, which is usually the branch of the ‘switch’ statementThe last branch(can be in any location, but recommended at the last one).When the value of ‘case’ is not equal to the value of ‘default’ statement. The ‘default’ branch does not require a ‘break’ statement
  • When the ‘switch… Case’ statement is executed, it must be matched first. If the match is successful, the statement in the branch will be executed. Then, according to whether there is a ‘break’, whether to continue to execute the statements in other branches below or jump out of the whole ‘switch… Case’ statement is judged.

Example:

/*The result is that the console outputs in turn: 1 2 3*/
public static void main(String args[]){
    int i = 1;
    switch(i){
        case 0:
            System.out.println("0");
        case 1:
            System.out.println("1");
        case 2:
            System.out.println("2");
        case 3:
            System.out.println("3");
            break;
        default:
            System.out.println("default");
    }
}


Recommended Today

A field in MS-SQL server batch modification table can be null

–Dangerous operation, remember to backup database 1 declare @ SQL varchar (500) before processing ,@tbname varchar(100) 2 begin 3 4 — create cursor 5   declare cursor_item cursor fast_forward for select [name] from sysobjects where xtype=’U’ AND id in(select id from syscolumns where name=’myColumnName’ and colstat=0 ) 6   open cursor_ Item; — Open cursor 7 while […]