Java basics: conditional statement if else and switch case

In the previous article, we introduced loop statements in Java control statements: for, while, and do-while.

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-P21trEZq-1691977178471)(https://img-wljslmz-1259086031.cos.ap-nanjing.myqcloud.com /picgo/202212091507373.png)]

This article introduces the conditional statement in the Java control statement: if elseand switch case.

if else

grammar:

if (condition1) {

    // 第一个条件被执行
}
else if (condition2) {

    // 第二个条件被执行
}

if else has 3 different variants:

  • if statement - you can simply use this to do something if some condition is true

  • else statement - use this statement with if, if is not true and finally the else block is executed.

  • else if statement - This can be done with an if condition, when the error code will come this statement checks another condition and if this condition is true, the code will be executed.

We introduce these three situations directly in the form of code:

package com.test.javaroads.condition;

/**
 * @author: javaroads
 * @date: 2022/12/10 21:02
 * @description:
 */
public class IfElse {

    public static void main(String[] args) {

        int a = 1;

        // 1、if 语句
        if (a == 1) {
            System.out.println("if 语句值为" + a);
        }

        // 2、else 语句
        if (a == 2) {
            System.out.println("if 语句值为" + a);
        } else {
            System.out.println("else 语句为" + a);
        }

        // 3、else if 语句
        if (a == 2) {
            System.out.println("if 语句值为" + a);
        } else if (a == 1) {
            System.out.println("else if 语句为" + a);
        } else {
            System.out.println("else 语句为" + a);
        }
    }
}

Results of the:

if 语句值为1
else 语句为1
else if 语句为1

switch case

A switch statement is a branching statement in which multiple conditions appear in the form of cases. The switch statement can handle various data types such as byte, short, int, long, String, etc.

More often than not, the Java Switch statement provides better options than the various options available with the Java if-else statement.

grammar:

switch (expression){
  case 1:
    // case 1语句
  break;
  case 2:
    // case 2语句
  break;
  case 3:
    // case 3语句
  break;
.
.
.
  case N:
    // case N语句
  break;
default;
  // 默认语句
} 

The operation of the switch statement is to first judge the (expression) written in the switch, and then execute the statement that matches the case value. If none of the case values ​​match, default executes the statement described in . break is placed after the case statement and is used to jump out of the entire switch statement after the case statement is executed.

default and break can be omitted. If there is no break, even if the case statement is executed, the processing of the entire switch statement will not exit, and the next case value will be judged. Write a break after each case statement, unless you intend to execute multiple case statements.

Let's demonstrate it in code form:

package com.test.javaroads.condition;

/**
 * @author: javaroads
 * @date: 2022/12/10 21:29
 * @description:
 */
public class SwitchCase {

    public static void main(String[] args) {
        int a = 30;
        switch (a) {
            case 10:
                System.out.println("匹配的数字是 10");
                break;
            case 20:
                System.out.println("匹配的数字是 20");
                break;
            case 30:
                System.out.println("匹配的数字是 30");
                break;
            default:
                System.out.println("没有匹配的数字");
        }
    }
}

Results of the:

匹配的数字是 30

When using switch case, we should keep the following rules in mind:

  • Duplicate case values ​​are not allowed in a switch statement.
  • The data types of the case and the variable in the switch should be the same.
  • To terminate a statement, the break keyword should be used, otherwise other conditions will be checked and executed.
  • The default label is optional in switch statements.

Summarize

This article explains conditional statements in Java control statements:

We also give a code example for each statement, and execute the code to give the result. You can understand it better by combining the code and the result. I hope this article is helpful to you!

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132269555