Home > Java > javaTutorial > body text

switch case judgment variable

WBOY
Release: 2024-02-19 08:04:05
Original
1052 people have browsed it

switch case判断变量

switch case determines variables and requires specific code examples

In programming, we often need to perform different operations based on different variable values. The switch case statement is a convenient structure that allows you to select different blocks of code for execution based on the value of a variable.

The following is a specific code example that shows how to use the switch case statement to determine the different values ​​of the variable:

#include <stdio.h>

int main() {
    int day;

    printf("请输入一个整数(表示星期几):");
    scanf("%d", &day);

    switch (day) {
        case 1:
            printf("今天是星期一
");
            break;
        case 2:
            printf("今天是星期二
");
            break;
        case 3:
            printf("今天是星期三
");
            break;
        case 4:
            printf("今天是星期四
");
            break;
        case 5:
            printf("今天是星期五
");
            break;
        case 6:
            printf("今天是星期六
");
            break;
        case 7:
            printf("今天是星期天
");
            break;
        default:
            printf("输入有误,请输入1-7的整数
");
            break;
    }

    return 0;
}
Copy after login

In the above code, an integer variable is first defined day, and then obtain an integer value from the user input through the scanf function. Then use the switch case statement to determine what day of the week today is based on the value of day, and output the corresponding information accordingly.

If the value of day is 1, then the switch case statement will execute the code block after the first case and output "Today is Monday"; if day The value is 2, then the code block following the second case is executed and "Today is Tuesday" is output; and so on.

If the value of day is not between 1 and 7, the code block after default will be executed and "Input error, please enter an integer from 1 to 7" will be output.

Through this example, we can see the structure of the switch case statement: first use the switch keyword to indicate the start of judging the variable, and then use the case keyword to match the specific value. If the match is successful, the corresponding execution code block; if no match is successful, the code block after default is executed. The break keyword needs to be added to the code block after each case to indicate the end of the current branch and jump out of the switch case statement.

In short, the switch case statement is a very commonly used structure, which can perform different operations according to different values ​​of variables, which is very convenient. In the actual programming process, we can flexibly use switch case statements to simplify code writing according to specific needs.

The above is the detailed content of switch case judgment variable. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!