Traditional Culture Encyclopedia - Traditional customs - What are the characteristics of structured design methods and programming languages? What are their basic control structures?

What are the characteristics of structured design methods and programming languages? What are their basic control structures?

The concept of structured program was first put forward from the unrestricted use of transfer statements in the previous programming process. The transfer statement can force the control flow of the program to any part of the program. In the traditional flow chart, this transfer function is described by the "very random" streamline mentioned in the previous section. If this migration occurs in many places of a program, it will lead to the disorder of program flow and program structure. Such a program is difficult to understand and accept, and it is also easy to make mistakes. Especially in the development of actual software products, more attention is paid to the readability and modifiability of software, and programs with this structure and style are not allowed. Therefore, three basic structures of the program are proposed.

When discussing the algorithm, we list three control processes: sequence, selection and cycle, which are the three basic structures emphasized by structured programming method. The execution process of the algorithm consists of a series of operations, and the execution order between these operations is the control structure of the program. 1996, computer scientists Bohm and Jacopini proved the fact that any simple or complex algorithm can be composed of three basic structures: sequence structure, selection structure and loop structure. So these three structures are called the three basic structures of programming. It is also a structure that must be adopted in structured programming.

1. sequence structure

Sequence structure indicates that the operations in the program are executed in the order in which they appear, and the process is shown in figure 1-6. S 1 and s2 in the figure represent two processing steps, which can be a non-transfer operation or a plurality of non-transfer operation sequences, even a null operation, or any of three basic structures. There is only one entry point a and one exit point b in the sequence structure. The characteristic of this structure is that the program starts from the entry point A and executes all operations in sequence until the exit point B, so it is called sequence structure. Figure 1-2 in the previous section shows the flow chart of a sequence structure. In fact, no matter what kind of structure is included in the program, the whole process of the program is sequential. For example, in the flowcharts shown in Figure 1-3, Figure 1-4 and Figure 1-5, the overall structural flow is executed from top to bottom.

Choose a structure

The selection structure indicates that there are branches in the processing steps of the program, and it needs to select one of them to execute according to certain conditions. There are three forms of choice structure: single choice, double choice and multiple choice.

Double selection is a typical selection structure, and its flow is shown in Figure 1-8, where s 1 and s2 are the same as those in the sequence structure. As can be seen from the figure, there is a judgment box at the entry point A of the structure, which indicates that there are two alternative branches in the program flow. If the condition is met, the processing of s 1 is executed, otherwise the processing of s2 is executed. It is worth noting that only one of these two branches can be selected and executed, but no matter which branch is selected for execution, the final process will definitely reach the exit point B of the structure. In the previous figure 1-3, the flow chart of double-selection structure is adopted.

When any processing of s 1 and s2 is empty, it means that there is only one branch in the structure to choose from. If the conditions are met, s 1 processing will be executed, otherwise it will enter the process exit B in sequence. That is to say, when the conditions are not met, nothing is executed, so it is called a single-choice structure, as shown in figure 1-7.

Multi-choice structure means that many branches, such as s 1, s2, ..., sn, as shown in figure 1-9, are encountered in the program flow, and the execution direction of the program will be determined according to the conditions. If the condition 1 is satisfied, the processing of s 1 is executed, and if the condition n is satisfied, the processing of Sn is executed. In short, one of the branches should be selected for execution according to the judgment conditions. No matter which branch you choose, the final process will reach the same exit. If the conditions of all branches are not met, go directly to the exit. Some programming languages do not support multi-choice structure, but all structured programming languages do. C language is a process-oriented structured programming language, which can realize this function very simply. In the fifth chapter, this book will introduce the application of various forms of choice structure in detail.

3. Ring structure

Loop structure means that the program repeatedly performs one or some operations until a certain condition is false (or true) before terminating the loop. The most important thing in the loop structure is: when to execute the loop? What operations need to be executed circularly? There are two basic forms of circulation structure: when-type circulation and until-type circulation, and the flow chart is shown in figure 1- 10. The operation in the dotted box in the figure is called the loop body, which refers to the processing steps from the loop entry point A to the loop exit point B, and is the part that needs to be executed circularly. And when to execute the loop depends on the conditions.

Type structure: it means that the conditions are judged first, and when the given conditions are met, the loop body is executed, and the process automatically returns to the loop entrance at the end of the loop; If the conditions are not met, exit the loop and go directly to the exit of the process. Because it is a "cycle when conditions are met", that is, it is judged first and then executed, so it is called a cycle. The process is shown in figure 1- 10(a).

