[JavaSE Column 12] Java's switch conditional statement

Author's homepage : Designer Xiaozheng
Author's brief introduction : A Java full-stack software engineer from Ningbo, Zhejiang. Certified lecturer of CSDN Academy and Blue Bridge Cloud Class, high-quality creator in the full-stack field. Love technology, focus on business, open cooperation, willing to share, look forward to growing up with you and me!
Main direction : Vue, SpringBoot, WeChat applet

The switch statement is a type of Java selection structure . This article will explain the switch statement in Java.

insert image description here


1. What is a switch conditional statement

In Java, the switch statement is a control flow statement used for multi-branch conditional judgment . It allows different blocks of code to be executed among a series of candidate cases depending on the value of an expression.

A switch statement consists of keywords switch, an expression , and a set caseof statements.

First, the value of the expression is evaluated and compared to the constant value in each case statement .

If a matching casestatement is found, the corresponding code block will be executed, and then the switch statement will be jumped out of by the break statement .

If no matching case statement is found, the default code block can be executed using the default statement .

The following is an example of a simple Java switchstatement:

int dayOfWeek = 3;
String day;

switch (dayOfWeek) {
    
    
    case 1:
        day = "星期一";
        break;
    case 2:
        day = "星期二";
        break;
    case 3:
        day = "星期三";
        break;
    case 4:
        day = "星期四";
        break;
    case 5:
        day = "星期五";
        break;
    default:
        day = "周末";
        break;
}
System.out.println("今天是" + day);

In the above example, according to dayOfWeekthe value of , choose to execute the corresponding casestatement.

In this case, dayOfWeekthe value of 3 33 , so the thirdcasestatement is executed, and day is assigned as "Wednesday".

Finally, the output is " Today is Wednesday ".

The switch statement provides a concise and efficient way to handle multiple options and act accordingly based on the value of an expression . However, it should be noted that casethe break statement is used in each statement to ensure that the program jumps out of switchthe statement after executing the current branch and avoids executing other unnecessary branches.

insert image description here


2. The syntax and usage scenarios of the switch statement

The switch statement in Java is used to execute different code blocks depending on the value of an expression .

Its basic syntax is as follows:

switch (表达式) {
    
    
    case1:
        // 执行代码块1
        break;
    case2:
        // 执行代码块2
        break;
    // 可以有更多的 case
    default:
        // 如果没有匹配的值,则执行默认代码块
}

In the switch statement, the value of the expression will be compared with the value of each case in turn, and if it matches, the corresponding code block will be executed. Use the keyword break to end each case's code block to prevent subsequent cases from being executed as well.

The usage scenario of the switch statement is usually when we need to perform different operations according to different values ​​of a variable, we can avoid using a long string of if-else if-else structures to make the code more concise and readable.

It should be noted that the switch statement can only be used to judge values ​​of integer, character or enumeration types, and cannot be used to judge values ​​of floating point, string or other types. At the same time, the value in the case must be a constant, not a variable or an expression.

insert image description here


3. What is the difference between a switch statement and an if statement

switchStatement and statement in Java ifhave some differences in function and usage.

  • Conditional judgment : The if statement can execute the corresponding code block according to the value of a Boolean expression, while the switch statement can perform multiple selection branches according to the value of an expression.

  • Expression type : The condition of the if statement can be any expression that can return a Boolean value, such as relational operators, logical operators, etc. The expression of the switch statement is usually an integer type (byte, short, int), character type (char) or enumeration type (enum). Since Java 7, the string type is also supported.

  • Branching logic : The if statement implements multiple conditional branches by using multiple if-else if-else structures. The switch statement uses the case keyword to define different options, and each option needs to end with a break statement to avoid executing other options.

  • Readability : In some cases, if there are many conditions to be judged, using a switch statement can make the code more concise and easy to read. For complex conditional judgments or situations where range judgments are required, the if statement is more flexible.

In general, the if statement is suitable for judging multiple different conditions, while the switch statement is suitable for judging multiple options of a single expression . Developers need to choose which statement structure to use based on specific scenarios and requirements.

Four. Summary

This article briefly introduces the switch conditional statement in Java, explains the syntax, demonstrates the sample code, and proposes the difference and connection with the if statement. In the next blog, I will explain the syntax of the for loop statement and give a suitable application scenario.

insert image description here

Guess you like

Origin blog.csdn.net/qq_41464123/article/details/131443726