C language four kinds of program structure

1. Sequential structure

Sequential programming is the simplest, as long as the corresponding statements are written in the order in which the problem is solved, it is executed from top to bottom.

For example; A = 3, b = 5, now swap the values of a and b, the problem is like swapping two cups of water, this of course uses the third cup, if the third cup is C, then the correct procedure is: c = a; A = b; B = c. The result is a = 5, b = c = 3.

If you change the order, write: A = b; C = a; B = c. The execution result will become a = b = C = 5, can not achieve the expected purpose, this is our beginners are most prone to make this mistake, so we must pay more attention to.

The sequence structure can be used independently to form a simple and complete program. The common input, calculation and output three-step program is the sequence structure. For example, to calculate the area of a circle, the program’s statement sequence is the radius of the input circle R, to calculate s = 3.14159 RR, and the area of the output circle S.

But in most cases, the sequential structure is a part of the program, together with other structures to form a complex program, such as compound statements in branch structure, loop body in loop structure and so on.

2. Branch structure

Although the sequential structure of the program can solve the calculation, output and other problems, but can not make judgment and then choose. The branching structure is used for decisions before choices.

The execution of a branching structure is based on a selection of execution paths based on certain conditions, rather than strictly following the physical order in which statements appear. The key of the program design method of branch structure is to construct appropriate branch conditions and analyze program flow and select appropriate branch statements according to different program flow.

Branch structure is suitable for calculation with logical or relational comparison and other conditional judgment, the design of this kind of program is often to draw its program flow chart, and then write the source program according to the program flow, so as to separate the program design analysis and language, make the problem simple, easy to understand.

Program flow chart is a program execution flow chart drawn based on problem analysis.

This is a typical branch structure. If the condition is true, branch 1 is executed, otherwise branch 2 is executed. Both branch 1 and branch 2 can be composed of one or more statements. Find the root of ax^2+bx+c=0

Analysis: Because the equation has two real roots when b^2-4ac>=0, otherwise (b^2-4ac<0) has two conjugate complex roots. The program segment is as follows:

d=b*b-4*a*c; if(d>=0) { x1=(-b+sqrt(d))/2a; x1=(-b-sqrt(d))/2a; Printf (" x1 = % 8.4 f, x2 = % 8.4 f \ n ", x1, x2); } else { r=-b/(2*a); i =sqrt(-d)/(2*a); Printf (" x1 = % 8.4 f + % 8.4 fi \ n "r, I); Printf (" x2 = % % 8.4 f - 8.4 fi \ n "r, I); }Copy the code

③ Nested branch statement: its statement format is: if(condition 1) {branch 1}; Else if (conditional 2) {branch 2} else if (conditional 3) {branch 3}… Else if (condition n) {branch n} else {branch n+1}

Although nested branch statements can solve the problem of multiple entrances and exits, but after more than 3 times of nesting, the statement structure becomes very complicated, and it is very inconvenient for the program to read and understand. It is recommended to nest within 3 times, and if more than 3 times, the following statements can be used.

This statement is also a multi-branch select statement, which part of the execution depends on the switch Settings, i.e., the value of the expression matches the constant expression.

It is different if… Else statement, all of its branches are parallel. When the program executes, the first branch is searched, and if it matches, the next block is executed, then the second branch, then the third branch… Block until a break statement is encountered; If not, find out if the next branch matches.

When this statement is used, special attention should be paid to the reasonable setting of the switch condition and the reasonable application of the break statement.

3. Circular structure

Loop structure can reduce the workload of the source program repeated writing, used to describe the problem of repeated execution of a certain algorithm, which is the most able to play the advantages of the computer program structure in the program design, C language provides four cycles, that is, goto cycle, while cycle, do – while cycle and for cycle.

The four loops can be used to deal with the same problem. In general, they can be replaced by each other, but goto loops are generally not recommended, because forcing to change the order of the program often brings unexpected errors to the operation of the program. In learning, we mainly learn while, do… While and for loops.

The key point of learning the three commonly used cycle structures is to make clear the similarities and differences between them, so that they can be used in different occasions. This is to know the format and execution sequence of the three cycles, and to understand the flow chart of each cycle thoroughly, you will understand how to replace them.

For example, take the example of the while loop and rewrite a program with a for statement to better understand what they do. In particular, it is important to include terminating statements in the body of the loop (that is, changes in the value of the loop variable), otherwise it may become an infinite loop, a common mistake for beginners.

After learning these three loops, you should make clear the similarities and differences between them: use while and do… While loop variables should be initialized before the body of the loop, while for loop variables are initialized in statement 1.

While and for loops evaluate the expression and then execute the body, while do… The while loop evaluates the expression after executing the body of the loop. The body of the while loop is executed at least once, while the while loop and for may not be executed at all.

Note also that all three loops can be broken with a break statement and terminated with a continue statement, whereas goto and if loops cannot be controlled with a break or continue statement.

The order structure, branch structure and loop structure are not isolated from each other. There can be branch and sequence structure in the loop, and there can also be loop and sequence structure in the branch. In fact, no matter what kind of structure, we can regard them as a statement in a broad sense.

4, modular program structure

C language modular program structure with functions to achieve, that is, complex C program is divided into several modules, each module is written into a C function, and then through the main function call function and function call function to achieve a large problem of C programming.

Therefore, it is often said: C program = main function + child function. Therefore, the definition of function, call, value return, etc., should pay special attention to understanding and application, and through the machine debugging to consolidate.