This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

Java conditional statement – if… else

An if statement contains a Boolean expression and one or more statements.

grammar

The syntax for the if statement is as follows:

If (Boolean expression) {// The statement to execute if Boolean expression is true}Copy the code

If the value of the Boolean expression is true, the code block in the if statement is executed, otherwise the code following the if statement block is executed.

if… Else statements

If statements can be followed by else statements. If the Boolean expression value of the if statement is false, the else block is executed.

grammar

The if… The use of else is as follows:

If (Boolean expression){// if Boolean expression value is true}else{// if Boolean expression value is false}Copy the code

if… else if… Else statements

If statement can be followed by else if… Else statement, which can detect a variety of possible cases.

When using if, else if, and else statements, note the following:

  • An if statement can have at most one else statement, which follows all else if statements.
  • The if statement can have several else if statements, which must precede the else statement.
  • Once one of the else if statements checks true, the other else if and else statements are skipped.

grammar

if… Else syntax format is as follows:

If (Boolean expression 1){// execute code if Boolean expression 1 is true}else if(Boolean expression 2){// Execute code if Boolean expression 2 is true}else if(Boolean expression 3){// Execute code if Boolean expression 3 is true}else if(Boolean expression 3){// Execute code if Boolean expression 3 is true }else {// execute code if none of the above Boolean expressions are true}Copy the code

Nested if… Else statements

Using nested if… The else statement is legal. That means you can use an if or else if statement inside of another if or else if statement.

grammar

Nested if… Else syntax format is as follows:

If (Boolean expression 1){//// execute code if(Boolean expression 2){//// execute code if Boolean expression 2 is true}}Copy the code

You can nest else if… The else.

Java Switch Case statement

The switch case statement determines whether a variable is equal to one of a series of values, each of which is called a branch.

grammar

The syntax of the Switch Case statement is as follows:

Switch (expression){case value: // statement break; // Optional case value: // statement break; // Optional // You can have as many case statements default: // Optional // statements}

The switch case statement has the following rules:

  • The variable type in a switch statement can be byte, short, int, or char. Starting with Java SE 7, the Switch supports strings, and case labels must be String constants or literals.
  • A switch statement can have multiple case statements. Each case is followed by a value to compare and a colon.
  • The data type of the value in a case statement must be the same as the data type of the variable, and can only be constant or literal.
  • When the value of the variable is equal to the value of the case statement, the statement after the case statement is executed, and the switch statement is not jumped until the break statement appears.
  • The switch statement terminates when a break statement is encountered. The program jumps to the statement following the switch statement. Case statements do not need to contain break statements. If no break statement occurs, the program continues with the next case statement until a break statement occurs.
  • The switch statement can contain a default branch, which is usually the last branch of the switch statement (it can be anywhere, but it is recommended to be the last branch). Default is executed when no case statement has the same value as the variable. The default branch does not require a break statement.

When executing the switch case, it will match the case first. If the match is successful, the value of the current case will be returned. Then, it will judge whether to continue the output or jump out of the judgment according to whether there is a break.

/** * Java conditional statement */
public class learn_8 {
    public static void main(String[] args) {
        //if
        int a = 10;
        int b = 20;
        if (a > b) {
            System.out.println("a>b1");
        }
 
        if (a > b) {
            System.out.println("a>b2");
        }else{
            System.out.println("a<b2");
        }
 
        int i = 5;
        switch(i){
            case 0:
                System.out.println("0");
            case 1:
                System.out.println("1");
            case 2:
                System.out.println("2");
            default:
                System.out.println("default"); }}}Copy the code