Until loop: refers to executing the loop body directly from the entrance of the structure and judging the conditions at the end of the loop. If the condition is not satisfied, return to the entrance to continue executing the loop body, and then exit the loop until the condition is true and reach the exit of the process. Execute first, then judge. Because it is "until the condition is true", it is called the until-type loop. The process is shown in figure 1- 10(b). The flow chart of figure 1-5 in this chapter, summed by iterative method, is a typical until cycle structure.

Similarly, the loop structure has only one entry point A and one exit point B, and loop termination means that the process is executed at the exit point of the loop. The process represented in the diagram can be one or more operations or a complete structure or process.

The whole dotted box is a circular structure.

Through the three basic control structures, we can see that any basic structure in a structured program has a unique entrance and exit, and the program will not have an infinite loop. There is a good correspondence between the static form of the program and the dynamic execution flow.

1.3.2 north-south flow chart

N-S flow chart is one of the graphical tools used to represent algorithms in structured programming methods. For structured programming, the traditional flow chart has been difficult to fully adapt. Because the traditional flow chart appears earlier, it reflects the needs of machine instruction system design and traditional programming methods, so it is difficult to ensure a good program structure. In addition, some basic structures of structured programming have no corresponding expression symbols in the traditional flow chart. For example, in the traditional flow chart, the circular structure is still represented by judging structure symbols, so it is difficult to distinguish which structure it is. Especially, the traditional flow chart can't guarantee the top-down programming mode because of the turning point, and it is difficult to express the calling relationship between modules. For this reason, two American scholars, Nassi and Schneiderman, put forward a new flow chart form in 1973, that is, N-S flow chart, named after the initials of the two creators, also known as Nassi Shneiderman chart.

The basic unit of N-S graph is a rectangular frame, which has only one entrance and one exit. Rectangular frame is divided by lines with different shapes, which can represent sequence structure, selective structure and cyclic structure. In the N-S flow chart, the directional streamline is completely removed, and three basic structures of the program are represented by three rectangular boxes, which can be combined to represent all the algorithms. This flow chart eliminates the influence of arbitrary use control transfer on the program flow from the expression form, and limits the generation of bad program structure.

The basic symbols of the N-S flow chart corresponding to the three basic structures of sequence, selection and circulation are shown in figure1-1. Graph 1- 1 1(a) and graph1-kloc-0/(b) are the N-S graphs of the sequence structure and the selected structure, respectively, and graph1-1/kloc-. As can be seen from the figure, in the N-S diagram, the process always starts from the top of the rectangular frame and continues to the bottom of the rectangular frame, which is the entrance and exit of the process. In this form, unconditional transfer is impossible. Next, the N-S flow chart is used to show the algorithm for finding the function value m in the previous example 1-2, and the flow chart is shown in figure 1- 12.

It is worth noting that N-S flow chart is a graphic tool suitable for structured programming methods, but it cannot be used to represent unstructured programs.

For example 1-3, the algorithm for finding the greatest common divisor of any two positive integers is very classic. The algorithm is represented by the traditional flow chart of figure 1-5, but it cannot be directly represented by the N-S flow chart. Because the key of this algorithm is to execute a loop structure, the loop structure shown in figure 1-5 is neither the current loop nor the till loop, so it cannot be represented by the N-S flowchart. If the algorithm in example 1-3 is slightly adjusted to make the flow chart in the form of single-choice structure and the condition in it is changed to r≠0, the algorithm can be represented by the N-S flow chart of until loop. The figure 1- 13 is the N-S flow chart of the example 1-3.

N-S flow chart is one of the important graphic tools to describe the algorithm, which has been widely used in structured programming. This is just a brief introduction to attract jade. In actual software development, interested readers can refer to works on software engineering or software development technology.

1.3.3 structured programming method

Structured programming method is recognized as the basic method and principle that process-oriented programming should follow. Structured programming methods mainly include: ① only three basic program control structures are used to compile the program to make the program have a good structure; ② Top-down programming; (3) The algorithm is represented by structured programming flow chart.

There is a set of theories and techniques about structured programming and methods, which are constantly developing and improving. For beginners, it is difficult to master it completely. But at the beginning of learning, it is very helpful to understand the methods of structured programming and learn good programming ideas.

1. Structured programming features

The characteristics of structured programming mainly include the following points:

(1) program is described by the combination of three basic structures;

(2) The whole program adopts modular structure;

(3) Use the transfer statement restrictively, be very cautious when absolutely necessary, and only jump within one structure, and do not jump from one structure to another, so as to narrow the difference between the static structure and the dynamic execution process of the program and make people understand the function of the program correctly;

(4) Take the control structure as a unit, each structure has only one entrance and one exit, and the interface between units is simple and the logic is clear;

