What is the usage of switch in C language?
The usage of switch in C language is: 1. The [expression] in brackets after switch, the ANSI standard allows it to be of any type; 2. When the value of the expression matches the constant expression after a case When the values are equal, the statement following this case is executed; otherwise, the statement following default is executed.
The usage of switch in C language is:
Function: The switch statement is a multi-branch selection statement. Used Implement a multi-branch selection structure. The if statement has only two branches to choose from, but multi-branch selection is often used in practical problems. For example, student performance classification (90 is "A", etc., 80-89 is divided into 'B' etc., 70-90 is divided into 'C', etc...). Of course, these can be handled with nested if statements, but if there are many branches, there will be many layers of nested if statements, and the program The redundancy is long and the readability is reduced. The C language provides the switch statement to directly handle multi-branch selections, which is equivalent to the CASE statement in the PASCAL language.
Form: switch (expression)
{ case 常量表达式 1:语句 1 case 常量表达式 2:语句 2 . . . case 常量表达式 n:语句 n default:语句 n+1 }
For example, if you want to print out the hundred-point score segment according to the test score level, you can use the switch statement:
switch(grade) { case 'A':printf("85-100\n"); case 'B':printf("70-84\n"); case 'C':printf("60-69\n"); case 'D':printf("<60\n"); default:printf("error\n"); }
Description:
(1) The "expression" in the brackets after the switch, ANSI The standard allows it to be of any type.
(2) When the value of the expression is equal to the value of the constant expression following a case, the statement following the case will be executed. If the constants in all cases If the value of the expression does not match the expression, the statement after default will be executed.
(3) The value of the constant expression in each case must be different from each other, otherwise there will be conflicts. Phenomenon (there are two or more execution plans for the same value of an expression).
(4) The order in which defaults appear in each case does not affect the execution results. For example, "default: ..." can appear first, then "case 'D': ...", and then "case 'A': ...".
(5) After execution After the statement following a case, the flow control is transferred to the next case to continue execution. The "case constant expression" only serves as a statement label, and does not perform conditional judgment there. When executing the switch statement, according to the expression following the switch If the matching entry label is found, the execution will start from this label without further judgment. For example, in the above example, if the value of grade is equal to 'A', it will be continuously output:
85-100 70-84 60-69 <60 error
Therefore, it should After executing a case branch, make the process jump out of the switch structure, that is, terminate the execution of the switch statement.
You can use a break statement to achieve this purpose. Rewrite the above switch structure as follows:
switch(grade) { case 'A':printf("85-100\n"); break; case 'B':printf("70-84\n"); break; case 'C':printf("60-69\n"); break; case 'D':printf("<60\n"); break; default:printf("error\n"); }
The last branch (default) does not need to add a break statement. If the value of grade is 'B', only "70-84" will be output.
Although there is more than one execution statement behind the case, it can There is no need to enclose it in curly brackets, all execution statements following this case will be automatically executed sequentially. Of course, curly brackets can also be added.
(6) Multiple cases can share a set of execution statements, for example:
case 'A': case 'B': case 'C': printf(">60\n");break; . .
The same set of statements is executed when the value of grade is 'A', 'B' or 'C'.
Related learning recommendations: C video tutorial
The above is the detailed content of What is the usage of switch in C language?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











VS Code and Visual Studio C++ IntelliSense may not be able to pick up libraries, especially when working on large projects. When we hover over #Include<wx/wx.h>, we see the error message "CannotOpen source file 'string.h'" (depends on "wx/wx.h") and sometimes, autocomplete Function is unresponsive. In this article we will see what you can do if VSCode and VSC++ IntelliSense are not working or extracting libraries. Why doesn't my Intellisense work in C++? When working with large files, IntelliSense sometimes

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

Are you unable to purchase or watch content on your Xbox due to error code 8C230002? Some users keep getting this error when trying to purchase or watch content on their console. Sorry, there's a problem with the Xbox service. Try again later. For help with this issue, visit www.xbox.com/errorhelp. Status Code: 8C230002 This error code is usually caused by temporary server or network problems. However, there may be other reasons, such as your account's privacy settings or parental controls, that may prevent you from purchasing or viewing specific content. Fix Xbox Error Code 8C230002 If you receive error code 8C when trying to watch or purchase content on your Xbox console

void in C is a special keyword used to represent an empty type, which means data without a specific type. In C language, void is usually used in the following three aspects. The function return type is void. In C language, functions can have different return types, such as int, float, char, etc. However, if the function does not return any value, the return type can be set to void. This means that after the function is executed, it does not return a specific value. For example: voidhelloWorld()

According to the TIOBE Programming Community Index, one of the benchmarks for measuring the popularity of programming languages, it is evaluated by collecting data from engineers, courses, vendors and search engines around the world. The TIOBE Index in January 2024 was released recently, and the official programming language rankings for 2023 were announced. C# won the TIOBE 2023 Programming Language of the Year. This is the first time that C# has won this honor in 23 years. TIOBE's official press release stated that C# has been in the top 10 for more than 20 years. Now it is catching up with the four major languages and has become the programming language with the largest growth in one year (+1.43%). It well-deserved to win this award. Ranked second are Scratch (+0.83%) and Fortran (+0

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

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.

PHP is a scripting language widely used in web development, while C language is a low-level programming language. In some specific scenarios, it may be necessary to convert PHP code into C language to improve program performance and operating efficiency. This article will delve into how PHP code is converted to C language and give specific code examples. Reasons for converting PHP code to C language First, let us take a look at why it is sometimes necessary to convert PHP code to C language? PHP is an interpreted language, and the execution code needs to be interpreted line by line during runtime.
