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.

oneswitchThe 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, andcaseThe following constant expression values are compared. If they are equal, the corresponding part of the statement block is executedbreakStatement outswitchBranch statement. If the value of the expression is related to allcaseDo not match any of the following constant expressionsdefaultThe statement corresponding to the itemn, and then jump outswitchBranch statement.
  • 2.caseThe following constant expression can only be an integer, a character, or an enumeration constant; eachcaseStatement 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. EachcaseStatements are not the end point of program execution and usually need to be executedbreakStatement to jump outswitchBranch statement; If acaseAfter a block of a statement has been executed, if it is not followedbreakStatement, the others are executed in ordercaseStatement until encounteredbreakStatement or all that followcaseAfter all statements are executed, jump out againswitchBranch statement.
  • More than 5.caseYou can share a set of execution blocks.
  • 6. EachcasedefaultThe order of occurrence does not affect the execution result.
  • 7. defaultStatement is not required, but is recommended as a default processing item.
  • 8.switchstatementsWe only do the equality test, not the imageifStatement to do relational expression or logical expression evaluation, to determine the logical true or false.

twoswitchStatement 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

  1. Install Visual Studio
  2. Install the Visual Studio plug-in, Visual Assist
  3. Visual Studio 2008 uninstall
  4. Visual Studio 2003/2015 uninstall
  5. Set Visual Studio font/background/line number
  6. C format control/placeholder
  7. C logical operators
  8. C ternary operator
  9. C language comma expression
  10. C ++ I/I ++
  11. C for loop
  12. C language break and continue
  13. C while loop
  14. C do while and while loops
  15. C switch statement

C language switch statements

This article is posted by the blog – Ape says Programming Ape says Programming!