(5) Using structured programming language to write programs, and adopting a certain writing format to make the program structure clear and readable;

(6) Pay attention to the programming style.

2. Top-down design method

The general idea of structured programming is to adopt modular structure, from top to bottom, and gradually improve it. That is, first, a complex big problem is decomposed into several relatively independent small problems. If the small problem is still complicated, it can be decomposed into several sub-problems, so that the small problem or sub-problem can be simply expressed by the three basic structures of the program. Then, corresponding to each small problem or sub-problem, write a program block with independent functions, which is called a module like building blocks. Each module is disassembled one by one and then assembled in a unified way. In this way, the solution of a complex problem becomes the solution of several simple problems. This is a top-down and step-by-step programming method.

Specifically, a module is a collection of program objects. Modularization is to divide the program into several modules, each module completes a certain sub-function, and the problem can be solved by integrating these modules into a whole. This kind of program assembled with modules is called modular structure program. In the modular structure programming, the top-down and gradual refinement design method facilitates the decomposition of problems and the division of modules, so it is the basic principle of structured programming.

Example 1-9: Find a quadratic equation with one variable;

ax2+bx+c=0

The root of.

Analysis: From the top, the algorithm of solving problems can be divided into three small problems, namely, input problem, root problem and output problem. These three small problems are three functional modules of a quadratic equation: input module M 1, calculation processing module M2 and output module M3. Among them, M 1 module completes the input of necessary original data, M2 module solves it according to the root-finding algorithm, and M3 module completes the display or printing of the obtained results. This division makes the problem of finding the root of a quadratic equation in one variable become three relatively independent subproblems, and its module structure is shown in figure 1- 14.

These three decomposition modules are generally sequential structures. Among them, M 1 and M3 modules complete simple input and output, and can directly design program flow without decomposition. M2 module completes the root finding, and the root finding needs to judge whether the quadratic coefficient a is 0 first. When a=0, the equation degenerates into a linear equation, and the method of finding the root is different from that of quadratic equation. If a≠0, the root of quadratic equation should be found according to b2-4ac. It can be seen that the M2 module is complex and can be subdivided into two submodules M2 1 and M22, which correspond to the roots of the linear equation and quadratic equation respectively. The module structure is shown in figure 1- 15.

After such decomposition, the function of M2 1 sub-module is to find the root of a linear equation, and its algorithm is simple and can be directly expressed. M22 is the root of quadratic equation. The algorithm represented by the flow chart is shown in figure 1- 16. It consists of a simple sequence structure and a selection structure, and it is the most elaborate flow representation of M22 module. Then, according to the method of refining M22 module, the algorithms of M 1, M2 1 and M3 are represented by flowcharts respectively, and then assembled according to the module structures of Figure1-kloc-0/5 and Figure1-kloc-0/4 respectively, and finally a refined complete flowchart will be obtained.

It can be seen that programming is the same as Gai Lou. First of all, we should consider the overall structure of the building and ignore some details. After the overall framework is built, the details of each room will be solved step by step. In programming, first consider the top-level design of the problem, and then gradually refine it to complete the bottom-level design. The design method of top-down and gradual refinement conforms to the general law of solving complex problems, and is a method that people are used to accepting, which can significantly improve the efficiency of program design. Under the guidance of this top-down, divide-and-conquer method, the gradual refinement process from the whole to the part, from the whole to the details, and from the abstract to the concrete has been realized. The program written in this way has the characteristics of clear structure, which improves the readability and maintainability of the program.

3. Programming style

In a sense, programming style is a habit when individuals write programs. But the style problem does not involve a set of relatively perfect theories and rules like the method problem. Programming style is the refinement of programming experience and lessons, and programmers with different degrees and different application angles also have their own views on this issue. Because of this, programming style is easily overlooked by people, especially novices. Structured programming emphasizes the requirement of programming style. Because the programming style mainly affects the readability of the program. A program with good style should pay attention to the following points:

(1) statement formalization. Programming language is a formal language, which needs to be accurate. Therefore, rigid form and lively content are the demeanor of the software industry;

(2) program consistency. Keep the style and document format of all parts of the program consistent;

(3) structural standardization. Program structure, data structure and even software architecture should conform to the principle of structured programming;

(4) Appropriate use of notes. Annotation is an important means to help programmers understand the program and improve its readability. Comments can be added to a program or a line of programs as appropriate.

(5) The identifier is close to reality. The naming principle of data, variables and functions in programs is to choose meaningful identifiers for easy identification and understanding. Avoid using vague abbreviations and identifiers. For example, try to use V and I instead of A and B as variable names representing voltage and current. Avoid using non-intuitive variable names, such as aa, bb, etc.