Home Java javaTutorial switch case judgment variable

switch case judgment variable

Feb 19, 2024 am 08:04 AM
variable switch case

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can Elden's Ring be played on switch? Can Elden's Ring be played on switch? Mar 11, 2024 am 11:31 AM

Can Elden's Ring be played on the switch? As a very charming action RPG game, many friends may not know whether it can be played smoothly on the switch platform. The answer is that it cannot be played at the moment. accomplish. Can Ring of Elden be played on switch? Answer: It cannot be played on switch. This highly anticipated Souls series role-playing action game has been officially released. Players can purchase it on PC, PS4/5 and Xbox Series eX|S/XboxOne and experience it immediately. Many friends who own a switch may still be eager to enjoy this game on the NS, but unfortunately, there is no switch version of the game. According to the official website configuration requirements, the game configuration is relatively high, and sw

What are instance variables in Java What are instance variables in Java Feb 19, 2024 pm 07:55 PM

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Is Switch2 compatible with Switch cartridges? Is Switch2 compatible with Switch cartridges? Jan 28, 2024 am 09:06 AM

Switch2 is a new model announced by Nintendo at Gamescom 2023. Some players are worried about whether there will be compatibility issues between the new model and the cartridges of previous versions. Let’s take a look. Is switch2 compatible with switch cassette? Answer: switch2 is not compatible with switch cassette. Introduction of Switch 2 cartridges According to information from Nintendo’s production chain company, Switch 2 may use 64GB cartridges. Because it has better performance and supports more 3A game masterpieces, it requires a larger cartridge capacity. Because many game works need to be castrated and compressed before they can be stuffed into a game cartridge. Moreover, Switch cartridges are prone to copying game content, so replace them with new cartridges.

Deep understanding of const in C language Deep understanding of const in C language Feb 18, 2024 pm 12:56 PM

Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

Nintendo's revenue increased by 21.2% in the first half of the year, and Switch sales reached 132 million units Nintendo's revenue increased by 21.2% in the first half of the year, and Switch sales reached 132 million units Nov 07, 2023 pm 06:29 PM

According to news from this site on November 7, Nintendo today announced its second fiscal quarter report as of September 30, 2023. Sales in the first half of the fiscal year were 796.2 billion yen (note from this site: currently about 38.695 billion yuan), year-on-year An increase of 21.2%; operating profit was 279.9 billion yen (currently approximately 13.603 billion yuan), a year-on-year increase of 27.0%. As of the end of September, cumulative sales of Switch were 132.46 million units, and cumulative software sales were 1.13323 million copies. In the first half of the fiscal year, sales of the entire Switch series increased by 2.4% year-on-year to 6.84 million units. Among them, Switch sales were 1.25 million units, Switch OLED version was 4.69 million units, and Switch Lite sales were 900,000 units. software

jQuery usage practice: several ways to determine whether a variable is empty jQuery usage practice: several ways to determine whether a variable is empty Feb 27, 2024 pm 04:12 PM

jQuery is a JavaScript library widely used in web development. It provides many simple and convenient methods to operate web page elements and handle events. In actual development, we often encounter situations where we need to determine whether a variable is empty. This article will introduce several common methods of using jQuery to determine whether a variable is empty, and attach specific code examples. Method 1: Use the if statement to determine varstr="";if(str){co

See all articles