The following describesCThe four program structures of the language.
1) Ordered Structure
The program design of the sequential structure is the simplest. You only need to write the corresponding statements in the order of solving the problem. The execution sequence of the statements is from top to bottom and executed in sequence.
For example, a = 3, B = 5, and the values of a and B are now exchanged. This problem is like switching two cups of water. Of course, the third cup is used, if the third cup is c, the correct procedure is:
- c = a;
- a = b;
- b = c;
The execution result is a = 5, B = c = 3. If you change the order, write it as follows:
- a = b;
- c = a;
- b =c;
Then the execution result becomes a = B = c = 5, which cannot achieve the expected purpose. Therefore, beginners are most likely to make this mistake. The sequential structure can be used independently to form a simple and complete program. Common Input and computing programs and three-step output programs are sequential structures, such as calculating the area of the circle, the statement sequence of the program is the input circle radius r, calculation s = 3.14159 * r, and output Circle area s.
However, in most cases, the sequential structure is a part of the program, which forms a complex program together with other structures, such as the compound statement in the branch structure and the loop body in the loop structure.
2) Branch Structure
Although the sequential structure program can solve problems such as computing and output, it cannot make judgments and then choose. The branch structure should be used for determining and selecting the branch first. The execution of the branch structure selects the execution path based on certain conditions, rather than strictly following the physical order of statements. The key to the program design method of the branch structure is to construct appropriate Branch Conditions and analyze program flows, and select appropriate branch statements based on different program flows.
The branch structure is suitable for computing with conditions such as logical or relational comparison. When designing such programs, you must first draw the program flowchart and then write the source program according to the program flow, in this way, the program design analysis is separated from the language, making the problem simple and easy to understand. The program execution flow chart is drawn based on the problem-solving analysis.
To learn the branch structure, do not be confused by the nested branch. As long as the flowchart is correctly drawn and the functions to be executed by each branch are clarified, the nested structure is not difficult. Nesting is nothing more than a branch statement. It is not a new knowledge. As long as you have a clear understanding of the dual branch, it is not difficult to nest the branch. Below I will introduce several basic branch structures.
① If (condition) {branch body}
In this branch structure, the branch body can be a statement. At this time, "{}" can be omitted, or multiple statements can be compound statements. It has two branch paths. One is to execute the branch body when the condition is true. Otherwise, the branch body will not be executed when the branch body is skipped. For example, to calculate the absolute value of x, according to the definition of the absolute value, we know that when x> = 0, its absolute value remains unchanged, while when x <0, its absolute value is the inverse Number of x, therefore, the program segment is: if (x <0) x =-x;
②
- If (condition)
- {Branch 1}
- Else
- {Branch 2}
This is a typical branch structure. If the condition is true, execute branch 1. Otherwise, execute branch 2. Both branch 1 and branch 2 can be composed of one or several statements.
For example, find the root of ax ^ 2 + bx + c = 0.
Analysis: When B ^ 2-4ac> = 0, the equation has two solid roots. Otherwise, B ^ 2-4ac <0) has two composite roots. The program segment is as follows:
- main()
- {
- int a,b,c,d,x,y;
- printf("Please put the number of a,b&c from the quadratic equation of one variable one by one\n");
- scanf("%d%d%d",&a,&b,&c);
- d=b*b-4*a*c;
- if(d<0)
- {
- printf("NO Root!Wrong!\n");
- }
- else
- {
- y=-b-sqrt(d)/2*a;
- x=-b+sqrt(d)/2*a;
- printf("The 1st equation root=%d\nThe 2nd equation root=%d",y,x);
- }
- }
③ IF nested branch statement:
The statement format is:
- If (condition 1) {branch 1}
- Else if condition 2) {branch 2}
- Else if Condition 3) {Branch 3}
- ......
- Else if condition n) {branch n}
- Else {branch n + 1}
- FOR nesting, the statement format is:
- For (Initial Value A; range A; Step)
- {
- For (Initial Value B; range B; Step B)
- {
- Loop body
- }
- }
FOR nested example: 9-9 multiplication table
- main()
- {
- int a,b,c;
- for(a=1;a<=9;a++)
- {
- for(b=1;b<=a;b++)
- {
- c=b*a;
- printf("%dx%d=%d ",b,a,c);
- }
- printf("\n");
- }
- }
Although nested branch statements can solve the problem of multiple entrances and exits, the statement structure becomes very complicated after more than three duplicates are nested, and it is extremely inconvenient to read and understand the program, it is recommended that the values be nested within three, and the following statement can be used if the values exceed three.
④ Switch statement
This statement is also a multi-branch selection statement. It depends on the switch setting, that is, the path where the expression value matches the constant expression. It is different from if... Else statement. All its branches are parallel. When the program is executed, it starts to look up from the first branch. If they match, execute the subsequent block, and then execute the 2nd branch, 3rd branch ...... Until the break statement is encountered. If not, check whether the next branch matches. When using this statement, pay special attention to the reasonable setting of the switch conditions and the rational application of the break statement.
3) loop structure:
The loop structure can reduce the workload of repeated Writing of source programs and describe the problem of repeated execution of a certain segment of algorithms. This is the program structure that best utilizes computer expertise in programming. The C language provides four loops, that is, goto loop, while loop, do? Cwhile loop and for loop. The four loops can be used to deal with the same problem. Generally, they can be replaced by each other, but the goto loop is generally not recommended, forced changes to the program sequence often lead to unexpected errors in program running. in learning, we mainly learn while, do... The while and for loops.
The most common three types of cyclic structure learning focus on figuring out their similarities and differences so that they can be used in different scenarios. This requires that the format and execution sequence of the three types of loops be clear, after understanding the flowchart of each loop, you will understand how to replace and use it. for example, you can re-compile a program with the for statement to better understand the functions of the while loop. Note that the loop body should contain the statement that tends to end, that is, the change of the variable value of the loop). Otherwise, it may become an endless loop, which is a common error for beginners.
After learning these three cycles, we should clarify their similarities and differences: Use the while and do... During the while loop, the initialization operation of the loop variable should be prior to the loop body, while the for loop is generally carried out in statement 1. Both the while loop and the for loop determine the expression first, and do... The while LOOP first executes the loop body and then judges the expression, that is, do... The while LOOP body is executed at least once, while the while loop and for may not be executed at once.
Note that all three cycles can jump out of the loop with the break statement, and end the loop with the continue statement, while the loop composed of the goto statement and the if statement, the break and continue statements cannot be used for control.
The order structure, branch structure, and loop structure are not isolated from each other. In a loop, there can be branches and order structures, and the branches can also have loops and order structures. In fact, no matter which structure, we can think of them as a statement in a broad sense. In the actual programming process, these three structures are often combined to implement various algorithms and design corresponding programs. However, programming is a big problem, programming is often very long and the structure is repeated, resulting in poor readability and hard to understand. The solution to this problem is to design the C program into a modular structure.
(4) modular program structure
The modular program structure of the C language is implemented by functions. The complicated C program is divided into several modules, and each module is compiled into a C function, then, the main function call function and function call function are used to compile a large-scale C program. Therefore, C program = Main Function + subfunction. Therefore, we should pay special attention to understanding and applying the definition, call, and return of values of functions, and consolidate them through machine debugging.
3. Master some simple algorithms
In fact, a major part of programming is to analyze the problem, find a solution to the problem, and then write the code in the corresponding programming language. This requires understanding of algorithms. According to the syllabus of "C programming", we only need to master some simple algorithms. After mastering these basic algorithms, it is easy to analyze the problem. For example, the exchange of two numbers, the comparison of three numbers, the sort by choice method, and the sort by Bubble Method require us to understand the inherent meaning of these algorithms.
Conclusion: When we grasp the above aspects, as long as the students can overcome the fear, dislike, concentrate on the lectures in class, and do a good job in exercises and computer debugging, the C language is not difficult to learn.