In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about the use of nested statements in the C language branch loop, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Hello,everyone! Forever introduced the branch structure and loop structure of C language before, and introduced branch statement nesting in the branch, but did not introduce switch nesting at that time, so this time mainly introduced switch nesting and loop nesting.
First, let's talk about what nesting is. A statement that contains another complete statement structure is called statement nesting. Other statement structures can continue to be nested within the nested statement structure, which constitutes multi-layer nesting. For example, nesting a complete loop statement in the loop body is called loop nesting; in the internal loop nesting, nesting a loop structure, that is, multi-layer loop. The meaning of nesting means "complete inclusion".
Switch nesting
Like if statements, switch statements can form a nested structure. If you nest another switch-case statement within switch-case, you will form a switch nesting structure; if you continue to nest inside, you will form multiple layers of nesting.
1. Grammatical structure:
The following is the nested structure of switch. Default can be used on a case-by-case basis, and when it is not used, it is equivalent to not dealing with it. Generally, when you use default, you can leave break behind it.
Switch (expression 1) {case constant expression 1: statement 1; switch (expression 2) {case constant expression A: statement a; break; case constant expression B: statement b; break;... Default: statement N;} break;case constant expression 2: statement 2; switch (expression n) {. } break;case constant expression 3: statement 3; break;... Default: statement n;} 2, switch-case nested statement program example
Example 1: a simple understanding of the nested structure of switch statements
* simple switch nesting program * # includeint main () {int a = 0, b = 0; scanf ("% d% d", & a, & b) / / enter two values switch (a) / / this external switch to judge a {case 1: / / axi1 enter the internal nested switch statement switch (b) / / here switch judges b {case 0:printf ("$$\ n"); break Case1: printf ("&\ n"); default:printf ("* *"); / / here is break;} break; / / ending the nested statement case 2:printf ("#\ n") inside the case1 Break; case 3:printf ("@ @\ n"); default:printf ("!!\ n");} return 0;}
Running result:
Example 2: the combined use of switch and if. Please enter the first letter of the week to determine what day it is, and continue to judge the second letter if the first letter is the same.
* Please enter the first letter of the week to determine what day it is, and continue to judge the second letter if the first letter is the same. * # include#include// this is the toupper function call header file int main () {char letter1; char letter2; printf ("Please enter the first letter of a certain day of the week: (M/W/F/T/S)\ n"); scanf ("% c", & letter1); letter1 = toupper (letter1); / / toupper is a function getchar () that converts lowercase letters to uppercase letters / / use getchar to eliminate the carriage return character'\ ncarriage. If there is no getchar, switch (letter1) / / then the carriage return character'\ ncarriage will be entered when the second letter is entered, which is directly judged as error {case 'M':printf ("Monday\ n"); break; case' W':printf ("Wednesday\ n") Break; case 'F':printf ("Friday\ n"); break; case' T':printf ("Please enter the second letter: (ugamot h)\ n"); scanf ("% c", & letter2) If (letter2 = ='u') / / case in conjunction with the if-else statement uses {printf ("Tuesday\ n");} else if (letter2 = ='h') {printf ("Thursday\ n") } else {printf ("error\ n");} break; case 'S':printf ("Please enter the second letter: (an Accord u)\ n"); scanf ("% c", & letter2) If (letter2 = ='a') {printf ("Saturday\ n");} else if (letter2 = ='u') {printf ("Sunday\ n") } else {printf ("error\ n");} break; default:printf ("error\ n");} return 0;}
Run result: find Tuesday
Loop nesting
The loop statement of the loop structure can be any legal C statement. If the loop body of a loop structure contains another loop statement, it constitutes the nesting of the loop, which is called multiple loops. Nesting means "complete inclusion", then the nesting of a loop refers to the complete inclusion of one or more loop structures in the body of a loop. Three kinds of loop control statements in C language can be nested together.
1. While loop nesting
(1) grammatical structure:
While (expression 1) {statement 1; while (expression 2) {statement 2;}. }
(2) example of while loop nesting program
Example 1: printing ninety-nine multiplication tables using while loop nesting
/ / print 99 multiplication table # includeint main () {int I = 1, j = 1; while (I < 10) {j = 1; while (j)
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.