当前位置:网站首页>Switch statement
Switch statement
2022-06-11 07:43:00 【Fried tomatoes 110】
switch Statement can implement multi branch statements , Its general form is as follows :
switch( Integer expression )
{
case Integer constant expression 1: sentence 1;break;
case Integer constant expression 2: sentence 2;break;
......
case Integer constant expression n: sentence n;break;
default: sentence n+1;break;
}
switch The statement has two points to note , One is switch What is in the brackets must be Integer expression , Two is case The latter must also be Integral constant expression . Another reminder ,case And integer constant expression , Don't connect what you write , What I wrote at the beginning was connected , Then the compilation fails ~
Switch Where is the appropriate place to use a sentence ?
Actually switch Statement represents multi branch selection , For example, when we hope to have different results under many different conditions , It can be used in general switch Statement to implement . For example, when we enter a number , When you want it to output the corresponding statement , You can use it switch La . Don't talk much , Let's go straight to the code ~
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
int a;
scanf("%d",&a);
switch (a)
{
case 1:
printf(" week 1\n");
case 2:
printf(" week 2\n");
case 3:
printf(" week 3\n");
case 4:
printf(" week 4\n");
case 5:
printf(" week 5\n");
case 6:
printf(" week 6\n");
case 7:
printf(" week 7\n");
}
return 0;
}We just want to input this code 1~7 A certain number of species , Can output the complete day of the week .
When the input “5” This number is , The operation results are as follows :
week 5
week 6
week 7
Is the result different from what we want ? Why? , So , When we type in “5” when , It will automatically match with case 5 The corresponding statement is then executed . But once it's done , No more matching , Put all the following directly case All corresponding execution . But sometimes we just want it to match one and jump out switch sentence , There is no need to output all the following , Now , We just need to be in each case At the end of the corresponding statement Add one more “break;” That's it . Such as :
case 1:
printf(" week 1\n");
break;
case 2:
printf(" week 2\n");
break;
case 3:
printf(" week 3\n");
break;
case 4:
printf(" week 4\n");
break;
case 5:
printf(" week 5\n");
break;
case 6:
printf(" week 6\n");
break;
case 7:
printf(" week 7\n");
break;
Now , When you type 5 after , The only output is “ week 5” 了 .break The function here is to jump out switch sentence .
switch Statements can also be used in this way , Let's look at the code :
switch (a)
{
case 1:
case 2:
case 3:
case 4:
printf(" week 1\n");
break;
case 5:
case 6:
printf(" week 6\n");
break;
case 7:
printf(" week 7\n");
break;
}
It's also correct to write like this , Is when you enter 1~4 when , Will output week 1, Input 5 or 6 When, the week will be output 6, Input 7 Then the output is week 7. This makes the code look simpler , The specific method should be based on your own needs ~
in addition ,swutch Nesting is also allowed , Please have a look at :
#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
scanf("%d", &b);
scanf("%d", &a);
switch (a)
{
case 1:
a++;
printf(" week 1\n");
case 2:
switch (b)
{
case 1:
printf(" January \n");
break;
case 2:
printf(" February \n");
break;
case 3:
printf(" January \n");
break;
}
break;
case 3:
printf(" week 3\n");
break;
case 4:
printf(" week 4\n");
break;
}
return 0;
}When the input “2\n1” when , Running results :
week 1
February
Through this example, it is not difficult to find break stay switch Nested inside just ends where it is switch Sentences ~
边栏推荐
- 二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...
- The solution of "no startup device" after running Bochs
- C language judging big end and small end [consortium or pointer] big end and small end conversion
- Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]
- C language - Growth Diary -02- function
- Sort - Swap sort
- What exactly is PMP?
- C language - Growth Diary -01- count primes and sum
- 2. Graduated from this course, and the bank has outsourced testing work for more than 4 months. Talk about some real feelings
- Qunhui ds918 creates m.2 SSD read / write cache
猜你喜欢

Aiop introduction

TiDB Cloud 上线 Google Cloud Marketplace,以全新一栈式实时 HTAP 数据库赋能全球开发者

How to prepare for the new PMP syllabus exam?

Miscellany C language

【Oracle 数据库】奶妈式教程day03 排序查询

The solution of "no startup device" after running Bochs

2022 low voltage electrician test questions and online simulation test

QT table display data

C wechat upload form data

测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...
随机推荐
[atcoder2306] rearranging (topology)
[Oracle database] mammy tutorial day03 Sorting Query
Arduino_ Esp32 development record
2021-11-05 definition of cache
Euler's theorem and its extension (with proof)
【AtCoder1983】BBQ Hard (组合数+巧妙模型转化)
2022 low voltage electrician test questions and online simulation test
Import on CSDN MD file
The maximum number of divisors of numbers in the int range is 1536
【AtCoder2307】Tree Game(博弈)
[atcoder1983] BBQ hard (combination number + clever model transformation)
[atcoder2307] tree game
20200727 T2 small w playing game [generating function (binomial inversion technique)]
[untitled] Weng_ C lesson 1
Sdl-4 PCM playback
【AtCoder2304】Cleaning
Modular linear equations (Chinese remainder theorem + general solution)
【AtCoder2306】Rearranging(拓扑)
Configuration software -- control drag and drop
MFC custom string linked list