This is the 8th day of my participation in the August More Text Challenge. For details, see:August is more challenging
directory
- Introduction to switch statements
- Switch statement combat
- Three. Guess you like it
Zero basic C/C++ learning route recommendation: C/C++ learning directory >> C language basic introduction
In C, the switch statement can be used as a conditional branch, similar to the if/else statement. If there are fewer branches, the if/else statement is recommended. The switch statement is recommended when there are many branch decisions. For details, see below.
oneswitch
The statement profile
The switch syntax is as follows:
Switch (expression) {case constant expression 1: block 1; break; Case constant expression 2: statement block 2; break; ... Case constant expression m: statement block m; break; Default: statement block n; break; }Copy the code
The instructions are as follows:
- 1. When the program is executed, the value of the expression is first evaluated, and
case
The following constant expression values are compared. If they are equal, the corresponding part of the statement block is executedbreak
Statement outswitch
Branch statement. If the value of the expression is related to allcase
Do not match any of the following constant expressionsdefault
The statement corresponding to the itemn
, and then jump outswitch
Branch statement. - 2.
case
The following constant expression can only be an integer, a character, or an enumeration constant; eachcase
Statement expressions have different values and serve as a label to guide the program to the entry. - 3. The statement block here can be a single statement or other compound statements. Statement blocks can be made without curly braces
{}
- 4. Each
case
Statements are not the end point of program execution and usually need to be executedbreak
Statement to jump outswitch
Branch statement; If acase
After a block of a statement has been executed, if it is not followedbreak
Statement, the others are executed in ordercase
Statement until encounteredbreak
Statement or all that followcase
After all statements are executed, jump out againswitch
Branch statement. - More than 5.
case
You can share a set of execution blocks. - 6. Each
case
和default
The order of occurrence does not affect the execution result. - 7.
default
Statement is not required, but is recommended as a default processing item. - 8.
switch
statementsWe only do the equality test, not the imageif
Statement to do relational expression or logical expression evaluation, to determine the logical true or false.
twoswitch
Statement of actual combat
For example: What day is it today according to the numbers?
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - switch statement // @time :2021/05/31 08:00 08:00 //@Motto: little step without a thousand miles, do not accumulate small flow cannot become a river sea, program life wonderful need to keep on accumulating! /******************************************************************************************/ #include "stdafx.h" #include "stdio.h" #include "windows.h" int _tmain(int argc, _TCHAR* argv[]) { int i = 1; Switch (I) {case 1: printf(" Monday "); break; Case 2: printf(" Tuesday "); break; Case 3: printf(" Wednesday "); break; Case 4: printf(" Thursday "); break; Case 5: Printf (" Friday "); break; Case 6: printf(" Saturday "); break; Case 7: printf(" 7 "); break; Default: printf(" I mistyped "); break; } system("pause"); return 0; } /* Output: Monday */Copy the code
Of course, you can write 8 if/else’s to do this, but it’s better to use the switch for multiple branches;
3.Guess you like
- Install Visual Studio
- Install the Visual Studio plug-in, Visual Assist
- Visual Studio 2008 uninstall
- Visual Studio 2003/2015 uninstall
- Set Visual Studio font/background/line number
- C format control/placeholder
- C logical operators
- C ternary operator
- C language comma expression
- C ++ I/I ++
- C for loop
- C language break and continue
- C while loop
- C do while and while loops
- C switch statement
C language switch statements
This article is posted by the blog – Ape says Programming Ape says Programming!