Home >Common Problem >What are the three basic control structures of a program?
Any simple or complex algorithm can be composed of three basic structures: sequential structure, selection structure, and loop structure. Therefore, these three structures are the basic structures of programming and are also necessary for structured programming. The structure adopted.
The operating environment of this tutorial: Windows 7 system, Dell G3 computer.
Structured programming mainly emphasizes the algorithm for realizing a certain function, and the implementation process of the algorithm is composed of a series of operations. The execution order between these operations is the control structure of the program. Any simple or complex algorithm can be composed of three basic structures: sequential structure, selection structure, and loop structure. Therefore, these three structures are the basic structures of programming and the structures that must be used in structured programming.
Sequential structure:
The sequential structure indicates that each operation in the program is executed in sequence according to the order in which they are arranged in the source code. The process is as shown in the figure.
The processing steps in the figure can be one non-transfer operation or multiple non-transfer operations, or even a no-op, or any of the three basic structures. structure. The entire sequence structure has only one entry point and one exit point. The characteristics of this structure are: the program starts execution from the entry point and performs all operations in sequence until the exit point, so it is called a sequential structure.
Selection structure:
The selection structure indicates that the program processing needs to select one of the branches for execution based on a specific condition. There are three types of selection structures: single selection, double selection and multiple selection.
Double selection is a typical selection structure, and its process is as shown in the figure.
There is a judgment condition at A at the structure entrance, which indicates that there are optional branches in the program flow. If the judgment condition is true, execute step 1, otherwise execute Processing step 2. Only one of these two branches can be selected and one must be selected for execution, but no matter which one is selected, the final process must reach the exit point B of the structure.
When either of the two processing steps is empty, it means that there is only one branch available for selection in the structure. If a branch without processing steps is selected, nothing will be executed, which is called a single-selection structure. . As shown in the figure:
#Multi-selection structure means that multiple branches are encountered in the program flow, and the program execution direction is determined based on judgment conditions. As shown in the figure:
If condition 1 is true, execute step 1; if condition 1 is false and condition 2 is true, execute step 2; if If condition 1 is false, condition 2 is false, condition 3 is true, choose to execute step 3...and so on. It can be seen from the figure that the further back, the more stringent the conditions that need to be met. No matter which branch is chosen, the final process must reach the same exit point B. If the conditions of all branches are not met, it will directly reach exit point B.
Loop structure:
The loop structure means that the program repeatedly performs one or more operations until a certain condition is false (or true) before stopping the loop. There are two basic forms of loop structures: when-type loops and until-type loops.
*When-type structure: *As shown in the figure:
# Judge the condition first, execute the loop body when the condition is true, and automatically when the loop body ends Return to the loop entrance and judge the loop condition again; if the condition is false, exit the loop body and reach the process exit. Because it is "execute the loop when the condition is true", that is, judge first and then execute, it is called a when loop.
*Until loop: *As shown in the figure:
#The loop body is executed directly from the entrance. The condition is judged at the end of the loop body. If the condition is true, Then return to the entrance and continue executing the loop body until the condition is false and the loop ends and reaches the process exit. This is executed first and then judged. Because it "ends the loop until the condition is false", it is called a until-type loop.
Similarly, the loop structure has only one entry point A and one exit point B. Loop termination means that the process is executed to the exit point of the loop. The processing steps in the figure can be one or more operations, or a complete structure or process.
For more computer-related knowledge, please visit the FAQ column!
The above is the detailed content of What are the three basic control structures of a program?. For more information, please follow other related articles on the PHP Chinese website!