C language program structure, C language program structure
There are three program structures in C language: sequential structure, selection structure, and cyclic structure.
1. The program structure executed in the order of statements is called the sequential structure.
The following is an example of Triangle Area. The Code is as follows:
Example 1.1
1 # include <stdio. h> 2 int main () 3 {4 int width, height, s; 5 printf ("Enter the bottom width of the triangle: \ n"); 6 scanf ("% d ", & width); 7 printf ("Enter the triangle height: \ n"); 8 scanf ("% d", & height); 9 s = width * height/2; 10 printf ("Triangle Area: s = % d \ n", s); 11 return 0; 12}
When executing the program, you must first enter the bottom width of the triangle (6 rows), then enter the height (8 rows). The program runs down and calculates the area of the triangle (9 rows ), display the area (10 rows ). This is executed in the order of statements.
2. The program structure that uses different program segments for processing based on the establishment of a certain condition is called the selection structure.
Generally, the selected structure has two branches. If the condition is true, execute the program a segment. Otherwise, execute the program B segment. Sometimes, the two branches cannot fully describe the actual problem (for example, the score of A student is divided into A, B, C, D, E and other levels based on the score, such a program structure is called a multi-branch selection structure.
1. if simple statement
The syntax format is as follows:
If (expression)
{Statement 1}
Function: calculates the expression value. if it is true, Statement 1 is executed. Otherwise, Statement 1 is skipped and the next statement of the if statement is executed.
Example 2.1
1 # include <stdio. h> 2 int main () 3 {4 int age; 5 scanf ("% d", & age); 6 if (age> 18) 7 {8 printf ("you are an adult! "); 9} 10 re & turn 0; 11}
In the above program, the age variable is defined first. After the age value is input, the system determines whether the age is an adult based on the size of the age value. When running the Code, if the input value is greater than 18 years old, the output will be "you are an adult! ", Otherwise exit the program directly.
2. if-else statement
The syntax format is as follows:
If (expression)
{Statement 1}
Else
{Statement 2}
Function: Calculate the expression value. if the expression value is true, execute Statement 1, skip Statement 2, and continue to execute the next statement of the if-else statement. if the expression is false, skip Statement 1, execute Statement 2, and then continue to execute the next statement of the if-else statement.
Example 2.2
1 # include <stdio. h> 2 int main () 3 {4 int age; 5 scanf ("% d", & age); 6 if (age> 18) 7 {8 printf ("you are an adult! "); 9} 10 else11 {12 printff (" You are still a minor! "); 13} 14 re & turn 0; 15}
Compared with example 2.1, the above program has an additional statement. When running the Code, if the input value is more than 18 years old, the output will be "you are an adult! ", Otherwise output" you are still a minor! ".
3. nested if statements
In the if statement and if-else statement form, statement 1 or statement 2 can be any legal statement. If they are also if statements, they constitute nested if statements. There are multiple nested forms, which are described below.
Nested Form 1 nested Form 2 nested Form 2
If (expression 1) if (expression 1) if (expression 1)
{Statement 1}
If (expression 2) if (expression 2) else if (expression 2)
{Statement 1} {Statement 1} {Statement 2}
Else} else
{Statement 2} else {Statement 3}
} {Statement 2}
Else
{Statement 3}
Example 2.3: Find the following piecewise Functions
When x> 0, y = x + 1; when x = 0, y = x; when x <0, y = X-1;
Nested Form 1:
1 #include<stdio.h> 2 int main() 3 { 4 int x,y; 5 scanf("%d",&x); 6 if(x>=0) 7 { 8 if(x>0) 9 {10 y=x+1;11 }12 else13 { 14 y=x;15 }16 }17 else18 {19 y=x-1;20 }21 printf("x=%d,y=%d\n",x,y);22 return 0;23 }
Nested Form 2:
1 #include<stdio.h> 2 int main() 3 { 4 int x,y; 5 scanf("%d",&x); 6 y=x; 7 if(x>=0) 8 { 9 if(x>0)10 {11 y=x+1;12 }13 }14 else15 {16 y=x-1;17 }18 printf("x=%d,y=%d\n",x,y);19 return 0;20 }
Nested Form 3:
1 int main() 2 { 3 int x,y; 4 scanf("%d",&x); 5 if(x>0) 6 { 7 y=x+1; 8 } 9 else if(x=0)10 {11 y=x;12 }13 else 14 {15 y=x-1;16 }17 printf("x=%d,y=%d\n",x,y);18 return 0;19 }
4. switch statement
The format is as follows:
Switch ()
{
Case constant expression 1: Statement 1
Case constant expression 2: Statement 2
......
Case constant expression n: Statement n
Default: Statement n + 1
}
Idea: The switch statement is often used with break. The break function is to terminate the switch statement where it is located.
For example, 2.4 The Code is as follows:
1 # include <stdio. h> 2 int main () 3 {4 int x; 5 printf ("Enter Student Score: \ n"); 6 scanf ("% d", & x ); 7 switch (x/10) 8 {9 case 10: 10 case 9:11 printf ("A"); 12 break; 13 case 8:14 printf ("B"); 15 break; 16 case 7:17 printf ("C"); 18 break; 19 case 6:20 printf ("D"); 21 break; 22 case printf ("E"); 29 break; 30 default: 31 printf ("invalid data! "); 32} 33 return 0; 34}
In the preceding program, an integer variable x is defined. After the value of x is input, the score level is determined based on the range of x. When running the code, if 85 is input, "B" is output, which indicates that the score of the student is B. If the switch statement does not contain a break statement, the program continues to execute the following statement and outputs B, C, D, and E.
3. Control the execution of a code block multiple times and know the program structure that a condition is met. It is called a loop structure.
In C, while statements, do-while statements, and for statements are loop statements that directly control the loop process.
1. while statement
The syntax format is as follows:
While (expression)
{Loop body statement}
Function: Calculate the expression value first. If it is true, execute the loop body statement. After the statement is executed, calculate the expression value. If it is still true, the loop body statement is executed repeatedly. When the expression value is "false", the while statement is executed and the statement following the while statement is executed.
The following is an example of 1 + 2 + 3 + ...... The sum of 100 and is used as an example to describe the usage of the while LOOP statement.
Example 3.1
1 #include<stdio.h> 2 int main() 3 { 4 int s,i; 5 s=0; 6 i=1; 7 while(i<=100) 8 { 9 s=s+i;10 i++;11 }12 return 0;13 }
2. do-while statement
The syntax format is as follows:
Do
{Loop body statement}
While (expression );
Function: first execute the loop body statement, and then check the value of the Loop Control Condition expression. If it is true, the loop body statement is repeatedly executed; otherwise, the loop is exited.
The following is an example of 1 + 2 + 3 + ...... The sum of 100 and is used as an example to describe the usage of the do-while loop statement.
Example 3.2
1 #include<stdio.h> 2 int main() 3 { 4 int s,i; 5 s=0,i=1; 6 do 7 { 8 s=s+i; 9 i++;10 }while(i<=100);11 return 0;12 }
3. for statement
The syntax format is as follows:
For (expression 1; expression 2; expression 3)
{Loop body statement}
Function: calculate the value of expression 1, and then check the value of expression 2. If the value is true, execute the loop body statement. After the statement is executed, calculate expression 3. Then, test whether the value of expression 2 is true. If the value is true, continue to execute the loop body statement. If the value is false, terminate the loop.
The following is an example of 1 + 2 + 3 + ...... The sum of 100 and is used as an example to describe the usage of for loop statements.
Example 3.3
1 #include<stdio.h> 2 int main() 3 { 4 int i,s; 5 s=0; 6 for(i=1;i<=100;i++) 7 { 8 i++; 9 }10 return 0;11 }
From examples 3.1, 3.2, and 3.3, we can see that the three loop statements can be converted to each other. However, they also have their respective focuses. Appropriate statements should be selected based on different requirements.
- The while and for statements belong to the "when" loop, that is, "first judgment, then execution; while the do-while statement belongs to the" until "loop, that is," first execution, then judgment ".
- In practice, the for statement is often used to identify the number of loops, but it is natural to use the while or do-while statement to determine the number of loops.
- The three expressions of the for statement have multiple changes, such as omitting some or all expressions, or even writing the loop body into expression 3. The loop body is a null statement, to meet the syntax requirements of loop statements.
4. nested loops
The loop body statement of the loop structure can be any valid C statement. If the loop body of a loop structure contains another loop statement, it forms a nested loop, which is called a multiple loop.
The following uses * to output a rectangle as an example to describe the usage of loop nesting.
Example 3.4
1 # include <stdio. h> 2 int main () 3 {4 int I, j; 5 int width, height; 6 printf ("length and width of the input rectangle: \ n "); 7 scanf ("% d", & width, & height); 8 for (I = 1; I <= width; I ++) // I control the length of the rectangle 9 {10 for (j = 1; j <= height; j ++) // j controls the width of the rectangle 11 {12 printf ("*"); 13} 14 printf ("\ n "); // a line * returns a new line 15} 16 return 0; 17}
In the preceding procedure, four variables are applied: I, j, width, and height. Both I and j are used as cyclic control variables to control the width and height of the rectangle. When the width and height values are input, the width and height of the rectangle are determined, finally, use the for statement to output the rectangle.
If width = 5, height = 2, the output result is as follows:
*****
*****