当前位置:网站首页>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 ~

原网站

版权声明
本文为[Fried tomatoes 110]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020519106041.html