In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to understand the switch statement of multi-way switch mode, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.
In Example 10, after removing the break statement, all statements that meet the test condition are output. Using this feature, you can design a switch statement for multi-way switch mode, for example: in a year of 12 months, January, March, May, July, August, December is 31 days, April, June, September, November is 30 days, February is 28 days, if you enter any month on the console, you can know how many days there are in this month. In this case, you can use the multi-way switch mode of the switch statement, which has the following form:
switch(expression){ case 1: statement 1 break; case 2: case 3: statement 2 break; … default: default statement break;}
As you can see from the form, if you don't use the break statement after case 2, the effect of the compliance test is the same as that of the compliance test in case 3. That is to say, using the multiplex switch mode, multiple test conditions can be output in one sentence block.
Example 11: Which season does a month belong to?
In this example, you need to use the switch statement to determine which season a month entered on the console belongs to. It is known that March, April and May are spring, June, July and August are summer, September, October and November are autumn, and December, January and February are winter. The specific codes are as follows:
01 #include02 int main()03 {04 int month; /* Define variable Month */05 printf("please enter a month:\n");06 scanf("%d",&month); /* Enter Month */07 switch(month) /* Season by month */08 {09 /* Multiway switch mode */10 case 3:11 case 4: 12 case 5:13 printf("%d is spring\n",month); /* March, April and May are spring */14 break;15 /* Multiway switch mode */16 case 6:17 case 7: 18 case 8:19 printf("%d is summer\n",month); /* June, July and August are summer */20 break;21 /* Multiway switch mode */22 case 9:23 case 10: 24 case 11:25 printf("%d is autumn\n",month); /* September, October and November are autumn */26 break;27 /* Multiway switch mode */28 case 12:29 case 1: 30 case 2:31 printf("%d is winter\n",month); /* December, January and February are winter */32 break;33 default:34 printf("error!!!\ n"); /* No such month */35 }36 return 0; /* End of procedure */37 }
Run the program and display the results as shown in Figure 6.21.
Fig. 6.21 Seasonal operation diagram
From the example code and running results, you can see:
The program uses multi-way switch mode, so that when the value of month is 3, 4, and 5, the same operation will be executed, and the default statement will be used to display the number that does not match, and the prompt message indicates that the input error.
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.